Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll examine how we can start a shell in a new or running Alpine Docker container.
Docker containers are usually run in the background as services, managed by the Docker daemon. However, there are times when we need to do something in the container environment, for instance, modifying a config file, or installing an additional tool. We can carry out those tasks using an interactive shell process in the container.
We’ll demonstrate how to achieve this using Docker commands such as docker run and docker exec.
Alpine is the most lightweight Linux distribution. Due to its small size, many people are using Alpine as their base image. Although we’re using Alpine containers in this article, this method applies to other Docker containers as well.
To start a shell session in a new Alpine container, let’s enter the following command into our terminal:
$ docker run -it alpine /bin/sh
Let’s now break down the command:
Firstly, docker run is a Docker command that is used to create a Docker container and has the following syntax:
docker run [OPTIONS] IMAGE[:tags] [COMMAND]
In our case, we’ve instructed Docker to create a container based on image alpine and run the command /bin/sh with the -it flags.
If we’ve started the shell process without the -it flags, the container will start, and then exit with status 0 almost immediately. Before we move on further, it’s useful to know how Docker deals with a container’s standard I/O streams.
According to the documentation, a Docker container that runs in foreground mode will only have its standard output stream (STDOUT) and standard error stream (STDERR) attached if we don’t add the -a option. In addition to that, we must also recognize a few facts:
From the docker run command documentation, we can see that the flag -i will “keep STDIN open even if not attached”. On the other hand, the flag -t will allocate a pseudo-terminal to that container process. Using -it, we can start up an interactive shell process that is listening to the STDIN. The allocated pseudo-terminal will then allow us to send commands to the STDIN of the shell process.
To start a shell process in a running container, we can use the command:
$ docker exec -it <container-name> /bin/sh
Where the <container-name> should be replaced with either the container name or container ID. Similarly, we’re using the -it flags here to start the shell process in interactive mode.
Note that to start a shell process in a running container, we use docker exec instead of docker run.
Finally, we can quit the shell with the exit command, which will finish the shell process in the container:
/# exit
In this tutorial, we’ve shown how we can use docker run to start a shell in a new Alpine container. Besides that, we’ve also looked at starting a shell process in a running container with docker exec. Finally, we’ve examined the -it flags that make the shell process interactive.