Skip to content

Building a Docker ImageΒΆ

OverviewΒΆ

A Docker image is built using a Dockerfile, which defines the steps required to package an application.


Example DockerfileΒΆ

# Use base image
FROM node:18

# Set working directory
WORKDIR /app

# Copy files
COPY package.json .
COPY . .

# Install dependencies
RUN npm install

# Expose port
EXPOSE 3000

# Start application
CMD ["npm", "start"]

Build the ImageΒΆ

docker build -t my-app:1.0 .


Run the ContainerΒΆ

docker run -p 3000:3000 my-app:1.0


How the Build WorksΒΆ

Docker builds images in layers:

  • Base image is pulled
  • Each instruction creates a new layer
  • Layers are cached for efficiency

Image Layering ConceptΒΆ

graph TD BaseImage --> Layer1 Layer1 --> Layer2 Layer2 --> FinalImage

Best PracticesΒΆ

  • Use small base images (e.g. alpine)
  • Minimise number of layers
  • Use .dockerignore
  • Avoid running as root
  • Pin dependency versions

Common MistakesΒΆ

  • Copying unnecessary files into image
  • Not using caching effectively
  • Large image sizes

Real-World ImplicationsΒΆ

  • Smaller images = faster deployments
  • Efficient builds reduce CI/CD time
  • Clean images reduce security risks

Key TakeawaysΒΆ

  • Dockerfiles define how images are built
  • Images are layered and cached
  • Good practices improve performance and security