99

I have following docker command to run container

docker run -d --name test -v /etc/hadoop/conf:/etc/hadoop/conf -v /usr/lib/python2.7/dist-packages/hdinsight_common:/usr/lib/python2.7/dist-packages/hdinsight_common -v /etc/hive/conf/:/etc/hive/conf/ -v /etc/tez/conf/:/etc/tez/conf/ -v /usr/hdp/2.4.2.0-258/sqoop/lib/:/usr/hdp/2.4.2.0-258/sqoop/lib/ -i -t hdinsight /bin/bash 

This was to complicated so I was trying to create docker-compose file like this

version: '2' services: hdinsight: image: hdinsight container_name: ABC volumes: - /etc/hadoop/conf:/etc/hadoop/conf - /usr/lib/python2.7/dist-packages/hdinsight_common:/usr/lib/python2.7/dist-packages/hdinsight_common - /etc/hive/conf/:/etc/hive/conf/ - /etc/tez/conf/:/etc/tez/conf/ - /usr/hdp/2.4.2.0-258/sqoop/lib/:/usr/hdp/2.4.2.0-258/sqoop/lib/ entrypoint: - bash labels: - "HDInsight client VM" 

But I am not sure where to pass -d, -i & -t flages from my original docker run command

I was running docker-compose like this

docker-compose -f docker-compose.yml run hdinsight 

can anyone point me to right direction here ?

UPDATE after first answer

I tried to run docker-compose up -d

root@abc-docker:~/ubuntu# docker-compose up -d Creating ABC root@sbd-docker:~/ubuntu# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ffa4c359abf7 hdinsight "/bin/bash" 5 seconds ago Exited (0) 5 seconds ago ABC root@sbd-docker:~/ubuntu# 

Dont know why its in Exited status

Any idea ?

Thanks

3
  • 50
    docker-compose up -d Commented Jun 28, 2016 at 23:25
  • 4
    Your provided entrypoint executes bash, bash requries an interactive session to run. Executing docker-compose up -d starts the container non-interactive, thus bash immediately exits. If you want an interactive session, execute docker-compose run hdinsight. Commented Jun 29, 2016 at 12:46
  • Pit is right. Sorry, my answer did not cater for the fact that you were trying to run interactively. Up doesn't do that. Commented Jun 30, 2016 at 22:38

5 Answers 5

76

You should scour the Compose file docs.

Most docker run commands have a compose equivalent and should all be listed there.

The background flag -d goes after run or up.

The tty flag -t and interactive flag -i are not required as docker-compose run does this by default. You can add tty to individual containers in the compose file with -t, but you cannot use interactive mode since you may start multiple containers simultaneously and can't interact with them all.

Regarding your situation, the command you're using should be working. If you add -d after the run command it will run in the background. But I recommend using up instead of run, as it will simply start all containers in the file rather than you have to specify hindsight.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm struggling with docker-compose as well, it seems that I can use docker-compose run to run something interactively with a tty, and it works fine, but if I start it detached with -d, no tty is created. For comparison, docker run -dti image /bin/bash does create a tty, and I can attach later. I did specify tty: true in the docker-compose.yaml file. So, it seems this answer is not valid when using -d, or am I doing something wrong?
It looks like you're right. When you docker-compose run a container with -d it doesn't create a tty, doesn't keep bash running, and thus the container stops. Strange that there'd be such a discrepancy between such similar commands.
This answer is simply wrong. See other answers by Ortomala and Anand. The correct answer is tty:true and stdin_open: true
42

As said by Anand Suthar, you have to use tty: true and stdin_open: true. Here is a minimal example:

services: alpine1: image: alpine tty: true stdin_open: true 

Start with:

docker-compose up -d 

Attach to a container with:

docker attach 268bcfb650fb 

and detach with ^P^Q.

Comments

20

From the Document

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...] Options: -d, --detach Detached mode: Run containers in the background, print new container names. Incompatible with --abort-on-container-exit. 

docker-compose up doc

the command should be

docker-compose up -d 

2 Comments

I must be using some strange version (1.24.0, build 0aa5906) of docker-compose since the "-d" option is not present and docker-compose -d up simply does nothing
@andrej I think the -d must place behind 'up', as options are placed at last
10

Today I am facing the same issue and below is how I manage.

I add tty: true kay value & stdin_open: true key value in 'docker-compose.yml' file as below and I am sure it will run in detach mode and one can also interact with console.

version: '2' services: hdinsight: image: hdinsight container_name: ABC volumes: - /etc/hadoop/conf:/etc/hadoop/conf - /usr/lib/python2.7/dist-packages/hdinsight_common:/usr/lib/python2.7/dist-packages/hdinsight_common - /etc/hive/conf/:/etc/hive/conf/ - /etc/tez/conf/:/etc/tez/conf/ - /usr/hdp/2.4.2.0-258/sqoop/lib/:/usr/hdp/2.4.2.0-258/sqoop/lib/ entrypoint: - bash labels: - "HDInsight client VM" tty: true stdin_open: true 

Comments

1

Rather than trying to attach to an existing container, this is now much easier using profiles introduced in 1.28.0.

Here's a basic example showing a normal service alongside a CLI container:

version: '3' services: db: image: mariadb:10 environment: - MARIADB_ROOT_PASSWORD=INSECURE volumes: - mariadb:/var/lib/mysql ports: - "3306:3306" cli: image: node:16 user: node volumes: - .:/app environment: - DATABASE_URL=mysql://root:INSECURE@db/prisma working_dir: "/app" stdin_open: true tty: true command: bash profiles: - cli volumes: mariadb: 

After running docker-compose up -d, docker-compose run cli will give you a bash shell inside of the container.

For more complex apps, consider overriding the entrypoint. In the nodejs, world, if you set it to npx, and leave command undefined, you can then run arbitrary npx commands with docker-compose run npx .... And for common tasks that are always non-interactive, you can drop the std_open and tty lines while leaving the profile set.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.