What's the difference between the docker commands: run, build, and create

Docker

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

Solution 2 - Docker

docker build . converts your Dockerfile into an image. docker create your-imagecreates a container from your image, docker run your-image creates & starts the container based on your image.

Here is the difference between image and container:

Image An image is basically a specified snapshot of your filesystem and includes the starting command of your container. 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 A container is created by an image. One image may have multiple containers. Its file-snapshot 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>.

Finally docker run image is a shortcut for docker create <image> and docker start <container_id>.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeff WidmanView Question on Stackoverflow
Solution 1 - DockerNetworkMeisterView Answer on Stackoverflow
Solution 2 - DockerAdamView Answer on Stackoverflow