在使用Docker创建了容器之后,大家比较关心的就是如何进入该容器了,其实进入Docker容器有好几多种方式,这里我们就讲一下常用的几种进入Docker容器的方法。
进入Docker容器比较常见的几种做法如下:
1.使用docker attach
2.使用SSH
3.使用nsenter
4.使用exec
目前最为常用的是第四种,exec
四、docker exec进入Docker容器
这种方式相对更简单一些,下面我们来看一下该命令的使用:
[c@localhost ~]$ 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 (default [])
--help Print usage
-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>])
命令如下:
-it 标准输入和关联伪终端,-it后跟容器ID,/bin/bash是命令,表示在该容器中运行该命令
sudo docker exec -it 775c7c9ee1e1 /bin/bash