This tutorial guides you on how to explore docker container’s file system in different ways. Let’s see three ways to explore files in docker container; using docker exec, using docker commit and via sshd daemon.
Explore docker container’s file system – docker exec
Let’s use standard ubuntu image for our example. Docker provides docker exec command which can be used for running a new command in a running docker container. Note, the command runs only when the docker container is in running state.
For example, let’s say when you use docker exec command for the docker container which is not in a running state as shown below, it will result in the following error.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1e4467b07108 9 days ago 73.9MB $ docker run ubuntu $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e0c0d1e1907b ubuntu "/bin/bash" 5 seconds ago Exited (0) 4 seconds ago dazzling_roentgen $ docker exec -it e0c0d1e1907b /bin/bash Error response from daemon: Container e0c0d1e1907b1023ac8fb7ba64a28ddc87c7ea09ed8a2725a19613e2caa7c8af is not running
First, start the docker container using the following command in one terminal. We are running this command just to ensure the docker container is up and running.
$ docker run --rm -it ubuntu bash root@06c426928f03:/# echo hello hello
Then, in the another terminal try to run docker ps command and check if the container is up and running.
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06c426928f03 ubuntu "bash" 28 seconds ago Up 27 seconds lucid_jennings
Finally execute the following docker exec command in the second terminal. This command will start a bash session on the docker container and login you as root user, then you can use ls command.
$ docker exec -it 06c426928f03 /bin/bash root@06c426928f03:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
Explore using docker commit
Now, let’s see how to explore docker container’s file system using docker commit command. First, let’s create an copy image from the existing container file system (STEP 3). Then explore the file system using docker run to start an interactive bash session in the new/copy docker image.
//STEP 1 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1e4467b07108 10 days ago 73.9MB //STEP 2 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f03045d1048 ubuntu "/bin/bash" 25 seconds ago Exited (0) 22 seconds ago nifty_chandrasekhar //STEP 3 //copy image from the existing container file system $ docker commit 5f03045d1048 ubuntu_copy sha256:126d7f93268b4591a9a1b91c8ee90f76619d15969aa633585f12ba39302287b2 //STEP 4 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu_copy latest 126d7f93268b 4 seconds ago 73.9MB ubuntu latest 1e4467b07108 10 days ago 73.9MB //STEP 5 $ docker run -it ubuntu_copy /bin/bash root@c7b726f7cf6c:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
Explore file system using SSH
The third way is to install SSH in docker container and run ssh daemon. So that you can explore container’s file system continuously whenever needed by doing SSH in to the docker container.
Then run the following docker run command to run sshd daemon in the container.
docker run -d -p 22 ubuntu_copy /usr/sbin/sshd -D
Do docker ps and check which port to connect. Finally you can ssh in to the docker container and check the file system any time continuously.
Hope it helped 🙂
- Get Docker Container’s IP Address from the Host.
- Copy Files between Host and Docker Container.
- Remove All Unused and Dangling Docker Images ?
- Difference between CMD and ENTRYPOINT in Dockerfile ?
- Ping inside the docker container from host
- Docker: Error response from daemon: OCI runtime create failed: container_linux.go:349
- List the directories inside the docker container
- Add new user to the Docker container using Dockerfile
- Docker images are storage location
- Difference between Docker Images, Containers and Registries
- Clean up Old Stopped Docker Containers
- Docker EXPOSE Port only to the Host on Google Cloud