Build Context in Docker

The build context in Docker refers to the files and directories that are accessible to the Docker daemon during the image build process. When you run a docker build command, you specify a directory (the build context) from which Docker will read files and directories to create the image.

How the Build Context Works

When we run docker build . “.” represents the build context path which means when we run this command it will send the current complete repository to the docker daemon. Then daemon will use these repo files/folders during execution if these are being used in Dockerfile steps. Here it will automatically detect the Dockerfile and execute it. 

docker build -t my-image:latest .

In this example, the “.” at the end of the command specifies the current directory as the build context. That’s why we should not keep any unnecessary files at the build location. We will talk about the solution later in the article.

Importance of Build Context

Efficiency and Performance

The size of the build context can significantly impact the build process. A large build context can slow down the build process because Docker has to send all the files to the daemon before starting the build. It is essential to keep the build context as small as possible to ensure efficient builds.

Security

Including unnecessary files in the build context can pose security risks. Sensitive files or directories that are not required for the build should be excluded from the build context to avoid unintentional inclusion in the image.

Managing Build Context Effectively

Using .dockerignore File

One of the most effective ways to manage the build context is by using a .dockerignore file. Similar to a .gitignore file, a .dockerignore file specifies files and directories that should be excluded from the build context. This helps reduce the size of the build context and ensures that only necessary files are sent to the Docker daemon.

Example .dockerignore File
# Ignore node_modules directory
node_modules

# Ignore temporary files
*.tmp
*.log

# Ignore sensitive files
secret.env
Structuring Your Project

Organizing your project structure can also help manage the build context more effectively. Keep the Dockerfile and necessary files in a dedicated directory to minimize the build context size.

Example Project Structure
my-project/
|-- Dockerfile
|-- .dockerignore
|-- src/
|-- public/
|-- config/

In this structure, only the necessary directories and files are included in the build context, while other files are excluded using the .dockerignore file.

Specifying a Custom Build Context

You can specify a custom build context directory if your Dockerfile is located outside the build context. This is useful when you want to keep your Dockerfile in a separate directory.

Example Command
docker build -f /path/to/Dockerfile -t my-image:latest /path/to/context

In this command, -f specifies the path to the Dockerfile, and /path/to/context specifies the build context directory.

Examples of Build Context

Example 1: Building an Image with a Small Build Context

Consider a simple Node.js application with the following project structure:

my-node-app/
|-- Dockerfile
|-- .dockerignore
|-- app.js
|-- package.json
|-- node_modules/
|-- logs/

Dockerfile

FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Expose port and run the application
EXPOSE 8080
CMD ["node", "app.js"]

.dockerignore

node_modules
logs
*.log

Now run the below build command

docker build -t my-node-app:latest .
Example 2: Using a Custom Build Context

Suppose you have a project with the following structure, and you want to keep the Dockerfile in a separate directory:

my-project/
|-- docker/
|   |-- Dockerfile
|-- src/
|   |-- app.js
|   |-- package.json

Dockerfile

FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Expose port and run the application
EXPOSE 8080
CMD ["node", "app.js"]

Build Command

docker build -f docker/Dockerfile -t my-project:latest src

In this command, -f docker/Dockerfile specifies the Dockerfile’s location, and src specifies the build context directory.

That’s all for now.
Thank you for reading!!

Stay tuned for more articles on Cloud and DevOps. Don’t forget to follow me for regular updates and insights.

Let’s Connect: LinkedIn

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top