1. 容器进入命令
命令1
进入容器后开启一个新的终端,可以在里面操作。
docker exec -it 容器id /bin/bash
说明:/bin/bash只是一个shell命令路径,也可以是其他路径。
例如:
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd725007eb2a centos "/bin/bash" 2 minutes ago Up 2 minutes musing_yalow
[root@192 ~]# docker exec -it dd725007eb2a /bin/bash
[root@dd725007eb2a /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@dd725007eb2a /]#
命令2
进入容器正在执行的终端,不会启动新的进程。
docker attach 容器id
例如:
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68c3dd97898c centos "/bin/bash" 10 seconds ago Up 9 seconds blissful_carson
[root@192 ~]# docker attach 68c3dd97898c
[root@68c3dd97898c /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@68c3dd97898c /]#
2. 从容器拷贝文件至主机
docker cp 容器id:容器内的路径 目的主机路径
参数选项:
a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
例如:
[root@192 ~]# docker run -it centos --------------------运行centos容器
[root@c3055b70e929 /]# [root@192 ~]#
[root@192 ~]# docker ps ----------------------进程查看
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3055b70e929 centos "/bin/bash" 10 seconds ago Up 9 seconds silly_lewin
[root@192 ~]# docker attach c3055b70e929
[root@c3055b70e929 /]# cd /home/
[root@c3055b70e929 home]# ls
[root@c3055b70e929 home]# touch lucas.txt --------------------------新建文件
[root@c3055b70e929 home]# ls
lucas.txt
[root@c3055b70e929 home]# exit
exit
[root@192 ~]# docker ps -a -----------------------查看所有进程
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3055b70e929 centos "/bin/bash" 45 seconds ago Exited (0) 3 seconds ago silly_lewin
cec304582db7 centos "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago suspicious_bose
68c3dd97898c centos "/bin/bash" 12 minutes ago Exited (127) 7 minutes ago blissful_carson
dd725007eb2a centos "/bin/bash" 17 minutes ago Exited (1) 12 minutes ago musing_yalow
52d79c210793 centos "/bin/bash" 18 minutes ago Exited (0) 17 minutes ago reverent_elbakyan
9db0b4100aeb centos "/bin/bash" 18 minutes ago Exited (0) 18 minutes ago jovial_einstein
b39af6be1dbe tomcat "catalina.sh run" 37 minutes ago Exited (143) 22 minutes ago affectionate_jennings
f43c73e7e059 tomcat "catalina.sh run" 38 minutes ago Exited (143) 37 minutes ago romantic_stonebraker
975246a7106a tomcat "catalina.sh run" 39 minutes ago Exited (143) 37 minutes ago heuristic_haslett
09e351b4b155 tomcat "catalina.sh run" 42 minutes ago Exited (143) 41 minutes ago sweet_hoover
7996d8a2b9cd centos "/bin/bash" 43 minutes ago Exited (0) 43 minutes ago compassionate_mayer
74be551e2ae0 centos "/bin/bash" 44 minutes ago Exited (0) 44 minutes ago festive_elion
6f2224a1fa39 nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 22 hours ago nginx1
3fadac1354b0 centos "/bin/bash" 2 days ago Exited (137) 2 days ago eloquent_tharp
[root@192 ~]# docker cp c3055b70e929:/home/lucas.txt /home/ ------------------文件拷贝至主机
[root@192 ~]# cd /home/
[root@192 home]# ls
admin c++ c++11 coredump ftp_gzd gcc-build-10.2.0 gdb jack linux logadmin lucas lucas.txt muduo package practice_gzd shared shell src_code storage svn
[root@192 home]#