Interactive shell using Docker Compose

Better Stack Team
Updated on April 13, 2023

To create an interactive shell using Docker Compose, you can specify the command to run in the container as an interactive shell. Here's an example docker-compose.yml file that launches an interactive shell container:

 
version: '3'
services:
  myservice:
    image: myimage
    tty: true
    stdin_open: true
    command: sh

In this example, the myservice container is launched with the myimage image, and the tty and stdin_open options are set to true to enable an interactive shell. The command option is set to sh to run a shell as the default command.

You can then launch the container and start an interactive shell by running:

 
docker-compose up

This will start the container and attach your terminal to it, allowing you to enter commands in the interactive shell.

Note that if the container is running a long-lived process, such as a web server, you may need to use a tool like docker exec to start an interactive shell in the running container. To do this, you can run:

 
docker exec -it <container-name-or-id> sh

This will start a new shell session in the running container, which you can use to run commands interactively.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack