Interactive Shell Using Docker Compose

Is there a way to launch an interactive shell in a container using only Docker Compose? I tried something like this in my docker-compose.yml:

myapp: image: alpine:latest entrypoint: /bin/sh 

When I launch this container using docker layout, it will exit immediately. Are there any flags that I can add to the entrypoint command, or like an additional parameter for myapp, to run as an interactive shell?

I know that there are docker command options for this, it’s just curious if only Docker Compose can be used.

+194
shell docker interactive docker-compose
Mar 27 '16 at 16:25
source share
6 answers

You need to include the following lines in the docker-compose.yml file:

 stdin_open: true tty: true 

The first is -i in docker run , and the second is -t .

+251
Aug 25 '16 at 16:09
source share

The canonical way to get an interactive shell using docker-compose is to use: docker-compose run --rm myapp

You can set stdin_open: true, tty: true , however this will not actually give you the correct wrapper with up , because the logs are sent from all containers.

You can also use

 docker exec -ti <container name> /bin/bash 

to get a shell on a running container.

+159
Mar 28 '16 at 15:54
source share

In the official launch example ( https://docs.docker.com/compose/gettingstarted/ ) with the following docker-compose.yml :

 version: '3' services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine" 

After you run this with docker-compose up , you can easily redis into your redis container or your web container:

 docker-compose exec redis sh docker-compose exec web sh 
+43
Nov 14 '17 at 22:06
source share

docker-compose run myapp sh should close the deal.

There is some confusion with up / run , but docker-compose run docs have a great explanation: https://docs.docker.com/compose/reference/run

+29
Apr 04 '18 at 9:23
source share

Using docker-compose, I found that the easiest way to do this is to make docker ps -a (after running my containers with docker-compose up ) and get the identifier of the container in which I want to have an interactive shell (let name it xyz123).

Then just do the docker exec -ti xyz123 /bin/bash task

and voila, an interactive shell.

+21
Apr 18 '17 at 18:01
source share

If someone from the future is also surprised here:

 docker-compose exec container_name sh 

or

 docker-compose exec container_name bash 

or you can run single lines like

 docker-compose exec container_name php -v 

This happens after you have already loaded your containers.

+8
Oct 08 '18 at 8:40
source share



All Articles