Interactive Shell Using Docker Compose – 使用Docker Compose的交互式外壳

最后修改: 2022年 8月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll learn how to run multiple Docker containers with an interactive shell. First, we’ll run a Docker container using the simple docker run command. Later, we’ll run the same Docker container with the docker-compose command.

在本教程中,我们将学习如何使用交互式外壳运行多个Docker容器。首先,我们将使用简单的docker run命令运行一个Docker容器。稍后,我们将用docker-compose命令运行同一个Docker容器。

2. Docker and Docker Compose

2.Docker和Docker Compose

Docker containers allow developers to package applications that work seamlessly across different environments. In fact, a typical deployment of a web application in a production environment may require several services:

Docker容器允许开发者将应用程序打包,以便在不同的环境中无缝工作。事实上,在生产环境中,一个典型的网络应用部署可能需要几个服务。

  • database server
  • load balancing
  • webserver

In such situations, Docker Compose is a very handy tool.

在这种情况下,Docker Compose是一个非常方便的工具。

Docker Compose is mainly used to run multiple containers as a single service while maintaining a smooth connection between the containers.

Docker Compose主要用于将多个容器作为单一服务运行,同时保持容器之间的顺利连接。

3. Understanding Docker Compose

3.了解Docker Compose

To run a Docker container using the docker-compose command, we need to add all the configurations to the single docker-compose.yml configuration file. Importantly, one of the key benefits of using docker-compose over the normal docker run command is the configuration consolidation in a single file, which both machines and humans can read.

要使用docker-compose命令运行Docker容器,我们需要将所有配置添加到单个docker-compose.yml配置文件中。重要的是,与普通的docker run命令相比,使用docker-compose的主要好处之一是将配置整合在一个文件中,机器和人类都可以阅读。

Let’s create a simple docker-compose.yml to show how to run Docker containers using the docker-compose up command:

让我们创建一个简单的docker-compose.yml来展示如何使用docker-compose up命令来运行Docker容器。

version: "3"
services:
 server:
   image: tomcat:jre11-openjdk
   ports:
     - 8080:8080

Here, we used tomcat as the base image and exposed port 8080 on the host machine. To see it in action, let’s build and run this image using the docker-compose up command:

在这里,我们使用tomcat作为基础镜像,并在主机上暴露端口8080。为了看到它的运作,让我们使用docker-compose up命令构建并运行这个镜像。

$ docker-compose up
Pulling server (tomcat:jre11-openjdk)...
jre11-openjdk: Pulling from library/tomcat
001c52e26ad5: Pull complete
...
704b1ae41f0e: Pull complete
Digest: sha256:85bfe38b723bc864ed594973a63c04b112e20d6d33eee57cd5303610d8e3dc77
Status: Downloaded newer image for tomcat:jre11-openjdk
Creating dockercontainers_server_1 ... done
Attaching to dockercontainers_server_1
server_1  | NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
server_1  | 03-Aug-2022 06:22:17.259 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/10.0.23

Critically, we should run the above command from the directory containing the docker-compose.yml file.

关键是,我们应该从包含docker-compose.yml文件的目录中运行上述命令。

In the above output, we can see that the dockercontainers_server_1 is up and running. However, an issue with this approach is that once we exit the above shell, the container will also stop.

在上面的输出中,我们可以看到dockercontainers_server_1已经启动并运行。然而,这种方法的一个问题是,一旦我们退出上述shell,容器也将停止

To run the Docker container for the long run, we need to run it with an interactive shell.

为了长期运行Docker容器,我们需要用交互式外壳来运行它。

4. Interactive Shell in Docker

4.Docker中的交互式外壳

The interactive mode in Docker allows us to execute commands while the container is in a running state. To run the Docker container in interactive mode, we use the -it option. Further, we attach both the STDIN and STDOUT channels to our terminal with the -it flags.

Docker中的交互模式允许我们在容器处于运行状态时执行命令。为了在交互模式下运行Docker容器,我们使用-it选项。此外,我们用-it标志将STDIN和STDOUT通道附加到我们的终端。

Docker Compose uses a single-host deployment that has multiple benefits:

Docker Compose采用单主机部署,有多种好处。

  • quick and easy to configure
  • enables rapid deployment
  • reduces the amount of time needed to complete multiple tasks
  • all containers run independently, which reduces the risk of a breach

Let’s now run the tomcat container from earlier using docker-compose with an interactive shell:

现在让我们使用docker-compose用交互式shell运行先前的tomcat容器。

version: "3"
services:
 server:
   image: tomcat:jre11-openjdk
   ports:
     - 8080:8080
   stdin_open: true 
   tty: true

In this case, we added the stdin_open and tty options in the docker-compose.yml file so that we can have an interactive shell with the docker-compose setup.

在这种情况下,我们adocker-compose.yml文件中添加了stdin_opentty选项,这样我们就可以通过docker-compose设置拥有一个交互式外壳。

Of course, to access the Docker container, we need first to run the container using the below command:

当然,要访问Docker容器,我们首先需要使用下面的命令运行容器。

$ docker-compose up --d

Now, we can get an interactive shell of the running docker-compose service:

现在,我们可以得到一个正在运行的docker-compose服务的交互式外壳:

$ docker-compose exec server bash

Note how we use the service name and not the container name.

注意我们如何使用服务名称而不是容器名称.

Finally, we successfully log into the container with the above command.

最后,我们用上述命令成功登录到容器中。

5. Conclusion

5.总结

In this article, we demonstrated how to get an interactive shell using the docker-compose command. First, we learned how to run a Docker container using docker-compose. After that, we explored the same with an interactive shell using the docker exec command and the docker-compose YAML configuration.

在这篇文章中,我们演示了如何使用docker-compose命令获得一个交互式的外壳。首先,我们学习了如何使用docker-compose运行一个Docker容器。之后,我们通过使用docker exec命令和docker-composeYAML配置,对交互式shell进行了同样的探索。