Docker exec and Docker attach

Source: Internet
Author: User

Reprint 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.

[[email protected] temp]# docker psCONTAINER ID        IMAGE                       COMMAND             CREATED              STATUS              PORTS               NAMES2327e7eab0ed        busybox:buildroot-2014.02   "/bin/sh"           About a minute ago   Up About a minute                       bb2[[email protected] temp]# docker attach bb2/ # lsbin      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.

[[email protected] temp]# docker exec -i bb2 /bin/shdateTue Jul 14 04:01:11 UTC 2015echo $?0dir/bin/sh: dir: not foundecho $?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.

[[email protected] 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.

[[email protected] temp]# docker exec -t bb2 /bin/sh/ # pwdhanging....[[email protected] temp]# docker exec -t bb2 pwd/[[email protected] temp]# echo $?0[[email protected]alhost temp]# docker exec -t bb2 dir2015/07/14 04:03:57 docker-exec: failed to exec: exec: "dir": executable file not found in $PATH[[email protected] 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)

[[email protected] temp]# docker exec -it bb cat /a.shecho "running a.sh"exit 10[[email protected] temp]# docker exec -t bb /a.shrunning a.sh[[email protected] temp]# echo $?10[[email protected] temp]# docker exec -it bb /a.shrunning a.sh[[email protected] temp]# echo $?10[[email protected] temp]# docker exec -i bb /a.shrunning a.sh[[email protected] 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.

[[email protected] temp]# docker exec -d bb2 /a.sh[[email protected] 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.

[[email protected] temp]# docker exec  bb2 /a.sh^C[[email protected] temp]#[[email protected] temp]#[[email protected] temp]# docker exec -it  bb2 /a.sh^C[[email protected] temp]#[[email protected] temp]# docker exec -i  bb2 /a.sh^C[[email protected] temp]# docker exec -t  bb2 /a.sh^C[[email protected] temp]#

Docker exec and Docker attach

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.