What's the difference between the docker commands: run, build, and create
-
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 asdocker create
) and runs it.
docker build .
converts your Dockerfile
into an image. docker create your-image
creates 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>
.