Optimize Docker Image Using Volume Mounting and .Dockerignore

Nawarajprasain
Bajra Technologies Blog
3 min readNov 9, 2023

--

Docker has become an integral part of the modern development workflow due to its ability to create portable and reproducible containers. However, constantly rebuilding Docker images during development can be time-consuming and hinder productivity.

Copying all heavy storage files while building an image takes time since all the files need to be copied inside a container, impacting the deployment of any particular application.

To reduce Docker image size, developers can use minimal base images, minimize the number of layers, and leverage multi-stage builds.

Developers can use volume mounting and the .dockerignore file to speed up Docker builds.

Volume mounting allows developers to bind-mount their code into the running container and avoid rebuilds as much as possible, keeping iteration times short and avoiding busy waiting. The .dockerignore file allows developers to exclude irrelevant files and directories from the build context, significantly speeding up builds.

In this blog, we will explore how you can speed up Docker builds using volume mounting and the .dockerignore file, allowing instant code updates inside the container without the need to rebuild the entire image.

Volume Mounting: A Time-Saving Technique

Volume mounting is a feature in Docker that links a local directory or file from your host machine to a specific location within the container. The feature allows you to write code on your local system and see the changes instantly reflected in the running container. With volume mounting, developers can significantly reduce the time spent waiting for Docker builds, leading to a more efficient development process.

Example: Setting up Volume Mounting

Assuming we have a simple Node.js application in a directory named `my-app`:

```
my-app/
|- package.json
|- package-lock.json
|- index.js
|- …
```

We can use volume mounting in Docker Compose to mount the `my-app` directory into the container:

Create a Dockerfile on the root directory:

```Dockerfile
# Use the base Node.js image
FROM node:14-alpine
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
COPY . .
# Start the application
CMD [“node”, “index.js”]
```

Create a docker-compose.yml file:

```yaml
version: ‘3’
services:
app:
build:
context: ./my-app
dockerfile: Dockerfile
ports:
— “3000:3000”
volumes:
— ./my-app:/app
```

Explanation:

- The `docker-compose.yml` file specifies the `./my-app` directory as a volume to mount at the `/app` directory inside the container.

- The `context: ./my-app` parameter tells Docker to use the `my-app` directory as the build context for the image.

The Power of .dockerignore

While volume mounting enables rapid updates inside the container, it is essential to avoid copying unnecessary files into the container during the build process. The `.dockerignore` file allows you to exclude specific files and directories from being copied into the image, leading to a more efficient build.

By excluding unnecessary files and directories, developers can reduce Docker image size, speed up Docker builds, and avoid unintended secret exposure.

The .dockerignore file is placed in the same directory as the Dockerfile and is used during the build process to determine which files should be included or excluded from the Docker image.

Example: Using .dockerignore to Optimize Build

Create a .dockerignore file in the root directory (outside of `my-app`):

```
# .dockerignore
node_modules
npm-debug.log
Dockerfile
Dockerfile.dev
.git
.gitignore
README.md
```

Explanation:

- The .dockerignore file lists files and directories not to include in the build context. For instance, `node_modules` is excluded as it will be installed within the container.

Enhancing Development Workflow

By combining volume mounting with the .dockerignore file, developers can optimize the Docker build process and achieve faster iteration cycles during development. Here’s how the setup improves the workflow:

- Make code changes on the local machine (host).

- The changes are instantly mirrored inside the running container due to volume mounting.

- The .dockerignore file ensures that only the necessary files are included in the build context, reducing unnecessary overhead.

Conclusion

Docker volume mounting and the .dockerignore file are powerful tools that enhance the development workflow by reducing Docker build times.

These techniques enable developers to make code changes and see the results immediately, leading to faster iterations and increased productivity.

Your development team can implement it to enjoy a seamless and efficient Docker-using experience. Happy Coding!

--

--