Search code examples
dockerdocker-composedocker-engine

How to set a docker-compose /bin/bash entrypoint?


I'd like to enter a docker container in interactive mode with the commad /bin/bash using a docker-compose.yml only. There is a similar question here on stack overflow: Interactive shell using Docker Compose Answers provided there didn't work. This is what my docker-compose.yml looks like:

version: "3"
services:
  server:
    image: golang:1.11.1
    volumes:
      - './server:/go'
    ports:
      - '8080:8080'

    command: '-ti'
    entrypoint:
      - '/bin/bash'

This is my console in and output:

[bluebrown@firefly gowild]$ docker-compose up --build
Recreating gowild_server_1 ... done
Attaching to gowild_server_1
server_1  | bash: cannot set terminal process group (-1): Inappropriate ioctl for device
server_1  | bash: no job control in this shell
server_1  | root@d5884893075a:/go# exit
gowild_server_1 exited with code 0

Reading the above-mentioned post I tried of course also to substitute:

command: '-ti'

for these two lines:

stdin_open: true
tty: true

but when doing this docker compose gets stuck while attaching:

[bluebrown@firefly gowild]$ docker-compose up --build
Recreating gowild_server_1 ... done
Attaching to gowild_server_1

And nothing happens further. No error and exit nor a 'done' message.

When trying it with sh instead of bash it says the following for the command: '-it:

server_1  | /bin/sh: 0: Illegal option -t

And also gets stuck just like with bash while attaching when substituting it.

Note that I can build and run the server without the command and entrypoint simply using the following:

docker-compose up
docker-compose run --service-ports server

Still my question is how to do it using docker-compose and an entrypoint, so It can be done with docker-compose up only.

Update: I'm using Linux manjaro


Solution

  • In that particular use case the solution should be like below. The reason for this is usually /bin/bash is used with -ti to enter a shell inside the container.

    The same thing can be done with compose by using the run command with a particular service. Note that I am exposing the service ports too.

    docker-compose run --service-ports server bash
    

    https://docs.docker.com/compose/reference/run/

    If the container is already running then exec should be enough.