Two questions about docker

General support questions
Post Reply
Nirel
Posts: 8
Joined: 2016/01/17 19:47:55

Two questions about docker

Post by Nirel » 2016/07/25 09:07:01

Hi,

I have installed docker for learning purposes.
I am trying to make it work 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:
1) 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?
If 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,
Nirel

aks
Posts: 3073
Joined: 2014/09/20 11:22:14

Re: Two questions about docker

Post by aks » 2016/07/25 16:49:08

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?
From memory, the same as --dettach (see https://docs.docker.com/engine/referenc ... ne/attach/ - the opposite of that - i.e.: detach from a tty)

why if I use a /bin/bash as the command it wont work?
You're confusing run and exec. This is to do with the ENTRYPOINT/CMD in your Dockerfile (or compose if used). So if I have a docker with an entrypoint of (say) httpd, that'll start with the run and if I "override" that with /bin/bash, the docker will stop once I exit the process, as it's terminated by the "overridden" /bin/bash which has now exited.

See: https://github.com/tldr-pages/tldr/blob ... /docker.md for a simple runtime "cheat sheet"

Post Reply