I see there are three docker
commands that seem to do very similar things:
docker build
docker create
docker run
What are the differences between these commands?
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 as docker create
) and runs it.docker build .
converts your Dockerfile
into an image.docker create your-image
creates a container from your image from step 1.docker start container_id
starts the container from step 2.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>
.
docker create
shouldn't work when there is no image available or if docker build
wasn't run before?
Commented
Oct 6, 2020 at 8:04
docker run
execute a docker build
command automatically if there is no image available?
Commented
Oct 6, 2020 at 8:22
docker build
,docker create
,docker run