1. Overview
1.概述
While working with a docker container, we often need to run it in an interactive mode. This is where we attach the standard input, output, or error streams of our terminal to the container.
在使用docker容器的过程中,我们经常需要在交互式模式下运行它。这就是我们将终端的标准输入、输出或错误流附加到容器上的地方。
Often we prefer to run our container in the background. However, we may wish to connect to it later to check its output or errors or disconnect the session.
通常我们喜欢在后台运行我们的容器。然而,我们可能希望稍后连接到它,以检查其输出或错误或断开会话。
In this short article, we’ll learn some useful commands to achieve these. We’ll also see different ways to detach from a session without stopping the container.
在这篇短文中,我们将学习一些有用的命令来实现这些。我们还将看到在不停止容器的情况下从会话中分离的不同方法。
2. Run a Container in Attached/Detached Mode
2.在附加/分离模式下运行一个容器
Let’s see how to run a container in attached or detached mode.
让我们看看如何在附加或分离模式下运行一个容器。
2.1. Default Mode
2.1 默认模式
By default, Docker runs a container in the foreground:
默认情况下,Docker在前台运行一个容器。
$ docker run --name test_redis -p 6379:6379 redis
This means we can’t return to our shell prompt until the process finishes.
这意味着我们不能回到我们的shell提示符,直到该进程结束。
The above command links the standard output (stdout), and the standard error (stderr) streams with our terminal. So, we can see the console output of the container in our terminal.
上述命令将标准输出(stdout)和标准错误(stderr)流与我们的终端连接起来。因此,我们可以在终端看到容器的控制台输出。
The –name option gives the container a name. We can later use the same name to refer to this container in other commands. Alternatively, we can refer to it by the container id which we get executing the docker ps command.
-name选项给了容器一个名字。我们以后可以在其他命令中使用相同的名字来引用这个容器。另外,我们也可以通过执行docker ps命令得到的容器ID来引用它。
We may also use the -a option to choose specific streams from stdin, stdout, and stderr to connect with:
我们还可以使用-a选项,从stdin、stdout和stderr中选择特定的流来连接。
$ docker run --name test_redis -a STDERR -p 6379:6379 redis
The above command means we see only the error messages from the container.
上述命令意味着我们只看到来自容器的错误信息。
2.2. Interactive Mode
2.2.互动模式
We initiate a container in the interactive mode with -i and -t options together:
我们用-i和-t选项一起启动一个互动模式下的容器。
$ docker run -it ubuntu /bin/bash
Here, the -i option attaches the standard input stream (stdin) of the bash shell in the container and the -t option allocates a pseudo-terminal to the process. This lets us interact with the container from our terminal.
这里,-i选项将标准输入流(stdin)的bash shell附在容器中,-t选项给进程分配一个伪终端。这让我们可以从我们的终端与容器进行交互。。
2.3. Detached Mode
2.3.分离模式
We run a container in detached mode with the -d option:
我们用-d选项以分离模式运行一个容器。
$ docker run -d --name test_redis -p 6379:6379 redis
This command starts the container, prints its id, and then returns to the shell prompt. Thus, we can continue with other tasks while the container continues to run in the background.
这个命令启动了容器,打印了它的id,然后返回到shell提示符。因此,我们可以继续执行其他任务,而容器继续在后台运行。
We can connect to this container later using either its name or container id.
我们以后可以用它的名字或容器ID连接到这个容器。
3. Interact With a Running Container
3.与运行中的容器互动
3.1. Execute a Command
3.1.执行一个命令
The execute command lets us execute commands inside a container that is already running:
execute命令让我们在已经运行的容器内执行命令。
$ docker exec -it test_redis redis-cli
This command opens a redis-cli session in the Redis container named test_redis which is already running. We can also use the container id instead of the name. The option -it, as explained in section 2.2, enables the interactive mode.
这个命令在已经运行的名为test_redis的Redis容器中打开一个redis-cli会话。我们也可以用容器的id来代替名字。选项-it,如第2.2节所解释的,启用交互式模式。
However, we may only want to get the value against a key:
然而,我们可能只想获得针对某个键的值。
$ docker exec test_redis redis-cli get mykey
This executes the get command in the redis-cli, returns the value for the key mykey, and closes the session.
这将执行get命令,redis-cli,返回键mykey的值,并关闭会话。
It is also possible to execute a command in the background:
也可以在后台执行一个命令。
$ docker exec -d test_redis redis-cli set anotherkey 100
Here, we use -d for this purpose. It sets the value 100 against the key anotherKey, but doesn’t display the output of the command.
在这里,我们使用-d来达到这个目的。它针对键anotherKey设置值100,但不显示命令的输出。
3.2. Attaching a Session
3.2.附加一个会话
The attach command connects our terminal to a running container:
attach命令将我们的终端与正在运行的容器连接起来。
$ docker attach test_redis
By default, the command binds the standard input, output, or error streams with the host shell.
默认情况下,该命令将标准输入、输出或错误流与主机外壳绑定。
To see only the output and error messages, we may omit stdin using the –no-stdin option:
要想只看到输出和错误信息,我们可以使用-no-stdin选项省略stdin。
$ docker attach --no-stdin test_redis
4. Detach From a Container
4.从一个容器中分离出来
The way to detach from a docker container depends on its running mode.
脱离docker容器的方式取决于其运行模式。
4.1. Default Mode
4.1 默认模式
Pressing CTRL-c is the usual way of ending a session. But, if we’ve launched our container without the -d or -it option, the CTRL-c command stops the container instead of disconnecting from it. The session propagates the CTRL-c i.e., SIGINT signal to the container and kills its main process.
按CTRL-c是结束会话的通常方式。但是,如果我们在启动容器时没有使用-d或-it选项,CTRL-c命令会停止容器,而不是与之断开连接。会话将CTRL-c即SIGINT信号传播给容器并杀死其主进程。
Let’s override the behavior passing –sig-proxy=false:
让我们通过-sig-proxy=false来重写该行为。
$ docker run --name test_redis --sig-proxy=false -p 6379:6379 redis
Now, we can press CTRL-c to detach only the current session while the container keeps running in the background.
现在,我们可以按CTRL-c只分离当前会话,而容器在后台继续运行。
4.2. Interactive Mode
4.2.互动模式
In this mode, CTRL-c acts as a command to the interactive session and so it doesn’t work as a detach key. Here, we should use CTRL-p CTRL-q to end the session.
在这种模式下,CTRL-c充当了交互式会话的命令,所以它不能作为分离键使用。这里,我们应该使用CTRL-p CTRL-q来结束会话。
4.3. Background Mode
4.3.背景模式
In this case, we need to override the –sig-proxy value while attaching the session:
在这种情况下,我们需要在附加会话时覆盖-sig-proxy的值。
$ docker attach --sig-proxy=false test_redis
We may also define a separate key by –detach-keys option:
我们也可以通过-detach-keys选项定义一个单独的密钥。
$ docker attach --detach-keys="ctrl-x" test_redis
This detaches the container and returns the prompt when we press CTRL-x.
当我们按CTRL-x时,这将分离容器并返回提示。
5. Conclusion
5.总结
In this article, we saw how to launch a docker container in both attached and detached mode.
在这篇文章中,我们看到了如何在连接和分离模式下启动一个docker容器。
Then, we looked at some commands to start or end a session with an active container.
然后,我们看了一些命令,以开始或结束一个活动容器的会话。