Start a Bash terminal in a new Docker container
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers to manage their applications and dependencies.
Docker provides us with multiple ways to access a shell instance by launching a bash terminal in a docker container. This is particularly useful when running certain commands inside a docker container.
Additionally, this could also be for debugging reasons or you might want to check if everything is configured correctly. This article explores the different methods we can use to run containers and not run containers.
Using the Bash Interactive Shell
Bash is a common command processor in Linux systems that allows users to enter commands that result in actions. If you have interacted with Unix-based systems or WSL at all, you have probably interacted with various commands through bash.
Similarly, we can also directly access the Linux terminal in the docker container and execute commands just like using a normal Linux bash. One advantage of this approach is that we can do this with a non-running container, docker exec
unlike other commands such as the command.
As shown below, we will use the official image from the docker registry rabbitmq
to create a docker container and directly access bash inside the container. You can do this with any other command; just make sure you have the image beforehand.
docker pull rabbitmq
docker images
Now that we have the image, we can create the docker container interactively. This means we can run commands inside the docker container while it is running as shown below.
$ docker run -it rabbitmq bash
Output:
root@f418a3286aae:/#
As you can see, we are now inside the docker container and we have successfully run bash inside the new container. We can now execute commands as if we were using a real terminal.
For example, we can list the files and directories inside this container as follows.
root@f418a3286aae:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt plugins proc root run sbin srv sys tmp usr var
root@f418a3286aae:/#
Use docker exec
the command
Alternatively, we can also docker exec
run bash in a new docker container using the command. However, unlike the previous method, this command requires that we already have the container running; otherwise, the command will not work.
Use docker ps -a
the command to confirm that our container is running. If the container you want to use is not running, you may need to docker start
start it using the command followed by the container ID or name.
docker ps
We will -it
use docker exec
the command alongside the docker tag. exec
The docker command allows us to execute commands within a running container, while -it
the docker tag allows us to open a container interactively.
We can execute as shown below.
$ docker exec -it f418a3286aae bash
Output:
root@f418a3286aae:/# ls
bin boot dev etc home isaac lib lib32 lib64 libx32 media mnt opt plugins proc root run sbin srv sys tmp tonyloi usr var
Now that we have successfully started bash in this docker container, we can run various commands in the container. We can also use it for containers without bash sh
.
This is also a command that will open up a basic shell prompt where we can run our commands within the container.
$ docker exec -it f418a3286aae sh
Output:
# ls
bin boot dev etc home isaac isaactonyloi lib lib32 lib64 libx32 media mnt opt plugins proc root run sbin srv sys tmp tonyloi usr var
Use docker attach
the command
We can also docker attach
start bash inside a running docker container using the command. This allows us to attach the local standard input, output, and error streams to the running container using the container's ID.
We can then run various commands, accept input, and debug a specific container. We need a running container to attach our output, input, and error streams to.
We can achieve this using either the container name or the id as shown below.
Code:
$ docker container attach f418a3286aae
Output:
root@f418a3286aae:/# ls
bin boot dev etc home isaac isaactonyloi lib lib32 lib64 libx32 media mnt opt plugins proc root run sbin srv sys tmp tonyloi usr var
root@f418a3286aae:/#
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:100 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:164 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:129 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:139 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application
Copy files from host to Docker container
Publish Date:2025/03/25 Views:127 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:99 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t