在 Docker 中设置工作目录
如果存在,则计算中进程的工作目录是链接的分层文件系统中的一个目录,该文件系统对每个进程都是动态的。 在 Docker 中,我们可以通过编辑 Dockerfile 并添加密钥 WORKDIR 来设置我们的工作目录。
本文将讨论在 Docker 中更改我们当前和默认的工作目录。
Docker 中的 WORKDIR
Dockerfile 中的 WORKDIR 命令确定其后的任何 RUN、CMD、ENTRYPOINT、COPY 和 ADD 指令的工作目录。 我们可以在 Dockerfile 中多次使用 WORKDIR 指令。
如果我们提供相对路径,它将相对于先前 WORKDIR 指令的路径。
例如:
WORKDIR /usr
WORKDIR src/serv
RUN sampleserv
此 Dockerfile 中最终 sampleserv 命令的输出将是 /usr/src/serv。
WORKDIR 指令可以解析先前使用 ENV 指令指定的环境变量。 但是,我们只能使用在 Dockerfile 中显式设置的环境变量。
ENV DIRPATH=/usr
WORKDIR $DIRPATH/$DIRNAME
RUN sampleserv
此 Dockerfile 中的最终 sampleserv 命令的输出将是 /usr/$DIRNAME。
默认工作目录是根目录或 /
如果未指定。 实际上,如果您不是从头开始创建 Dockerfile (FROM scratch
),您使用的基础镜像可能已经配置了 WORKDIR。
因此,我们建议明确设置您的 WORKDIR 以防止未知文件夹中的意外活动。
为了清晰和可靠,我们应该始终为您的 WORKDIR 使用绝对路径。 另外,我们应该使用 WORKDIR 而不是增加像 RUN cd … && sample-code
这样难以阅读、故障排除和维护的指令。
相关文章
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