![]() |
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.
Here, we will be using a centos container, you can use any container of your choice. Exploring the Filesystem in a Centos containerThere are several ways of exploring a container’s file system:
Method 1: Interactive wayBefore 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.
![]()
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.
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 ![]()
Method 2: Non-interactive wayStep 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.
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.
![]()
Method 3: Using SSHSSH 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.
![]()
Step 2: Now we have to edit the configuration parameters to allow root logins. Use the below command:
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.
Method 4: Using a tar fileA 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.
![]()
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.
![]()
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 filesystemThis 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. |
Reffered: https://www.geeksforgeeks.org
Linux Unix |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |