How to Explore Docker Container’s File System?
Tags: docker, linux
Published: 2021-06-29 15:10:25

<p dir="ltr" style="text-align: justify;">When it comes to exploring a <a href="https://techtutorialsite.com/how-to-create-docker-container/" rel="noopener noreferrer" target="_blank">Docker container&rsquo;s</a> file system, it can be in two ways - interactive and non-interactive. In short, interactive exploration of a Docker container&rsquo;s file system can be done by accessing the file system through the bash of the container. While on the other hand, non-interactive exploration relates to studying the filesystem without entering the container environment either through tar files or copying the filesystem of the container.</p>
<p dir="ltr" style="text-align: justify;">Let&rsquo;s try to discuss both these ways in this article.</p>
<h2 dir="ltr" style="text-align: justify;">Interactive Exploration of a Container File System</h2>
<p dir="ltr" style="text-align: justify;">A container can be in two states where we can explore its file system - non-running and running. By non-running containers, we mean that the container is either not yet created or created but stopped. To explore the file systems of such containers, we need to <a href="https://techtutorialsite.com/docker-container-start-command/" rel="noopener noreferrer" target="_blank">run the container</a> first.&nbsp;</p>
<p dir="ltr" style="text-align: justify;">Suppose you want to explore an Ubuntu container&rsquo;s file system and the container does not exist yet. You can use the <a href="https://techtutorialsite.com/docker-container-start-command/" rel="noopener noreferrer" target="_blank">Docker run command</a> along with the interactive option to access the bash of the container.&nbsp;</p>

<pre>$ docker run -it --name=myubuntu ubuntu:latest bash</pre>

<p dir="ltr" style="text-align: justify;">The above command uses the i (interactive) and t (pseudo-TTY) options to attach a terminal to the container&rsquo;s streams allowing us to interact with the bash of the container through our host machine. We have also used the name option to provide a name to the container. After executing this command, you will have access to the bash of the container and you will be able to interact with the container&rsquo;s file system in the same way you would interact with an Ubuntu machine.</p>
<p dir="ltr" style="text-align: justify;">If the container is stopped and not running, you will have to start the container first using the <a href="https://techtutorialsite.com/docker-container-start-command/" rel="noopener noreferrer" target="_blank">docker start command</a> mentioned below.</p>

<pre>$ docker start myubuntu</pre>

<p dir="ltr" style="text-align: justify;">This will start the myubuntu container which was earlier in the exited state.</p>
<p dir="ltr" style="text-align: justify;">Next, you can use the <a href="https://techtutorialsite.com/docker-exec-command/" rel="noopener noreferrer" target="_blank">Docker exec command</a> to attach a terminal to the already running container.</p>

<pre>$ docker exec -it myubuntu bash</pre>

<p dir="ltr" style="text-align: justify;">The Docker exec command is used to run a command inside a running container. With the above command, you have run the bash command inside the myubuntu container which will open a bash in interactive mode. You can now easily interact with the file system of the container through this bash.</p>
<p dir="ltr" style="text-align: justify;">You can also use the <a href="https://techtutorialsite.com/docker-container-start-command/" rel="noopener noreferrer" target="_blank">Docker start command</a> with the -ai option to attach a terminal.</p>

<pre>$&nbsp;docker start -ai myubuntu</pre>

<h2 dir="ltr" style="text-align: justify;">Non-Interactive Exploration of a Container File System</h2>
<p dir="ltr" style="text-align: justify;">Suppose you have a container in your local machine and you want to explore its file system in non-interactive mode. You can create a copy of its filesystem using the <a href="https://techtutorialsite.com/docker-commit-changes-to-containers/" rel="noopener noreferrer" target="_blank">Docker commit command</a>. This will create a new image after committing all the changes and you can run a container associated with this new copy image and inspect the file system.</p>
<p dir="ltr" style="text-align: justify;">To check the containers in your system, you can use the following command.</p>

<pre>$ docker ps -a</pre>
                
<p dir="ltr" style="text-align: justify;">Now, suppose you have a container called myubuntu in the exited state. Let&rsquo;s try to commit this container to a new image called copy-image.</p>

<pre>$ docker commit myubuntu copy-image</pre>

<p dir="ltr" style="text-align: justify;">You can now check that the new image has been created by <a href="https://techtutorialsite.com/list-docker-images/" rel="noopener noreferrer" target="_blank">listing all the images</a>.</p>

<pre>$ docker images</pre>
                
<p dir="ltr" style="text-align: justify;">Now, you can run a test-container associated with this copy-image to explore the file system of the container.</p>

<pre>$ docker run -it --name=test-container copy-image bash</pre>
                
<p dir="ltr" style="text-align: justify;">Another way to explore a container&rsquo;s file system is to install SSH in your Docker container and run the ssh daemon. In this way, you can explore the container&rsquo;s file system continuously by doing SSH into the docker container.</p>

<pre>$ docker run -d -p 22 copy-image /usr/sbin/sshd -D</pre>

<p dir="ltr" style="text-align: justify;">You can run the docker ps to check which port to connect to.</p>
<p dir="ltr" style="text-align: justify;">To sum up, in this article we have discussed four different ways to explore a Docker container&rsquo;s file system.</p>