Reputation: 3164
Assume this simple Dockerfile:
FROM debian:stretch
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod a+x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
And entrypoint.sh looks like this:
#!/bin/bash
echo yyyyyyyyyyyyyyy
exec "$@"
Now if I build the image and create the container in foreground, the entrypoint script is executed:
$ docker build . -t mytest
[...]
$ docker run --rm -it mytest /bin/bash
yyyyyyyyyyyyyyy
root@3e3d7290b09c:/#
But if I create the container in detached mode, it is not executed:
$ docker run --rm -d -it mytest /bin/bash
f8e72a222c5194f61843569ae76798bb09736fa4205b93e484f11de32df4db64
Why is that? Or, more importantly, how can I create a detached container, where the entrypoint script is executed?
Upvotes: 1
Views: 3454
Reputation: 2141
If you take a look at the docker docs
-d=false: Detached mode: Run container in the background, print new container id
Detach modes are generally made for services not standalone scripts if you want to see the output. You can use detached mode on standalone scripts if you don't want to see the output. Services that should be ran in detached mode would include databases, web servers, est. Not one time ran scripts that go and quit.
Your container runs in the background then quits because the script has ended. Since -d
option only prints out the container id that is all you will see
Upvotes: 5