我已经注意到,与docker,我需要了解什么发生在容器内或什么文件存在于那里。一个例子是从docker索引中下载图像-你不知道图像包含什么,所以不可能启动应用程序。

理想的情况是能够ssh进入它们或具有同等功能。是否有工具可以做到这一点,或者我对docker的概念错误,认为我应该能够做到这一点。


当前回答

docker exec命令在运行中的容器中运行命令在多种情况下都有帮助。


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:
                             [:])
  -w, --workdir string       Working directory inside the container

例如:

1)在bash中访问正在运行的容器文件系统:

docker exec -it containerId bash 

2)在bash中以root身份访问正在运行的容器文件系统,以获得所需的权限:

docker exec -it -u root containerId bash  

这对于能够在容器中作为根执行一些处理特别有用。

3)在bash中访问特定工作目录下运行的容器文件系统:

docker exec -it -w /var/lib containerId bash 

其他回答

在我的例子中,除了sh,容器中不支持任何shell。所以,这就像一个魅力

docker exec -it <container-name> sh

如果您使用的是Docker v19.03,请按照以下步骤操作。

# find ID of your running container:

  docker ps

# create image (snapshot) from container filesystem

  docker commit 12345678904b5 mysnapshot

# explore this filesystem 

  docker run -t -i mysnapshot /bin/sh

docker exec命令在运行中的容器中运行命令在多种情况下都有帮助。


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:
                             [:])
  -w, --workdir string       Working directory inside the container

例如:

1)在bash中访问正在运行的容器文件系统:

docker exec -it containerId bash 

2)在bash中以root身份访问正在运行的容器文件系统,以获得所需的权限:

docker exec -it -u root containerId bash  

这对于能够在容器中作为根执行一些处理特别有用。

3)在bash中访问特定工作目录下运行的容器文件系统:

docker exec -it -w /var/lib containerId bash 

对我来说,这个工作得很好(感谢最后的注释指出目录/var/lib/docker/):

chroot /var/lib/docker/containers/2465790aa2c4*/root/

这里,2465790aa2c4是运行容器的短ID(由docker ps显示),后面跟着一个星号。

我喜欢的理解容器内部发生什么的方法是:

暴露-p 8000 Docker运行-it -p 8000:8000镜像 在里面启动服务器 python -m SimpleHTTPServer