先来思考以下问题:

 为什么要进入容器?

首先只能进入正在运行的容器,进入容器后,我们可以查看容器的时间、ip等等

1. 先执行如下命令查找正在运行的容器

 docker container ls

[root@manager Asia]# docker container ls 
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
2e5ba472b34b        nginx:1.13.7-alpine   "nginx -g 'daemon of…"   6 hours ago         Up 6 hours          80/tcp              nginx.6.qtbkmib8ad1yq7r371hcoks2r
abeac496e390        nginx:1.13.7-alpine   "nginx -g 'daemon of…"   6 hours ago         Up 6 hours          80/tcp              nginx.5.mmar6mlyh1nz4hujgthly3nck
20479d2df6da        nginx:1.13.7-alpine   "nginx -g 'daemon of…"   6 hours ago         Up 6 hours          80/tcp              nginx.2.hjv6y7idpb1r5gdz2i0b8amv3
5bb946963610        nginx:1.13.7-alpine   "nginx -g 'daemon of…"   6 hours ago         Up 6 hours          80/tcp              nginx.3.lstevh4rz37ak6mpa2n3l458f
a5fc372d3af4        nginx:1.13.7-alpine   "nginx -g 'daemon of…"   6 hours ago         Up 6 hours          80/tcp              nginx.4.peetyb2zseqvw0vc09syiow8s
a9ed8b9f53ef        nginx:1.13.7-alpine   "nginx -g 'daemon of…"   6 hours ago         Up 6 hours          80/tcp              nginx.1.erz7j241ctm01qblzp6qfoj82
[root@manager Asia]# 

2. 从上面找到自己进入的容器,执行如下命令

docker exec -it <containter_id | containter_name> /bin/bash

如果执行上述命令报如下错误:

[root@manager Asia]# docker exec -it 2e5ba472b34b /bin/bash
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
[root@manager Asia]# 

上述错误说没有此类文件或目录/bin/bash,那么使用下面的命令

docker exec -it <containter_id | containter_name> sh

下面执行命令进入容器,显示下面信息表示成功进入容器


[root@manager Asia]# docker exec -it 2e5ba472b34b sh
/ # date
Fri May 24 17:02:16 GMT 2019
/ # 
/ # ls -lrt
total 8
drwxr-xr-x    1 root     root            30 Dec  1  2017 var
drwxr-xr-x    2 root     root             6 Dec  1  2017 srv
drwxr-xr-x    2 root     root          4096 Dec  1  2017 sbin
drwxr-xr-x    2 root     root             6 Dec  1  2017 run
drwxr-xr-x    2 root     root             6 Dec  1  2017 mnt
drwxr-xr-x    5 root     root            44 Dec  1  2017 media
drwxr-xr-x    2 root     root             6 Dec  1  2017 home
drwxr-xr-x    2 root     root          4096 Dec  1  2017 bin
drwxr-xr-x    1 root     root            77 Dec  1  2017 usr
drwxrwxrwt    1 root     root             6 Dec  1  2017 tmp
drwxr-xr-x    1 root     root            66 Dec  1  2017 lib
dr-xr-xr-x   13 root     root             0 May 24 10:18 sys
dr-xr-xr-x  131 root     root             0 May 24 10:21 proc
drwxr-xr-x    5 root     root           340 May 24 10:21 dev
drwx------    1 root     root            26 May 24 15:12 root
drwxr-xr-x    1 root     root            23 May 24 15:18 etc
/ # 

要退出的话执行命令:exit

 

其他参考:https://blog.csdn.net/boling_cavalry/article/details/80215214

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐