使用 Dockerfile 在容器中使用 Mkdir 命令创建目录
Docker 容器已经成为在不同环境中管理软件和依赖项的实际方式。 在处理实际应用程序时,毫无疑问,您需要先创建一个 Dockerfile,然后才能构建您的应用程序容器镜像。
使用 Dockerfile 在容器中使用 mkdir 命令创建目录
除了仅允许开发人员组合创建 Docker 映像所需的命令外,我们还可以使用 Dockerfile 做更多的事情。 要使用 Dockerfile 成功创建 Docker 镜像,您必须了解一些基本命令。
一些最常用的命令包括:
- FROM - 创建将要使用的父镜像/基础镜像层。
- WORKDIR - 允许我们设置工作目录。
- COPY - 允许我们将当前目录内容复制到容器中的目录中。
- PULL - 从您的 Docker 存储库添加文件。
- RUN - 当我们想要构建镜像时要执行的命令。
- CMD - 指定容器启动时要运行的命令。
- ENV - 定义构建期间使用的环境变量。
- ENTRYPOINT - 指定容器启动时要运行的命令。
- MAINTAINER - 指定镜像的作者。
使用上面的命令,我们可以创建一个 Dockerfile,如下所示,它使用 Python 作为基础镜像。
代码:
# 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"]
这个 Dockerfile 是一个简单的 Flask 应用程序的一部分,它只在控制台上打印一条消息。 如果您有兴趣创建相同的应用程序,这里是 requirements.txt 文件。
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
包含该主应用程序文件的主文件 app.py 如下所示。
代码:
from flask import Flask
app = Flask(__name__)
def hello():
print("Hello, this is a simple Flask application")
hello()
现在,要在我们的 Docker 容器的文件系统中使用 mkdir 命令创建一个目录,我们将使用如下所示的 RUN 命令。
# 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"]
命令 RUN mkdir -p /var/www/new_directory
允许您在 Docker 文件系统中创建一个名为 new_directory 的目录,我们最终将使用使用上述 Docker 文件构建的映像构建该目录。
但是,我们将首先通过运行以下命令构建一个基于 Python 的 Docker 镜像作为基础镜像。
isaac@DESKTOP-HV44HT6:~/my-app$ docker build -t new_image .
输出结果:
确认我们已经使用下面的 docker images
命令从 Docker 文件成功构建了 Docker 镜像。
$ docker images
输出结果:
REPOSITORY TAG IMAGE ID CREATED SIZE
new_image latest 7ab964c50876 13 minutes ago 932MB
现在我们有了一个 Docker 镜像,我们可以继续创建一个 Docker 容器并确认是否创建了名为 new_directory 的目录。 如下所示,我们需要在容器内启动 Bash 以在 Docker 容器内导航目录。
$ docker run -it new_image bash
root@ea42f35d5404:/var/www#
您会注意到 new_directory 是在列出该目录中的文件后创建的。 除此之外,我们还可以导航目录本身,甚至可以在其中创建新文件。
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
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类: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
发布时间:2025/03/26 浏览次数:107 分类: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
发布时间:2025/03/26 浏览次数:97 分类: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
发布时间:2025/03/26 浏览次数:124 分类: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
发布时间:2025/03/26 浏览次数:202 分类: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
发布时间:2025/03/26 浏览次数:87 分类: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
发布时间:2025/03/26 浏览次数:140 分类: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