Docker exec and Docker attach

Statement:
This blog is welcome to forward, but please keep the original author's information!
Blog address: http://blog.csdn.net/halcyonbaby 
Sina Weibo: @Looking for Miracles

The content is my study, research and summary. If there are similarities, it is my honor!

==================

Docker exec and Docker attach

Whether a developer or an operator, there is often a need to enter the container.  
At present, the main methods are as follows:
1. Landing in containers using ssh
2. Use third-party tools such as nsenter, nsinit, etc.
3. Use the tools provided by docker itself

Method 1 needs to start sshd in the container, which has the problem of increasing overhead and attack surface. It also violates Docker's advocacy.
The principle of a container and a process.  
Method 2 requires additional learning to use third-party tools.  
So in most cases it's better to use the Docker native method, which currently provides Docker exec and
Docker attach commands.

The following is validated on Fedora 21, docker 1.7.

Docker attach

Docker attach can attach to stdin of a running container and then perform command execution actions.  
However, it should be noted that exit from this stdin will cause the container to stop.

[root@localhost temp]# docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED              STATUS              PORTS               NAMES
2327e7eab0ed        busybox:buildroot-2014.02   "/bin/sh"           About a minute ago   Up About a minute                       bb2
[root@localhost temp]# docker attach bb2
/ # ls
bin      dev      etc      home     lib      lib64    linuxrc  media    mnt      opt      proc     root     run      sbin     sys      tmp      usr      var
/ # pwd
/
/ #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Docker exec

On-i and-t parameters

It can be seen that when - i is used only, it looks like pipe execution because there is no pseudo terminal allocated. But executing results, orders
The return values can be obtained correctly.

[root@localhost temp]# docker exec -i bb2 /bin/sh
date
Tue Jul 14 04:01:11 UTC 2015
echo $?
0
dir
/bin/sh: dir: not found
echo $?
127
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

When using - it, it is similar to our usual console interface. And it's not like attach ing because it exits.
The whole container exits.  
This method can replace ssh or nsenter or nsinit mode to operate in the container.

[root@localhost temp]# docker exec -it bb2 /bin/sh
/ # pwd
/
/ # echo $?
0
/ # dir
/bin/sh: dir: not found
/ # echo $?
127
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

If you only use the - t parameter, you can see a console window, but executing the command will find that stdin is not available
The output of the command cannot be seen.

[root@localhost temp]# docker exec -t bb2 /bin/sh
/ # pwd

hanging....
[root@localhost temp]# docker exec -t bb2 pwd
/
[root@localhost temp]# echo $?
0
[root@localhost temp]# docker exec -t bb2 dir
2015/07/14 04:03:57 docker-exec: failed to exec: exec: "dir": executable file not found in $PATH
[root@localhost temp]# echo $?
0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

After the docker exec executes, the command executes the return value. (Note Docker 1.3 seems to have a Bug that does not correctly return command execution results)

[root@localhost temp]# docker exec -it bb cat /a.sh
echo "running a.sh"
exit 10
[root@localhost temp]# docker exec -t bb /a.sh
running a.sh
[root@localhost temp]# echo $?
10
[root@localhost temp]# docker exec -it bb /a.sh
running a.sh
[root@localhost temp]# echo $?
10
[root@localhost temp]# docker exec -i bb /a.sh
running a.sh
[root@localhost temp]# echo $?
10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
On-d parameter

Execute a process in the background. As you can see, if a command takes a long time to process, using the - d parameter returns quickly.  
The program runs in the background.

[root@localhost temp]# docker exec -d bb2 /a.sh
[root@localhost temp]# echo $?
0
  • 1
  • 2
  • 3
  • 4
  • 5

If the - d parameter is not used, docker exec will get stuck because the command takes a long time to execute and wait until the command is executed.
Before returning.

[root@localhost temp]# docker exec  bb2 /a.sh
^C[root@localhost temp]#
[root@localhost temp]#
[root@localhost temp]# docker exec -it  bb2 /a.sh
^C[root@localhost temp]#
[root@localhost temp]# docker exec -i  bb2 /a.sh
^C[root@localhost temp]# docker exec -t  bb2 /a.sh
^C[root@localhost temp]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Copyright Statement: This article is the original article of the blogger. It can not be reproduced without the permission of the blogger. http://blog.csdn.net/halcyonbaby/article/details/46884605

Keywords: Docker Fedora ssh

Added by dhie on Sun, 19 May 2019 18:26:17 +0300