Interactive shell using Docker Compose

女生的网名这么多〃 提交于 2019-11-26 10:05:53

问题


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.


回答1:


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

stdin_open: true
tty: true

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




回答2:


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 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.




回答3:


In the official getting started 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 start this with docker-compose up, you can easily shell into either your redis container or your web container with:

docker-compose exec redis sh
docker-compose exec web sh 



回答4:


docker-compose run myapp sh should do the deal.

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




回答5:


Using docker-compose, I found the easiest way to do this is to do a docker ps -a (after starting my containers with docker-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.




回答6:


If anyone from the future also wonders up 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

That is after you already have your containers up and running




回答7:


You can do docker-compose exec SERVICE_NAME sh on the command line. The SERVICE_NAME is defined in your docker-compose.yml. For example,

services:
    zookeeper:
        image: wurstmeister/zookeeper
        ports:
          - "2181:2181"

The SERVICE_NAME would be "zookeeper".



来源:https://stackoverflow.com/questions/36249744/interactive-shell-using-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!