Skip to content

Docker - Containers & Shells

Containers are live instances of Docker images. Once you run an image, you often need to interact with the running container, perhaps to execute commands inside it or manage its lifecycle.

You can start a container and get an interactive shell immediately using the -it flags with docker run.

# Example: Start an Ubuntu container and get a bash shell
docker run -it ubuntu bash
  • -i (interactive): Keeps STDIN open even if not attached.
  • -t (tty): Allocates a pseudo-TTY (terminal).

While inside an interactive container session started this way, you can run commands as if you were logged into that system. To exit the shell without stopping the container, use the detach sequence: Ctrl+P followed by Ctrl+Q.

If you simply type exit or press Ctrl+D, the main process (in this case, bash) terminates, and the container will stop.

Executing Commands in Running Containers (docker exec)

Section titled “Executing Commands in Running Containers (docker exec)”

The standard and most flexible way to run commands inside an already running container is using docker exec. This is incredibly useful for debugging, checking logs, or performing administrative tasks without needing to stop and restart the container.

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  • -i, --interactive: Keep STDIN open.
  • -t, --tty: Allocate a pseudo-TTY.
  • -d, --detach: Run command in the background.
  • -w, --workdir: Specify working directory inside the container.

First, start a container in the background (detached mode using -d):

# Start an nginx container named 'my-webserver' in the background
docker run -d --name my-webserver nginx:stable-alpine

Now, use docker exec to interact with it:

# Get an interactive bash shell inside the 'my-webserver' container
docker exec -it my-webserver sh # Alpine images use 'sh' by default
# Run a single command (list files in /usr/share/nginx/html)
docker exec my-webserver ls /usr/share/nginx/html
# Check the nginx version non-interactively
docker exec my-webserver nginx -v

docker exec is preferred over older methods like nsenter or attaching directly to the container’s primary process for general interaction, as it’s simpler and purpose-built for this task.

Attaching to a Running Container (docker attach)

Section titled “Attaching to a Running Container (docker attach)”

The docker attach command connects your terminal’s standard input, output, and error streams to a running container’s primary process (the one specified by CMD or ENTRYPOINT in the Dockerfile). This is different from docker exec.

docker attach CONTAINER

Use docker attach primarily when you need to interact directly with the container’s main running application, especially if it’s designed for interactive use or you want to see its direct output. Be cautious: exiting an attached session often stops the container if the main process terminates.

For getting a shell or running arbitrary commands, docker exec is almost always the better choice.