Interactive shell using Docker Compose

Better Stack Team
Updated on October 5, 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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github