run
command creates a container from the image and then starts the root process on this container. Running it with run --rm
flag would save you the trouble of removing the useless dead container afterward and would allow you to ignore the existence of docker start
and docker remove
altogether.
run
command does a few different things:
docker run --name dname image_name bash -c "whoami"
docker ps
bash -c "whoami"
. If one runs docker run --name dname image_name
without a command to execute container would go into stopped state immediately. docker remove
before launching container under the same name.How to remove container once it is stopped automatically? Add an --rm
flag to run
command:
docker run --rm --name dname image_name bash -c "whoami"
How to execute multiple commands in a single container? By preventing that root process from dying. This can be done by running some useless command at start with --detached
flag and then using "execute" to run actual commands:
docker run --rm -d --name dname image_name tail -f /dev/null
docker exec dname bash -c "whoami"
docker exec dname bash -c "echo 'Nnice'"
Why do we need docker stop
then? To stop this lingering container that we launched in the previous snippet with the endless command tail -f /dev/null
.