1. Overview

In this tutorial, we’ll examine how we can start a shell in a new or running Alpine Docker container.

2. Introduction

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.

3. Starting a Shell in an Alpine Container

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.

3.1. Starting a Shell in a New Container

To start a shell session in a new Alpine container, let’s enter the following command into our terminal:

$ docker run -it alpine /bin/s

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.

3.2. 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. In addition to that, we must also recognize a few facts:

  • Docker containers will exit with status 0 if there are no more active processes within that container
  • We can run a shell process in interactive mode or non-interactive mode. In interactive mode, the shell listens to the commands through the standard input stream (STDIN) on a terminal. On the other hand, in non-interactive mode, the shell executes the script passed to it and exits

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.

3.3. Starting a Shell in a Running Container

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.

3.4. Exiting a Shell

Finally, we can quit the shell with the exit command, which will finish the shell process in the container:

/# exit

4. Conclusion

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments