Open In App

Exploring a Docker Container’s Filesystem in Linux

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

There are times during our development process when we need an in-depth exploration of the filesystem of the docker container that we are using. But how to do that? In this article we are going to look at 6 ways that are used for the above purpose.

If you are not familiar with the basic commands used in docker then you can refer to the below article about the top 9 most used docker commands.

Here, we will be using a centos container, you can use any container of your choice.

Exploring the Filesystem in a Centos container

There are several ways of exploring a container’s file system:

  • Method 1: Interactive way
  • Method 2: Non-interactive way
  • Method 3: By using the ssh daemon
  • Method 4: Using a tar file
  • Method 5: Exporting the filesystem (with hello-world container)
  • Method 6: Copying the filesystem

Method 1: Interactive way

Before we start exploring the filesystem, we have to keep one thing in mind, some containers come with default logs that show during the startup. Thus to connect to a shell we will have to give an additional /bin/bash command.

Step 1: Pull the docker image from the docker hub and run it.

docker run -it <container name> (in our case it is  centos)

 

Step 2: You can also use the exec command to connect to the shell, but in this case, you will have to run the container in the detached mode so that it keeps running in the background.

docker run -t -d –name <container_name> <image_name>

docker exec -it  <container_name> /bin/bash

The above two commands will also connect you to the bash shell.

Step 3: Now that we are inside the container let us the contents and start exploring them. Use the ls command to list the contents:

ls 

 

Note: When you use the run command you are creating a container for the first time, and if the container is stopped you can start it again with the help of “docker start” command.

Method 2: Non-interactive way

Step 1: Suppose we want to explore our container in a non-interactive way, for this we can create a copy or a snapshot of the filesystem of the container. When you copy a container’s file system you are creating an image that has exactly the same feature as that of the container.

docker commit <container_name> <copied_image_name>

In our case, the name of the centos container is my_cont and the name of the snapshot is sample_image.

 

Step 2: Now use this commit or snapshot can be explored with the help of bash shell.

docker run -t -i sample_image  /bin/bash (This will open a bash shell)

 

Method 3: Using SSH

SSH is a service that allows you to interact with remote servers. You can also ssh into a container and explore it. We will set up a dummy alpine container and bind port 22(on the dummy container) to a port on host 8081. 

Step 1: Enter the alpine container and install openssh-server.

 docker run -it -p 8081:22 alpine ash

 

apk add openssh-server ( used to install ssh server)

Step 2: Now we have to edit the configuration parameters to allow root logins. Use the below command: 

sed -E ‘s/^#(PermitRootLogin )no/\1yes/’ /etc/ssh/sshd_config -i 

Step 3: After this, we will have to generate host keys and start the server with “/usr/sbin/sshd &” command.

ssh-keygen -A
/usr/sbin/sshd &

 

Step 4: Use the command ps aux to verify the installation of the server. Now we can successfully login from another container.

ssh root@<ip_address_of_the_host>

Method 4: Using a tar file 

A tar file is a UNIX utility that allows you to package various files together so that they can be used for backup and distribution purposes.

Sometimes you may have a container that does not have an interactive shell then in that case you can store all of its contents into a tar file and then copy that file to another system to explore the contents. For doing this we will use the Export command.

docker export (container_name)  >  file.tar   

 

Now we can unzip this tar file whenever we need to explore the filesystem.

Method 5: Exporting the filesystem (with hello-world container)

We can also export our filesystem in this way, for this method we will be using the simple hello-world container that comes with no shell to explore it. Note that we can export the container in the stopped condition also.

Step 1: Type the below command to get the id of the hello-world container.

docker ps -a

 

 

Step 2: Now we will copy the filesystem of this container into a sample.tar file by using the below command.

docker export -o sample.tar <id_of_the_container>

 

Note: The -O flag is used to put the contents inside the tar file.

Step 3: To see the contents of the filesystem, we can use the “tvf” flag with tar.

tar -tvf sample.tar

 

Method 6: Copying the filesystem

This method also comes in very handy, with this we can just simply copy our entire filesystem and explore it afterward.

Step 1: Let us create a sample2 directory file and copy the contents of the hello-world container into it.

docker cp <container_id>:/  ./sample2

 

Step 2: To see the contents use the ls command:

ls -all sample2/

 

The “:/” is used so that everything from the root directory of the container is copied into our sample2 directory.

So these were the 6 ways in which you can explore a container’s file system in Linux.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads