CodeGym /Courses /Docker SELF /Building the image

Building the image

Docker SELF
Level 13 , Lesson 3
Available

4.1 Basics of the docker build Command

The docker build command is the main way to create a Docker image from a Dockerfile. It takes instructions from the Dockerfile and executes them step by step, forming an image layer by layer. In this lecture, we'll break down how the docker build command works, what options it has, and how to use it in practice.

The docker build command builds an image from a Dockerfile and a so-called build context. The build context is the files Docker uses to create the image. This could be a local folder on your machine or, for example, a repository on GitHub.

Syntax


docker build [OPTIONS] PATH | URL | -

Where:

  • PATH: the path to the directory containing the Dockerfile and build context.
  • URL: the URL of a remote repository.
  • -: reads the Dockerfile from standard input (stdin).

Basic Usage Example

In this example, Docker will use the Dockerfile from the current directory (.) and create an image with the name myimage and the tag latest.

Terminal

docker build -t myimage:latest .

4.2 Customizing the build Command

Main options for the docker build command

The docker build command supports many options that allow you to customize the build process.

1. Options -t, --tag

The -t or --tag option is used to assign a name and tag to the created image.

Terminal

docker build -t myimage:latest .

2. Options -f, --file

The -f or --file option allows you to specify an alternative Dockerfile if it differs from the default Dockerfile.

Terminal

docker build -f Dockerfile.dev -t myimage:dev .

3. Option --build-arg

The --build-arg option is used to pass build arguments defined using the ARG instruction in the Dockerfile.

Terminal

docker build --build-arg APP_VERSION=1.0 -t myimage:1.0 .

4. Option --no-cache

The --no-cache option allows you to build without using the cache. This is useful if you need to ensure that all commands are executed from scratch.

Terminal

docker build --no-cache -t myimage:latest .

5. Option --target

The --target option is used to specify the target stage in a multi-stage build.

Terminal

docker build --target builder -t myimage:builder .

6. Option --rm

The --rm option tells Docker to remove intermediate containers after a successful image build (enabled by default).

Terminal

docker build --rm -t myimage:latest .

4.3 Docker Build Examples

Example 1: Basic Build

Let's say you have a simple Node.js app, and you want to create a Docker image to deploy it.

Dockerfile:

dockerfile

FROM node:14

WORKDIR /app
        
COPY package*.json ./
RUN npm install
        
COPY . .
        
EXPOSE 3000
        
CMD ["node", "app.js"]

Build Command:

Terminal

docker build -t mynodeapp:latest .

In this example, Docker will create the image mynodeapp with the tag latest using the Dockerfile from the current directory.

Example 2: Build with Arguments

Build arguments let you pass variables into the Dockerfile during the build process.

Dockerfile:

dockerfile

FROM node:14

ARG APP_VERSION
ENV APP_VERSION=${APP_VERSION}
        
WORKDIR /app
        
COPY package*.json ./
RUN npm install
        
COPY . .
        
EXPOSE 3000
        
CMD ["node", "app.js"]

Build Command:

Terminal

docker build --build-arg APP_VERSION=1.0 -t mynodeapp:1.0 .

In this example, the APP_VERSION argument is passed into the Dockerfile, allowing you to define the app's version during the build.

Example 3: Multi-stage Build

Multi-stage builds are used to create images that include only the necessary components. This helps reduce the final image size.

Dockerfile:

Dockerfile


# Build stage
FROM node:14 AS builder
        
WORKDIR /app
        
COPY package*.json ./
RUN npm install
        
COPY . .
RUN npm run build
        
# Create the final image with minimal files
FROM nginx:alpine
        
COPY --from=builder /app/build /usr/share/nginx/html

Build Command:

Terminal


docker build -t mywebapp:latest .

In this example, an intermediate builder image is created first to build the app. After the build, the app is transferred to the final image, which uses Nginx to serve the built content.

4.4 Practical Tips

1. Optimize the order of instructions

To make the most out of the Docker cache, it's important to arrange your instructions properly. For instance, copy files that rarely change first, like package.json, to minimize dependency rebuilds. Then copy the rest of the files.

dockerfile

COPY package*.json ./
RUN npm install
COPY . .

2. Use .dockerignore

Add a .dockerignore file to exclude unnecessary files and directories from the build context. This shrinks the context size and speeds up the image building process.

Example of .dockerignore:

Text

node_modules
dist
*.log

3. Multi-stage builds

Use multi-stage builds to create minimal and lightweight images. This approach includes only the files and dependencies needed for the app to run, excluding unnecessary stuff like temporary files or source code from the final image.

3
Task
Docker SELF, level 13, lesson 3
Locked
Basic Image Build
Basic Image Build
3
Task
Docker SELF, level 13, lesson 3
Locked
Building an image using an argument
Building an image using an argument
3
Task
Docker SELF, level 13, lesson 3
Locked
Building an image without cache
Building an image without cache
3
Task
Docker SELF, level 13, lesson 3
Locked
Multi-stage build
Multi-stage build
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION