1

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?

4
  • Take a look at the docs here. When you are not in detached mode, you can attach your STDIN, STDOUT, STDERR per the -a flag. Try passing that -a when you are in -d mode, you will be met with an error. Being in detached mode, your process is running in the background and is not outputting to your stdout. Commented Sep 22, 2018 at 21:46
  • 1
    If you run docker logs f8e72a22 you should see the output of your container, starting with your echo statement. Commented Sep 22, 2018 at 21:48
  • Good call @DavidMaze that's true. Commented Sep 22, 2018 at 21:48
  • Thanks to both of you. Especially for the logs command - that's a great one! Commented Sep 22, 2018 at 22:35

1 Answer 1

5

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

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

2 Comments

Thanks a lot, I understand it better now. I actually do want to run a standalone-script and see the output of it. However, before I can execute it, I already need an existing container, because I have to copy some files into it (and no, I can't do that with the Dockerfile). So my sequence is docker run -d ..., then docker cp ... and then docker exec -it ... my-standalone-script. Perhaps run -d is not the right way to create the container?
+1 for the answer here.. if you want to run more commands combined i suggest you sequence them in the dockerfile instead of passing them to docker cli

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.