JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Use the Dockerfile to create a directory in the container using the Mkdir command

Author:JIYIK Last Updated:2025/03/25 Views:

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_directoryallows 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 build new image

docker imagesConfirm 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.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial