迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

在 Docker 中更改工作目录命令

作者:迹忆客 最近更新:2023/04/16 浏览次数:

在 Docker 中,如果我们没有创建和启动容器并且没有在容器的 shell 中执行,我们就无法无缝地运行终端命令。 这些命令的一个示例是更改工作目录。

如果我们需要在特定目录中安装组件,则更改工作目录是必不可少的。 本文将讨论在 Docker 中更改工作目录的方法。


在 Docker 中更改工作目录

我们将处理的以下命令不是主要命令,而是我们在准备 Dockerfile 时将使用的条目。 如果我们还记得的话,我们可以在运行 docker build 命令时使用 Dockerfile 自动创建容器。

在下一节中,我们将讨论两种通过 Dockerfile 更改工作目录的方法。

使用 RUN 命令

我们可以使用 RUN 命令运行终端脚本。 然后,在我们的 Dockerfile 中,我们可以插入如下所示的条目来更改我们的工作目录。

RUN cd /dev

在运行其他命令之前,我们可以使用 cd 命令更改工作目录。 但是,我们不需要向 Dockerfile 添加新的 RUN 条目来运行多个命令。

我们可以只用一个 RUN 行来使用和附加各种命令。

示例代码:

RUN cd /dev && git clone sample.git && pip install -r requirements.txt

我们可以使用 AND (&&) 运算符运行多个命令。 例如,以下命令只有在其后面的前一个命令成功时才会运行。

在这种情况下,如果 git clone 命令失败,我们的 pip install 命令将不会执行。 但是,如果我们更改容器内的工作目录,则有一种更直接的方法。

使用 WORKDIR 命令

与 RUN 命令一样,我们可以在 Dockerfile 中添加 WORKDIR 命令,以便在容器启动并运行后更改工作目录。

WORKDIR "/dev"

在为 Docker 编写构建文件时,WORKDIR 命令被认为是 Dockerfile 的最佳实践。 发生这种情况是因为上述命令更容易阅读和区分容器上执行的命令。

WORKDIR "/dev"
RUN git clone sample.git && pip install -r requirements.txt

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便