Use the Dockerfile to create a directory in the container using the Mkdir command
Docker containers have become the de facto way to manage software and dependencies in different environments. When working with real-world applications, you will undoubtedly need to create a Dockerfile before you can build your application container image.
Use Dockerfile to create a directory in the container using the mkdir command
In addition to allowing developers to assemble only the commands needed to create a Docker image, we can do more with Dockerfiles. To successfully create a Docker image using a Dockerfile, you must understand some basic commands.
Some of the most commonly used commands include:
- FROM - creates the parent/base image layer to be used.
- WORKDIR - allows us to set the working directory.
- COPY - allows us to copy the contents of the current directory to a directory in the container.
- PULL - Add files from your Docker repository.
- RUN - command to be executed when we want to build the image.
- CMD - Specifies the command to run when the container starts.
- ENV - Defines environment variables used during the build.
- ENTRYPOINT - Specifies the command to run when the container starts.
- MAINTAINER - specifies the author of the image.
Using the above command, we can create a Dockerfile as shown below, which uses Python as the base image.
Code:
# base image
FROM Python
# Set your working directory
WORKDIR /var/www/
# Copy the necessary files
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt
# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
# Run the app
CMD ["echo", "Hello, Developer"]
This Dockerfile is part of a simple Flask application that just prints a message to the console. If you are interested in creating the same application, here is the requirements.txt file.
click==8.0.4
Flask==2.0.3
gunicorn==20.1.0
itsdangerous==2.1.0
Jinja2==3.0.3
MarkupSafe==2.1.0
Werkzeug==2.0.3
The main file app.py which contains the main application file is shown below.
Code:
from flask import Flask
app = Flask(__name__)
def hello():
print("Hello, this is a simple Flask application")
hello()
Now, to create a directory in our Docker container's file system using the mkdir command, we will use the RUN command as shown below.
# base image
FROM Python
# Set your working directory
WORKDIR /var/www/
# Copy the necessary files
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt
# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
RUN mkdir -p /var/www/new_directory
# Run the app
CMD ["echo", "Hello, Developer"]
The command RUN mkdir -p /var/www/new_directory
allows you to create a directory in your Docker file system called new_directory, which we will eventually build using the image built using the above Docker file.
However, we will first build a Python based Docker image as a base image by running the following command.
isaac@DESKTOP-HV44HT6:~/my-app$ docker build -t new_image .
Output:
docker images
Confirm that we have successfully built the Docker image from the Docker file
using the following command.
$ docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
new_image latest 7ab964c50876 13 minutes ago 932MB
Now that we have a Docker image, we can go ahead and create a Docker container and confirm that a directory called new_directory is created. As shown below, we need to start Bash inside the container to navigate the directory inside the Docker container.
$ docker run -it new_image bash
root@ea42f35d5404:/var/www#
You'll notice that new_directory is created after listing the files in that directory. Apart from that, we can also navigate the directory itself and even create new files in it.
root@ea42f35d5404:/var/www# ls
app.py new_directory requirements.txt
root@ea42f35d5404:/var/www# cd new_directory
root@ea42f35d5404:/var/www/new_directory# ls
root@ea42f35d5404:/var/www/new_directory# touch new_file
root@ea42f35d5404:/var/www/new_directory# ls
new_file
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Docker build command with multiple parameters
Publish Date:2025/03/25 Views:138 Category:Docker
-
docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha
The difference between CMD and ENTRYPOINT in Docker
Publish Date:2025/03/25 Views:141 Category:Docker
-
Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container
Tagging images with Docker and Docker Compose
Publish Date:2025/03/25 Views:70 Category:Docker
-
When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima