当前位置:网站首页>Container (4) method of entering container [18]

Container (4) method of entering container [18]

2020-11-10 18:59:49 cuiyongchao007

( Two ) How to get into the container

​ We often need to go into the container to do some work , For example, check the log 、 debugging 、 Start other processes, etc . There are two ways to get into the container :attach and exec.

(1)docker attach

adopt  docker attach  Sure attach To the terminal of the container start command , for example :

root@cuiyongchao:/dockerfile# docker run -d ubuntu /bin/bash -c "while true;do sleep 1;echo this is ubuntu.;done"
d3d17f299cfc5de981ab67f34bcfa9110581fb961b49b67d7e646880cb4520c7
root@cuiyongchao:/dockerfile# docker attach d3d17f299cfc5de981ab67f34bcfa9110581fb961b49b67d7e646880cb4520c7
this is ubuntu.
this is ubuntu.

This time we passed “ Long ID” attach To the container's start command terminal , What we see after that is echo Information printed every second .

notes : It can be done by Ctrl+p then Ctrl+q Key combination exit attach terminal .

(2)docker exec

adopt  docker exec  Into the same container :

root@cuiyongchao:/dockerfile# docker run -d ubuntu /bin/bash -c "while true;do sleep 1;echo this is ubuntu.;done"
872ebb8a83c1ad210b8dc383af02c31480bb8bca70aacad1f1b971573cb290c0

root@cuiyongchao:/dockerfile# docker exec -it  872ebb8a83c1ad210b8dc383af02c31480bb8bca70aacad1f1b971573cb290c0 /bin/bash   ①
root@872ebb8a83c1:/# ②
root@872ebb8a83c1:/# ps -ef ③
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 02:42 ?        00:00:00 /bin/bash -c while true;do sleep 1;echo this is ubuntu.;done
root         53      0  0 02:43 pts/0    00:00:00 /bin/bash
root       2314      1  0 03:21 ?        00:00:00 sleep 1
root       2315     53  0 03:21 pts/0    00:00:00 ps -ef
root@872ebb8a83c1:/# exit ④
exit
root@cuiyongchao:/dockerfile# 

The explanation is as follows :

-it Open in interactive mode pseudo-TTY, perform bash, The result is to open a bash terminal .

② Enter into container , Container of hostname Is it “ short ID”.

③ It can be like in ordinary Linux Carry out the same command as in .ps -elf Shows the container startup process while And the current bash process .

④ perform exit Exit the container , go back to docker host.docker exec -it <container> bash|sh Is to perform exec The most common way .

(3)attach VS exec

​ attach And exec The main differences are as follows :

  1. attach Direct access to container Start command Terminal , No new process will be started .
  2. exec Open a new terminal in the container , And you can start a new process .
  3. If you want to view the output of the startup command directly in the terminal , use attach; Use in other situations exec.

Of course , If it's just to see the output of the start command , have access to  docker logs  command :

root@cuiyongchao:/dockerfile# docker logs -f d3d17f299cfc5de981ab67f34bcfa9110581fb961b49b67d7e646880cb4520c7
this is ubuntu.
this is ubuntu.
this is ubuntu.

-f  The role of  tail -f  similar , Able to continuously print out .

版权声明
本文为[cuiyongchao007]所创,转载请带上原文链接,感谢