DEV Community

Cover image for Docker – ARG Directive, .dockerignore, and Docker Volumes
Ramya Perumal
Ramya Perumal

Posted on

Docker – ARG Directive, .dockerignore, and Docker Volumes

ARG Directive

The ARG directive acts like a variable. We can define it inside the Dockerfile and change its value during the image build process.

ARG PYTHON_VERSION=3.8
FROM python:${PYTHON_VERSION}-slim
Enter fullscreen mode Exit fullscreen mode

Here, the Python version in the Dockerfile is set to 3.8. However, during the build process, we can change it to 3.10.

docker build -f Dockerfile --build-arg PYTHON_VERSION=3.10 -t helloworld_flask:v1 .
Enter fullscreen mode Exit fullscreen mode
  • -t means tag.
  • -f means Dockerfile path.

We can use ARG to make base image versions and other build-time values configurable.

Example

FROM node:20-alpine

# 1. Define the arguments
ARG APP_DIR=app
ARG INSTALL_ARGS="--omit=dev"

# 2. Use them in instructions
WORKDIR /${APP_DIR}
COPY . .
RUN npm install ${INSTALL_ARGS}
Enter fullscreen mode Exit fullscreen mode

Note: ARG values can only be changed during the image build process. They cannot be changed during container creation.


Docker Ignore

A .dockerignore file is used to specify files and directories that should not be copied into the Docker build context.

Create a file named .dockerignore in the application's root directory.

Examples of files and folders that can be ignored:

Dockerfile
.venv
__pycache__
*.pyc
requirements.txt
.git
.gitignore
Enter fullscreen mode Exit fullscreen mode

Ignoring unnecessary files reduces the build context size and speeds up image builds.


Docker Volumes

Generally, when a container is created, a writable layer is also created.

If we create files inside the container, they are stored in the writable layer. However, when the container is deleted, all data in the writable layer is lost.

What if we need to store files permanently on the host machine?

This is where Docker volumes come into the picture.

Docker volumes allow data to persist independently of the container lifecycle.

When a volume is mounted between a host directory and a container directory:

  • Files created in the container appear on the host machine.
  • Files created on the host machine appear inside the container.
  • Changes are synchronized between both locations.

Types of Docker Volumes

  1. Bind-Mounted Volumes
  2. Docker Managed Volumes (Named Volumes)

1. Bind-Mounted Volumes

A bind mount creates a mapping between a host directory and a container directory.

docker run -it -v ./data:/data busybox:1.36 sh
Enter fullscreen mode Exit fullscreen mode

Here:

  • ./data = Host machine directory
  • /data = Container directory

Characteristics:

  • Tightly coupled with the host file system.
  • Multiple containers can share the same host directory.
  • Changes made in either location are reflected in the other.

Note: The host directory is not deleted when the container is removed.


2. Docker Managed Volumes (Named Volumes)

Create a Docker-managed volume:

docker volume create dockersession
Enter fullscreen mode Exit fullscreen mode

This creates a volume outside the container lifecycle.

List Volumes

docker volume ls
Enter fullscreen mode Exit fullscreen mode

Inspect a Volume

docker volume inspect dockersession
Enter fullscreen mode Exit fullscreen mode

This displays information about the volume, including its mount location.

Example Linux location:

/var/lib/docker/volumes/dockersession/_data
Enter fullscreen mode Exit fullscreen mode

Mount the Volume to a Container

docker run -it -v dockersession:/data123 busybox:1.36 sh
Enter fullscreen mode Exit fullscreen mode

Here:

  • dockersession = Volume name
  • /data123 = Container directory

Multiple containers can use the same volume for data sharing.

Find Containers Using a Specific Volume

docker ps -a --filter volume=dockersession
Enter fullscreen mode Exit fullscreen mode

One of the major benefits of Docker volumes is that they are completely decoupled from the container lifecycle.

When a container is deleted, the volume and all its data remain safely stored on the host machine.


Interview Questions

Question:

What is the primary use of the ARG instruction in Docker?

Answer: To pass build-time variables to the Dockerfile.


Question:

Which of the following is true about ARG variables in Docker?

Answer: They are used only during the image build process.


Question:

Can an ARG variable be used in a RUN instruction within a Dockerfile?

Answer: Yes, but only after it has been declared.


Question:

Which files can be ignored using .dockerignore?

Answer: Any file or directory within the build context.


Question:

What is the purpose of Docker volumes?

Answer: To store data that persists even after a container is destroyed.


Question:

What is the default location of Docker volumes on Linux systems?

Answer:

/var/lib/docker/volumes
Enter fullscreen mode Exit fullscreen mode

Question:

Which command allows you to list all Docker volumes?

Answer:

docker volume ls
Enter fullscreen mode Exit fullscreen mode

Question:

In which scenario would you use a bind-mounted volume?

Answer: When you need to share specific directories between the host machine and a container.

Top comments (0)