How to Launch Bash Terminal in New Docker Container
Docker containers are the standard unit for packaging all dependencies of our applications allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now heavily rely on containers to manage their applications and dependencies.
Docker provides us with multiple ways to access a shell instance by launching a bash terminal within a docker container. This is particularly useful when running some commands inside the docker container.
Besides, this may also be for debugging reasons, or maybe you want to check if everything is properly configured. This article explores different methods that we can use to do that for running containers and containers that are not running.
Use the Bash Interactive Shell
Bash is a command processor common with Linux systems that allow users to type in commands that cause actions. If you had some interaction with Unix-based systems or the WSL, you might interact with various commands through bash.
Similarly, we can also directly access the Linux terminal within a docker container and execute commands as we would do with the normal Linux bash. One advantage of this method is that we can do this with a not running container, unlike other commands such as the docker exec
command.
As shown below, we’ll be using the official rabbitmq
image from the docker registry to create a docker container and directly access the bash within the container. You can do this with any other command; you want to ensure you have the image beforehand.
docker pull rabbitmq
docker images
Now that we have the images, we can create the docker container interactively. This means that we can run commands inside the docker container while still running, as shown below.
$ docker run -it rabbitmq bash
Output:
root@f418a3286aae:/#
As you can see, we are now within the docker container, and we have successfully managed to run the bash inside the new container. Now we can execute our commands as though we were working with the real terminal.
For instance, we can list the files and directories inside this container, as shown below.
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 the docker exec
Command
Alternatively, we can also use the docker exec
command to run the bash inside a new docker container. However, unlike the earlier method, this command requires that we have the container running already; otherwise, the command will not work.
Use the docker ps -a
command to confirm that our container is running. If the container you want to use is not running, you may want to get it started using the docker start
command followed by the container id or name.
docker ps
We will use the docker exec
command alongside the -it
tag. The exec
command allows us to execute a command into the running container, while the -it
tag allows us to open the container interactively.
We can execute that 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 launched bash inside this docker container, we can run various commands from within the container. We can alternatively use sh
for containers that do not have a bash.
This is also a command that will open 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 the docker attach
Command
We can also launch bash inside a running docker container using the docker attach
command. This allows us to attach the local standard input, output, and error streams to a running container using the ID of the container.
We can then run various commands, accept input, and debug the specified container. We need to have a running container to attach our output, input, and error streams.
We can implement this using the container name or 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:/#
Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.
LinkedIn