How do I get into a Docker container?

2019-01-03 18:44发布

I'm getting started working with Docker. I'm using the WordPress base image and docker-compose.

I'm trying to ssh into one of the containers to inspect the files/directories that were created during the initial build. I tried to run docker-compose run containername ls -la, but that didn't do anything. Even if it did, I'd rather have a console where I can traverse the directory structure, rather than run a single command. What is the right way to do this with Docker?

21条回答
家丑人穷心不美
2楼-- · 2019-01-03 19:51

If you're here looking for a Docker Compose-specific answer like I was, it provides an easy way in without having to look up the generated container ID.

docker-compose exec takes the name of the service as per your docker-compose.yml file.

So to get a Bash shell for your 'web' service, you can do:

$ docker-compose exec web bash
查看更多
干净又极端
3楼-- · 2019-01-03 19:51

To inspect files, run docker run -it <image> /bin/bash to get an interactive terminal. The list of images can be obtained by docker images. In contrary to docker exec this solution works also in case when an image doesn't start (or quits immediately after running).

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-03 19:51

In my case, for some reasons I need to check all network involved information in each container. So the following commands must be valid in container...

ip
route
netstat
ps
...

I checked through all these answers, no valid for me. I’ve searched information in other websites. I won’t add a super link here, since it’s not written in English. So I just put up this post with summary solution for people who has the same requirement as me.

Say you have one running container named light-test. Follow the steps below.

  • docker inspect light-test -f {{.NetworkSettings.SandboxKey}}. This command will get reply like /var/run/docker/netns/xxxx.
  • Then ln -s /var/run/docker/netns/xxxx /var/run/netns/xxxx. The directory may not exist, do mkdir /var/run/netns first.
  • Now you may execute ip netns exec xxxx ip addr show to explore network world in container.

PS. xxxx is always a same value got through the first command. And of course, any other commands are valid, i.e. ip netns exec xxxx netstat -antp|grep 8080.

查看更多
登录 后发表回答