How to Change Directory in Docker – 如何在Docker中改变目录

最后修改: 2022年 9月 3日

1. Overview

1.概述

Docker images comprise a set of sequential instructions that serve as a template to build a container. In this tutorial, we’ll learn how to change the directory when building a Docker image or when running a container using an image.

Docker镜像由一组连续的指令组成,作为构建容器的模板。在本教程中,我们将学习如何在构建Docker镜像或使用镜像运行容器时改变目录

2. Using the WORKDIR Instruction

2.使用WORKDIR指令

First, let’s start by spawning a Docker container using the readily available ubuntu:latest image:

首先,让我们从使用现成的ubuntu:最新image来催生一个Docker容器。

$ docker run -it ubuntu:latest
root@89848b34daa6:/# pwd
/

We can see that as soon as the container is up, the current directory is set as /.

我们可以看到,一旦容器启动,当前目录就被设置为/.

Next, let’s say we want to change this directory to /tmp at container startup. We can do this by using the WORKDIR instruction inside a custom image that uses the ubuntu:latest as the base image:

接下来,让我们假设我们想在容器启动时将这个目录改为/tmp。我们可以通过使用WORKDIR指令在一个使用ubuntu:fresh作为基础镜像的自定义镜像中做到这一点。

$ cat custom-ubuntu-v1.dockerfile
FROM ubuntu:latest
WORKDIR /tmp

Before we can run a container using this image, we’ll need to build the image. So, let’s go ahead and build the custom-ubuntu:v1 image:

在我们能够使用这个镜像运行容器之前,我们需要构建这个镜像。所以,让我们继续构建custom-ubuntu:v1镜像。

$ docker build -t custom-ubuntu:v1 - < ./custom-ubuntu-v1.dockerfile

Finally, let’s run a container using the custom-ubuntu:v1 image and verify the current directory:

最后,让我们使用custom-ubuntu:v1镜像运行一个容器并验证当前目录。

$ docker run -it custom-ubuntu:v1
root@4c26093b26e6:/tmp# pwd
/tmp

It looks like we’ve got this right!

看起来我们已经得到了这个权利!

3. Using the –workdir Option

3.使用-workdir选项

Using the WORKDIR instruction is the recommended practice for most cases where we want to change the directory when building the Docker image. Nevertheless, if our use case is limited to changing the directory when running the container, then we can achieve this by using the –workdir option:

使用WORKDIR指令是大多数情况下的推荐做法,我们要在构建Docker镜像时改变目录。尽管如此,如果我们的用例仅限于在运行容器时改变目录,那么我们可以通过使用-workdir选项来实现这一点

$ docker run --workdir /tmp -it ubuntu:latest
root@32c5533c248c:/tmp# pwd
/tmp

Looking at this, we can appreciate the command’s brevity and the fact that we didn’t have to create a custom image in this case.

看看这个,我们可以欣赏这个命令的简洁性,以及在这种情况下我们不必创建一个自定义图像的事实。

4. Using the cd Command

4.使用cd命令

In Linux, the cd command is the standard way to change the directory for most use cases. On the same note, when working with some docker instructions such as RUN, CMD, and ENTRYPOINT, we can use the cd command to change the directory for the current command in context.

在Linux中,cd命令是大多数使用情况下更改目录的标准方法。同样,在使用一些docker指令时,如RUNCMDENTRYPOINT,我们可以使用cd命令来改变上下文中当前命令的目录。

Let’s start by writing the custom-ubuntu-v2.dockerfile to use the RUN  instruction with the cd command:

让我们开始编写custom-ubuntu-v2.dockerfile,以使用RUN指令和cd指令。

FROM ubuntu:latest
RUN cd /tmp && echo "sample text" > data.txt

We can see that the intention is to write “sample text” to the /tmp/data.txt file.

我们可以看到,其意图是将 “样本文本 “写入/tmp/data.txt文件。

Next, let’s add the ENTRYPOINT instruction to run bash as the default command on container startup. Additionally, we use the cd command to change the current directory to the /tmp directory:

接下来,让我们添加ENTRYPOINT指令来运行bash作为容器启动时的默认命令。此外,我们使用cd指令来改变当前目录为/tmp目录。

ENTRYPOINT ["sh", "-c", "cd /tmp && bash"]

Moving on, let’s build the custom image:

继续,让我们建立自定义图像。

$ docker build -t custom-ubuntu:v2 - < ./custom-ubuntu-v2.dockerfile

Finally, let’s run the container using the custom-ubuntu:v2 image and verify the execution of the commands:

最后,让我们使用custom-ubuntu:v2镜像运行容器,并验证命令的执行。

$ docker run -it custom-ubuntu:v2
root@2731e50ea20a:/tmp# pwd
/tmp
root@2731e50ea20a:/tmp# cat /tmp/data.txt
random text

We can see that the results of both the change directory commands are as expected. Additionally, we must remember that WORKDIR remains the recommended way. Still, for simple use cases, we can use the cd command in conjunction with the RUN, ENTRYPOINT, or the CMD instructions.

我们可以看到,两个改变目录的命令的结果都和预期一样。此外,我们必须记住,WORKDIR仍然是推荐的方式。尽管如此,对于简单的使用情况,我们可以将cd命令与RUNENTRYPOINTCMD指令结合使用

5. Conclusion

5.总结

In this article, we learned different approaches to changing the directory when working with Docker images or starting the container.

在这篇文章中,我们学习了在使用Docker镜像或启动容器时改变目录的不同方法