6

I’m trying to use docker-compose to bring up a container. As an ENTRYPOINT, that container has a simple bash script that I wrote. Now, when I try to bring up the container in any of the below ways:

  • docker-compose up
  • docker-compose up foo

it doesn’t complete. i.e., trying to attach (docker exec -i -t $1 /bin/bash) to the running container, fails with:

Error response from daemon: Container xyz is restarting, wait until the container is running.

I tried playing around with putting commands in the background. That didn’t work.

my-script.sh

cmd1 &
cmd2 &&
...
cmdn &

I also tried i) with and without entrypoint: /bin/sh /usr/local/bin/my-script.sh and ii) with and without the tty: true option. No dice.

docker-compose.yml

version: '2'
services:
  foo:
    build:
      context: .
      dockerfile: Dockerfile.foo
    ...
    tty: true
    entrypoint: /bin/sh /usr/local/bin/my-script.sh

Also tried just a manual docker build / run cycle. And (without launching /bin/sh in the ENTRYPOINT ) the run just exits.

$ docker build ... .
$ docker run -it ... 
... shell echos commands here, then exits
$

I'm sure its something simple. What's the solution here?

2
  • When your shell script terminates also the container will terminate. So putting commands in the background is not a good idea. Commented Mar 21, 2017 at 9:10
  • @Henry I tried with and without putting my last command in the background. But it turns out that my last command should be a blocking one: touch 1.txt && tail -f 1.txt. Commented Mar 21, 2017 at 15:41

2 Answers 2

4

Your entrypoint in your docker-compose.yml only needs to be

entrypoint: /usr/local/bin/my-script.sh

Just add #! /bin/sh to the top of the script to specify the shell you want to use.

You also need to add exec "$@" to the bottom of your entrypoint script or else it will exit immediately, which will terminate the container.

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

1 Comment

The exit immediately part really came in handy to perform error handling in a script of mine. Thank you.
3

First of all you need to put something infinite to keep running your container in background,like you can tail -f application.log or anything like this so that even if you exit from your container bash it keeps running in background

you do not need to do cmd1 & cmd2 &&...cmdn & just place one command like this touch 1.txt && tail -f 1.txt as a last step in your my-script.sh. It will keep running your container. One thing also you need to change is docker run -it -d -d will start container with background mode.If you want to go inside your container than docker exec -it container_name/id bash debug the issue and exit.It will still keep your container running until you stop with docker stop container_id/name Hope this help. Thank you!

1 Comment

Ok, yes. Putting in a blocking command (touch 1.txt && tail -f 1.txt) fixes that problem. Cheers all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.