468

In practice to start a container I do:

docker run a8asd8f9asdf0

If thats the case, what does:

docker start

do?

In the manual it says

Start one or more stopped containers

Morten Siebuhr
  • 6,068
  • 4
  • 31
  • 43
mskw
  • 10,063
  • 9
  • 42
  • 64

6 Answers6

558

This is a very important question and the answer is very simple, but fundamental:

  1. Run: create a new container of an image, and execute the container. You can create N clones of the same image. The command is: docker run IMAGE_ID and not docker run CONTAINER_ID

enter image description here

  1. Start: Launch a container previously stopped. For example, if you had stopped a database with the command docker stop CONTAINER_ID, you can relaunch the same container with the command docker start CONTAINER_ID, and the data and settings will be the same.

enter image description here

Wild Pottok
  • 318
  • 3
  • 8
daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • 9
    Does a volume need to be created for the stopped container for the data to persist? – Logan Phillips Mar 20 '19 at 04:55
  • 3
    @LoganPhillips Lifecycle difference between files written to container's default _union_ filesystem layer and files written to volumes is: container's union filesystem layer data is always lost when removing the container (`docker rm container_id`). On the other hand, volumes data survive container's removal unless `-v` option is explicitly supplied on the command line. Volume location on the host system can be directly inspected. [See this article](https://container-solutions.com/understanding-volumes-docker/) – Stphane May 11 '19 at 22:46
  • About process, need `&` at the end of the `run` command line? I need it as a countinous (24 hours) service – Peter Krauss Nov 14 '19 at 23:54
  • So then what is the purpose of "docker create"? How/when would that be used? – Joseph Gagnon Jan 28 '20 at 19:41
  • 1
    When you do `docker run hello-world` it prints "Hello from Docker!" but when you create a container by doing `docker create hello-world` and then start the container it won't print that output. What's happening there? – Chamithra Thenuwara Jul 31 '20 at 19:46
  • @JosephGagnon: per Fumiski Wells comment above: run = create + start . Per the doc: "command creates a writeable container layer over the specified image and prepares it for running the specified command. This is similar to docker run -d except the container is never started." I personnally mostly use docker run from an image, but it's good to have more freedom as this just enable more use cases :). – codea Apr 04 '21 at 07:33
  • 1
    @ChamithraThenuwara: Create command just prepares the container. It does not run it. – codea Apr 04 '21 at 07:38
138
  • run runs an image
  • start starts a container.

The docker run doc does mention:

The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.

That is, docker run is equivalent to the API /containers/create then /containers/(id)/start.

You do not run an existing container, you docker exec to it (since docker 1.3).
You can restart an exited container.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Tarik don't you have to add `-i` to `docker run` for an interactive process? I mean, docker run needs an image to run a container. – VonC Nov 18 '16 at 21:22
  • 14
    There seems to be a room to somebody to write a more elaborate response. The answer does not look very explanatory. – qartal Mar 02 '17 at 01:11
  • 4
    Next question for me was whats-the-difference-between-a-container-and-an-image http://stackoverflow.com/questions/21498832/in-docker-whats-the-difference-between-a-container-and-an-image – Alex Punnen Mar 22 '17 at 11:11
  • and how is `docker exec` different? – Sohail Si Jun 21 '21 at 15:52
  • 1
    @SohailSi [`docker exec`](https://docs.docker.com/engine/reference/commandline/exec/) is for running command in a running container. – VonC Jun 21 '21 at 16:53
  • 1
    Thank you. I keep forgetting that. It is good that it is now mentioned here. – Sohail Si Jun 24 '21 at 09:14
25

Explanation with an example:

Consider you have a game (iso) image in your computer.

When you run (mount your image as a virtual drive), a virtual drive is created with all the game contents in the virtual drive and the game installation file is automatically launched. [Running your docker image - creating a container and then starting it.]

But when you stop (similar to docker stop) it, the virtual drive still exists but stopping all the processes. [As the container exists till it is not deleted]

And when you do start (similar to docker start), from the virtual drive the games files start its execution. [starting the existing container]

In this example - The game image is your Docker image and virtual drive is your container.

Shubham
  • 2,847
  • 4
  • 24
  • 37
Apb
  • 270
  • 4
  • 9
  • When you do `docker run hello-world` it prints Hello from Docker! but when you create a container by doing `docker create hello-world` and then start the container by doing `docker start 27c833038489` it won't print that output. What's happening there? – Chamithra Thenuwara Jul 31 '20 at 19:47
  • @ChamithraThenuwara:simply, a container always monitors a task, if tasks stops, container stops. ex: you start a windows server core image, and you run cmd: container will just wait for the process to complete, thus nothing happens until you kill it. So, in your case, you need to look at what is the task the container you start is doing and there are many ways to do that, and they can supersede each others via command line -it / run ...[COMMAND] --, dockerfile [CMD] or docker-compose.yaml entrypoint:. It's a bit complicated at first, but this is where it because so powerful and re-usable! – codea Apr 04 '21 at 07:46
  • 4
    @ChamithraThenuwara: because `docker run` by default runs in foreground (use `-d` to detach it), whereas `docker start` by default runs in background (use `-a` to run in foreground). So, `docker start -a 27c833038489` WILL print the _Hell from Docker_ message. So much for consistency, eh? – marc.guenther May 11 '21 at 13:35
20

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.

enter image description here

run command does a few different things:

docker run --name dname image_name bash -c "whoami"
  1. Creates a Container from the image. At this point container would have an id, might have a name if one is given, will show up in docker ps
  2. Starts/executes the root process of the container. In the code above that would execute bash -c "whoami". If one runs docker run --name dname image_name without a command to execute container would go into stopped state immediately.
  3. Once the root process is finished, the container is stopped. At this point, it is pretty much useless. One can not execute anything anymore or resurrect the container. There are basically 2 ways out of stopped state: remove the container or create a checkpoint (i.e. an image) out of stopped container to run something else. One has to run 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.

y.selivonchyk
  • 8,987
  • 8
  • 54
  • 77
  • 1
    By far, this is the best answer I've read. Not only answers to OP question, but explain some very useful tipical use cases in a very small space and with examples. For me, that I'm just starting with this, it did resolve many questions I'd have. Thanks! – Fer B. Mar 04 '21 at 08:18
  • 1
    I agree that this is an excellent answer. However, I am confused by the statement "Once the root process is finished, the container is stopped. At this point, it is pretty much useless. One can not execute anything anymore or resurrect the container. " -- I thought one can `docker start` it again, is that not so? – András Aszódi Oct 18 '21 at 15:55
12

daniele3004's answer is already pretty good.

Just a quick and dirty formula for people like me who mixes up run and start from time to time:

docker run [...] = docker pull [...] + docker start [...]

conan_z
  • 460
  • 6
  • 17
  • 4
    This is not quite true. According to the official doc, `"...docker run is equivalent to the API /containers/create then /containers/(id)/start.` (source: https://docs.docker.com/engine/reference/commandline/run/) – sshh Oct 16 '19 at 02:30
0

It would have been wiser to name the command "new" instead of "run".

Run creates a container instance of an existing (or downloadable) image and starts it.

Juergen Schulze
  • 1,515
  • 21
  • 29