Docker

Docker mkdir

Docker has quickly become the most influential and fundamental way of managing and packaging software and the required dependencies to run in different environments.

As you can guess, when working with real-world applications, you will create new directories to store the required code, packages, logs, and more.

In this tutorial, we will learn how to create directories in Docker containers by defining them inside a Dockerfile. This will help ensure that all the required directories are created in any container that uses your Dockerfile as the base image.

Create Directory Using Mkdir Command

In a Dockerfile, we follow the format of:

INSTRUCTION arguments

The INSTRUCTION is a case-insensitive command that tells the Docker engine what to do.

One of the most common instructions in a Dockerfile is the RUN command. The RUN command follows the following syntax:

RUN <command>

This default format allows you to execute a shell command using the /bin/sh on Linux and cmd /S /C on Windows.

The second format is as shown below:

RUN [executable. Parameter1, parameter2]

In this format, we tell the docker engine to execute a specific executable using the provided parameters.

In our case, we are interested in the first format, which allows us to execute a shell command. We can use this format to invoke the mkdir command on Linux systems to create any directories we wish.

Let us discuss how we can use this feature to create directories in Docker containers by defining them in the Dockerfile.

Requirements

To follow along with this tutorial, we assume you have the following requirements met:

  1. The latest version of the Docker engine is installed on your machine.
  2. A sudo or root permission to invoke the docker commands on your system.
  3. Network access to download images.
  4. Basic knowledge of working with Docker and defining Dockerfiles.

With the above requirements met, we can learn how to configure a Dockerfile to create directories.

Dockerfile Create Simple Directory

As mentioned, we can create a directory during the image build by using the RUN instruction followed by the mkdir command.

An example is as shown:

FROM ubuntu:22.04

RUN mkdir /custom

In this case, we start by defining the base image, which uses the Ubunt 22.04 base image. We then use the RUN instruction followed by the mkdir shell command to create a directory called custom in the / directory.

Dockerfile Create Nested Directories

If you are familiar with the workings and parameters of the mkdir command, you know that we can use the -p parameter to create nested directories.

An example definition is as shown:

FROM ubuntu:22.04

RUN mkdir -p /custom/sub

Using the -p flag, we ensure that the mkdir command will create the parent directories if they don’t exist.

Docker Create Directory in Running Container

In other cases, you might need to create a directory in a running container. For that, we can use the docker exec command to accomplish this. Feel free to reference our tutorial on docker exec to learn more about how this command works.

To create a directory in a running container using the docker exec command, use the command syntax as shown:

$ docker exec CONTAINER_ID mkdir -p /path/to/directory

Replace the CONTAINER_ID with the ID of the container on which you wish to create a directory.

Docker Create Directory Using Volumes

Another feature of Docker that allows us to create container directories is using volumes. Instead of creating files and directories inside the container filesystems, we can use the volumes to mount and share among containers.

The following command shows how we can use the v Docker volumes feature to create a directory for a given container.

docker run -v /host/path:/container/path image_name

If we run the command above, Docker will mount the host directory specified in the /host/path to the /container/path in the target container.

Conclusion

In this tutorial, we explored all the primary and efficient methods to create directories in Docker containers using the provided docker features. We covered the basics, such as defining the directory creations in Dockerfiles, using docker exec commands, and more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list