I've used docker run -it
to launch containers interactively and docker run -d
to start them in background. These two options seemed exclusive. However, now I've noticed that docker run -dit
(or docker run -itd
) is quite common. So what is the difference? When -it
is really needed together with -d
?
Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container! Easy!
docker build builds a new image from the source code. docker create creates a writeable container from the image and prepares it for running. docker run creates the container (same as docker create ) and runs it.
RUN is an image build step, the state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of one another to build the image. CMD is the command the container executes by default when you launch the built image.
The ENTRYPOINT instruction looks almost similar to the CMD instruction. However, the main highlighting difference between them is that it will not ignore any of the parameters that you have specified in the Docker run command (CLI parameters).
Yes, sometimes, it's necessary to include -it
even you -d
When the ENTRYPOINT
is bash
or sh
docker run -d ubuntu:14.04
will immediately stop, cause bash
can't find any pseudo terminal to be allocated. You have to specify -it
so that bash
or sh
can be allocated to a pseudo terminal.
docker run -dit ubuntu:14.04
If you want to use nano
or vim
with any container in the future, you have to specify -it
when the image starts. Otherwise you'll get error. For example,
docker run --name mongodb -d mongo
docker exec -it mongodb bash
apt-get update
apt-get install nano
nano somefile
It will throw an error
Error opening terminal: unknown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With