Open Docker Container Bash


Answer (1 of 5): The question is a bit different from the description. To answer based on what you described, those to commands can produce different outputs. The first one, code docker run -it —rm dockerimage bash/code will start a container based off the dockerimage image, and it wil. I tells Docker we want to have an interactive terminal and keep STDIN open even if not attached.t tells Docker we want to start a TTY session. Container-id Get this value from performing a docker ps on the command line to see your running container. It'll be in the Container ID column and if you can't see it then the container isn't running. Jun 27, 2019 Method 1: Snapshoting. You can go through the container filesystem: # find ID of your running container: docker ps # create image (snapshot) from container filesystem docker commit 4b5 mysnapshot # explore this filesystem using bash (for example) docker run -t -i mysnapshot /bin/bash. By doing this you can check the filesystem of. See full list on ligerlearn.com.

1. Overview


When we work with Docker, sometimes we need to check configuration or log files inside a container.

In this quick tutorial, we'll see how to inspect the filesystem of a Docker container to help us address such situations.

2. Interactive Exploring

We can explore the filesystem interactively for most containers if we get shell access to them.

2.1. Running a Container With Shell Access

Let's start a container directly with shell access using the docker run command with the -it option:

Here, we launched the Alpine Linux container in interactive mode and connected to its shell.

But what happens if we want to explore something that's not directly a Linux distribution?

The Cassandra docker container comes with a default startup command, which runs Cassandra. As a result, we're no longer connected to a shell.

Instead, we just see the standard output populated with log messages of the application.

However, we can bypass the default startup command.

Let's pass the /bin/bash additional argument to the docker run command:

Unfortunately, this has a nasty side-effect. The actual Cassandra application is no longer started, and we have to do this from the shell manually.

When we use this approach, we assume that we can control the startup of the container. In a production environment, this might not be possible.

2.2. Spawning a Shell in a Running Container

Fortunately, we can use the docker exec command, whichallows us to connect to running containers.

Let's first start the container we want to explore:

Next, we identify the container id with docker ps:

Then, we pass /bin/bash as the argument with -it option to docker exec:

Here, we used Bash as our shell of choice. This can vary depending on which Linux distribution the container is based on.

By contrast, our first example uses Alpine Linux, which comes with the Bourne Shell by default:

Open Docker Container Bash

Since Bash isn't available, we pass /bin/sh as the argument to docker exec:

3. Non-Interactive Exploring

Sometimes, the container is stopped, and we can't run it interactively, or it simply does not have a shell.

For example, the hello-world is a minimal container that starts from scratch. As a result, shell access is not possible.

Lucky for us, in both situations, we can dump the filesystem to our host machine for further exploration.

Let's see how we can do this.

3.1. Exporting the Filesystem

We can export the filesystem of a container into a tar file by using the docker exportcommand.

Open Docker Container Bash 2020

Let's first run the hello-world container:

Similarly, we first obtain the container id of a stopped container by passing the -a flag to docker ps:

Then we dump the filesystem into the hello.tar file using the -o option of docker export:

Finally, we print the contents of the archive using the tar utility with the -tvf flags:

Alternatively, we can use any archive explorer to see what's inside.

3.2. Copying the Filesystem

We can also copy the entire filesystem using the docker cp command.

Let's try this as well.

First, we copy the complete filesystem starting from the root (/)from our container to the test directory:

Next, let's print the contents of the test directory:

4. Conclusion

In this quick tutorial, we discussed how to explore the filesystem of a Docker container.

We can start most containers with shell access directly with the dockerrun command. In addition, we can spawn a shell for running containers with the help of docker exec.

When it comes to stopped containers or minimal containers, we can simply export or even copy the entire filesystem locally.

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


So, if you are new to Docker, you might wonder how to run a docker container. Let me quickly show you that.

You can create and run a container with the following command:

And then, if you want to enter the container (to run commands inside the container interactively), you can use the docker exec command:

Here’s an example where I create a new container with Ubuntu as the base image and then I enter the running Ubuntu container and run the ls command:

Lost? Don’t worry. I’ll explain in detail what the above two commands do and what is the -it option in the docker run and exec command.

How to run docker container

If you want to run a docker container with a certain image and a specified command, you can do it in this fashion:

The above command will create a new container with the specified name from the specified docker image. The container name is optional.

  • The -i option means that it will be interactive mode (you can enter commands to it)
  • The -t option gives you a terminal (so that you can use it as if you used ssh to enter the container).
  • The -d option (daemon mode) keeps the container running in the background.
  • bash is the command it runs.

The reason for running bash as command here is that the container won’t stop immediately.

In the above example, I didn’t name the container so it was randomly named determined_blackburn.

And as you can see, the container is running bash command in the background.

What happens if you don’t run it as a daemon (-d option) in the background?

If you do not use the -d option, docker run will create a new container and you’ll have a terminal in interactive mode running bash shell.

As you can see in the example below, the container is created and I am automatically inside the container (bash shell).

The problem here is that if you exit the container, the container stops.

What happens if you only use the -i (interactive) option?

Let’s say you only use the -i option. In that case, you’ll see an interactive prompt but if you exit the interactive prompt (using Ctrl+D or exit command), you exit the container and the container stops.

What happens if you only use the -t (terminal/tty) option?

That’s a tricky situation because in that case, you’ll have a pseudo terminal (if you use the -t option) but you won’t have the STDIN. In other words, you cannot enter anything, cannot run command inside the container like you did earlier:

I had to stop the container from another terminal in the above case.

How to run an existing container

The docker run command creates a new container from the specified image. But what happens when you already have a container?

If you want to run an existing container, you must first start the container and then you can use the exec option like this:

This example will be better for your understanding:

Why use bash all the time?

In all the above examples, I have used bash or /bin/bash as the command that runs with the container. I used it because it gives a shell and when you run the container, thanks to the shell, you can run regular commands inside the container as if you are inside a regular Linux system.

You can ask the container to run any command but keep in mind that the container exists as soon as the command completes.

As you can see in the example below, there is no interactive terminal session this time. You are not ‘inside’ the container anymore because the echo command finishes almost immediately.

You can in fact run other commands as well and enter the container afterwards.


In the example below, I created a new container that runs nginx server on port 80. Since I run it as a daemon with option -d, the nginx container keeps on running.

And then I use the docker exec command to get an interactive bash shell and thus enter inside the nginx container (which is basically a Linux preconfigured with nginx).

I hope you have a better understanding about how to run docker containers and why it uses certain options.

If you have questions or suggestions, do let me know in the comment section.

  • Most Viewed News

    • Mega App Mac
    • Download Xcode Mojave
    • Catalina Downloader
    • Install Docker Rancher
    • Install Docker Inside Ubuntu Container