【原创】Docker 进入容器内的 Linux 的交互命令行

概要

Docker 镜像都是基于各种版本的 Linux 的基础镜像构建的。那么我们如何从宿主机进入已经在运行的 docker 容器的 Linux 的命令行呢;我们又如何能进入容器的 Linux 交互命令行作为 root 用户呢?请看下文。

正文

1、查看要进入的容器的 CONTAINER ID

如下示例:

➜  ~ sudo docker ps -a                         
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                                            NAMES
80045ea2ebaf        my_jboss_v1.0.1     "/opt/jboss/wildfly/…"   6 minutes ago       Up 6 minutes                   0.0.0.0:8080->8080/tcp, 0.0.0.0:9990->9990/tcp   my_jboss_v1.0.1
15ee503df884        nginx               "nginx -g 'daemon of…"   28 minutes ago      Up 28 minutes                  0.0.0.0:18000->80/tcp                            nginx_v1.0.0
d23c3ce1b193        mysql:5.7           "docker-entrypoint.s…"   38 minutes ago      Exited (1) 38 minutes ago                                                       mystifying_hermann
a5134f112349        jboss/wildfly       "/opt/jboss/wildfly/…"   2 hours ago         Exited (0) About an hour ago                                                    bold_black
881250e83ebb        ubuntu              "/bin/sh -c 'while t…"   3 weeks ago         Exited (255) 3 weeks ago       0.0.0.0:8888->8888/tcp                           adoring_bell

2、进入容器中作为默认用户

如下示例,如提示输入密码,请输入你宿主机(本机,你的电脑)的密码。

从第一步查到的 CONTAINER ID 是 80045ea2ebaf,但我们不用写完整的 CONTAINER ID,只需要写足够与其他 CONTAINER ID 区分即可。

比如,上面我的 docker 容器有 80045ea2ebaf 与 881250e83ebb 都是 8 开头,那么就不能只写一个 8,可以写 80。

➜  ~ sudo docker exec -it 8 /bin/bash
Error response from daemon: Multiple IDs found with provided prefix: 8

➜  ~ sudo docker exec -it 80 /bin/bash
Password:

[jboss@80045ea2ebaf ~]$ 

3、执行如下命令使用 root 用户进入容器

➜  ~ sudo docker exec -u root -it 80 /bin/bash
[root@80045ea2ebaf jboss]# 
[root@80045ea2ebaf jboss]# 
[root@80045ea2ebaf jboss]#

注:也可以通过 docker exec -it {container_name} bash 的方式进入容器的 shell command。

如下所示:

➜  ~ docker ps                   
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                NAMES
eaff1218a9a9        elasticsearch:6.7.0   "/usr/local/bin/dock…"   8 days ago          Up About a minute   9200/tcp, 9300/tcp, 0.0.0.0:9203->9201/tcp, 0.0.0.0:9303->9301/tcp   ES03
aca2a00000b0        elasticsearch:6.7.0   "/usr/local/bin/dock…"   8 days ago          Up About a minute   9200/tcp, 9300/tcp, 0.0.0.0:9202->9201/tcp, 0.0.0.0:9302->9301/tcp   ES02
94c668218151        elasticsearch:6.7.0   "/usr/local/bin/dock…"   8 days ago          Up About a minute   9200/tcp, 0.0.0.0:9201->9201/tcp, 9300/tcp, 0.0.0.0:9301->9301/tcp   ES01
d6b531108d98        kibana_v0.1           "/usr/local/bin/kiba…"   12 days ago         Up About a minute   0.0.0.0:5601->5601/tcp                                               kibana
c26739b1ce25        portainer/portainer   "/portainer"             3 months ago        Up 10 hours         0.0.0.0:9000->9000/tcp                                               portainer
➜  ~ docker exec -it kibana bash 
bash-4.2$ 

结尾

docker exec 的参数如下:

➜  ~ docker exec --help          
Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注

%d 博主赞过: