Docker Community Forums

Share and learn in the Docker community.

[Solved] Two questions about docker

(Nirel) #1


I have installed docker for learning purposes on my Linux machine.
I am trying to make it work with nginx server i pulled but it only works under one condition (using no command)

I used “docker pull nginx” to try having a nginx server inside my docker,
after it pulled it I ran 4 tries only working with the first two.

docker run --name=nirel -it -v /var/www:/var/www -p 80:80 -d nginx (working)
*I now stopped the container and removed it and tried:
docker run --name=nirel -it -v /var/www:/var/www -p 80:80 nginx (also works)

*For both cases I saw they get a default command of nginx -g (and some text).

I also tried (after stopping the last one and deleting it):
docker run --name=nirel -it -v /var/www:/var/www -p 80:80 -d nginx /bin/bash (doesn’t work)
docker run --name=nirel -it -v /var/www:/var/www -p 80:80 nginx /bin/bash (doesn’t work)

so I have 2 questions:
I understand -d makes true for using the container as a daemon, but why
it works both with it and without it? what does the -d actually does?
2) why if I use a /bin/bash as the command it wont work?
i use /bin/bash and attach my container, then using service nginx start
it does work.But after I am exiting the container and starting it again
it doesn’t work again, and if I am attaching it i can see the service
(nginx) is stopped so why?

Thanks in advance,

(David Maze) #2

(I’m not totally clear what you mean by “doesn’t work”, but it’s clear what the commands you typed should do:)

What you should see, in both cases, is for the nginx image to run its default command (as you say, nginx -g). If you don’t specify -d then it will run in the foreground and whatever nginx prints to its stdout will be printed to your terminal. If you do specify -d then docker run will return immediately, nginx will keep running in the background, and you’d need to use docker logs nirel to see its stdout.

I’m going to guess that, by “doesn’t work”, you mean “nothing is being served on port 80”. A container runs only one command. If you tell it to run a shell, it runs instead of the Web server. The version of the command you posted, without -d,

docker run --rm -it -v /var/www:/var/www nginx /bin/bash

should get you an interactive shell looking at the container’s initial filesystem

It surprises me that that would work. I’d usually expect commands like “service”, “initctl”, or “systemctl” to not work at all inside a Docker container; they’re not the usual way to do things, and most containers don’t run an init system they would support.

If you do need to debug the startup, it’s pretty common to run bash in the way you showed, then manually run the command (nginx -g) at the shell prompt.

(Nirel) #3

Thank you for the great explanation,
I think i got it now, and also now I understand why it works and what the -d is for
thanks!!! :slight_smile: