Nutritioustim
Nutritioustim

Reputation: 2738

Using custom shell script as docker entrypoint

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:

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?

Upvotes: 6

Views: 14233

Answers (2)

user4442960
user4442960

Reputation:

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.

Upvotes: 4

chintan thakar
chintan thakar

Reputation: 1500

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!

Upvotes: 3

Related Questions