Docker exec and Docker attach

Source: Internet
Author: User
Tags requires stdin ssh docker ps

..
Statement:
This blog welcome forwarding, but please keep the original author information!
Blog Address: Http://blog.csdn.net/halcyonbaby
Sina Weibo: Searching for Miracles

The content of my study, research and summary, if there is similar, it is honored.

================== Docker exec and Docker attach

Whether the developer is an OPS person, there is often a need to enter the container.
At present, the main methods are as follows:
1. Log in to the container using SSH
2. Use third-party tools such as Nsenter, Nsinit, etc.
3. Using the tools provided by Docker itself

Method 1 requires that the sshd be started in the container, and there is an issue of overhead and attack surface enlargement. It also violates what Docker advocates
A container for the principle of a process.
Method 2 requires additional learning to use third-party tools.
So most of the time it's best to use the Docker native method, which currently provides Docker exec and
Docker attach two commands.

The following are verified on fedora21,docker1.7. Docker Attach

Docker attach can be attach to a stdin of a container that is already running, and then perform the action of the command execution.
However, it is important to note that if you exit from this stdin, it 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
/
/#
Docker exec about-I,-t parameters

It can be seen that only with-I, because there is no pseudo-terminal allocation, looks like pipe execution. But execution results, commands
The return value can be obtained correctly.

[Root@localhost temp]# Docker exec-i bb2/bin/sh
date
Tue Jul 04:01:11 UTC]
echo $?
0
dir
/bin/sh:dir:not found
echo $?
127

When using-it, it is similar to what we normally do with the console interface. And it wouldn't be like attach way because of the exit that led
The entire container exits.
This method can be used instead of SSH or nsenter, Nsinit mode, in the container to operate.

[Root@localhost temp]# Docker exec-it bb2/bin/sh
/# pwd
/
/# echo $?
0
/# dir
/bin/sh:dir:not found
/# echo $?
127

If you only use the-t parameter, you can see a console window, but the execution command will find that the stdin
Output, you cannot see the execution of the command.

[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": Ex ecutable file not found in $PATH
[Root@localhost temp]# echo $?
0

After Docker exec executes, the return value is executed by the command. (note Docker1.3 appears to have a bug that does not return the command execution result correctly)

[root@localhost temp]# Docker exec-it BB cat/a.sh
echo "Running a.sh"
exit
[Root@localhost temp]# Docker] EXEC-T bb/a.sh
running a.sh
[Root@localhost temp]# echo $?
[root@localhost temp]# Docker exec-it bb/a.sh
running a.sh
[Root@localhost temp]# echo $?
]
[root@localhost temp]# Docker exec-i bb/a.sh
running a.sh
[Root@localhost temp]# echo $?
] 10
about-D parameters

Executes a process in the background. As you can see, if a command takes a long time process, using the-D parameter will return quickly.
The program runs in the background.

[Root@localhost temp]# Docker exec-d bb2/a.sh
[root@localhost temp]# echo $?
] 0

If you do not use the-D parameter, Docker exec will get stuck and wait until the command is completed because the command takes a long time to execute
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]#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.