Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive shell using Docker Compose

Is there any way to start an 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 an additional option to myapp, to start an interactive shell?

I know there are native docker command options to achieve this, just curious if it's possible using only Docker Compose, too.

like image 618
drubb Avatar asked Mar 27 '16 16:03

drubb


People also ask

How do I run docker with interactive shell?

If you need to start an interactive shell inside a Docker Container, perhaps to explore the filesystem or debug running processes, use docker exec with the -i and -t flags. The -i flag keeps input open to the container, and the -t flag creates a pseudo-terminal that the shell can attach to.

What is Stdin_open?

tty and stdin_open are analogous to the -t and -i arguments for the docker run command, respectively. You use stdin_open when you need to work on a project outside the Docker container. You use tty when you need to work on a project inside the Docker container.

Is Docker compose deprecated?

Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.


2 Answers

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

version: "3" services:   app:     image: app:1.2.3     stdin_open: true # docker run -i     tty: true        # docker run -t  

The first corresponds to -i in docker run and the second to -t.

like image 54
Leon Carlo Valencia Avatar answered Sep 20 '22 10:09

Leon Carlo Valencia


The canonical way to get an interactive shell with docker-compose is to use:

docker-compose run --rm myapp 

(With the service name myapp taken from your example. More general: it must be an existing service name in your docker-compose file, myapp is not just a command of your choice. Example: bash instead of myapp would not work here.)

You can set stdin_open: true, tty: true, however that won't actually give you a proper shell with up, because logs are being streamed from all the containers.

You can also use

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

to get a shell on a running container.

like image 29
dnephin Avatar answered Sep 19 '22 10:09

dnephin