112

I see there are three docker commands that seem to do very similar things:

  1. docker build
  2. docker create
  3. docker run

What are the differences between these commands?

1

2 Answers 2

116
  1. docker build builds a new image from the source code.
  2. docker create creates a writeable container from the image and prepares it for running.
  3. docker run creates the container (same as docker create) and runs it.
78
  1. docker build . converts your Dockerfile into an image.
  2. docker create your-image creates a container from your image from step 1.
  3. docker start container_id starts the container from step 2.
  4. docker run image is a shortcut for 2. and 3. (docker create image and docker start container_id).

Here is the difference between image and container:

Image An image is a specified snapshot of your filesystem and includes the starting command of your container. An image occupies just disk-space, it does not occupy memory/cpu. To create an image you usually create instructions how to build that image in aDockerfile. FROM and RUN commands in the docker file create the file-snapshot. One may build an image from a docker file with docker build <dockerfile>

Container You can create new containers with an image. Each container has a file-snapshot which is based on the file-snapshot created by the image. If you start a container it will run the command you specified in your docker file CMD and will use part of your memory and cpu. You can start or stop a container. If you create a container, its not started by default. This means you can't communicate to the container via ports etc. You have to start it first. One may create an container from an image by docker create <image>. When a container has been created it shows the id in the terminal. One may start it with docker start <container_id>.

5
  • 1
    So docker create shouldn't work when there is no image available or if docker build wasn't run before?
    – winklerrr
    Commented Oct 6, 2020 at 8:04
  • 1
    @winklerrr thats right because you need to specify an image id as a parameter of the docker create command.
    – Adam
    Commented Oct 6, 2020 at 8:06
  • 1
    Does docker run execute a docker build command automatically if there is no image available?
    – winklerrr
    Commented Oct 6, 2020 at 8:22
  • 1
    @winklerrr as far as I know - no
    – Adam
    Commented Jan 18, 2021 at 16:40
  • 3
    I hope it's not too wordy. But it's better to say "You can create new container(s) with an image." than "One image may have multiple containers.". Containers don't belong to images.
    – p3nchan
    Commented Jul 13, 2021 at 10:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.