Is there any way to start a interactive shell in a container using Docker Compose only? I've tried something like this, in my docker-compose.yml:
myapp:
image: alpine:latest
entrypoint: /bin/sh
When I start this container using docker-compose up it's exited immediately. Are there any flags I can add to the entrypoint command, or as and additional option to myapp, to start as interactive shell?
I know there are native docker command options to achieve this, just curious if it's possible using only Docker Compose, too.
The canonical way to get an interactive shell with docker-compose is to use:
docker-compose run --rm myapp
You can set
stdin_open: true, tty: true
, however that won't actually give you a proper shell withup
, because logs are being streamed from all the containers.You can also use
to get a shell on a running container.
Using docker-compose, I found the easiest way to do this is to do a
docker ps -a
(after starting my containers withdocker-compose up
) and get the ID of the container I want to have an interactive shell in (let's call it xyz123).Then it's a simple matter to execute
docker exec -ti xyz123 /bin/bash
and voila, an interactive shell.
docker-compose run myapp sh
should do the deal.There is some confusion with
up
/run
, butdocker-compose run
docs have great explanation: https://docs.docker.com/compose/reference/runIf anyone from the future also wonders up here:
or
or you can run single lines like
That is after you already have your containers up and running
In the official getting started example (https://docs.docker.com/compose/gettingstarted/) with the following
docker-compose.yml
:After you start this with
docker-compose up
, you can easily shell into either yourredis
container or yourweb
container with:You need to include the following lines in your docker-compose.yml:
The first corresponds to
-i
indocker run
and the second to-t
.