Bootstrap FreeKB - Docker - Pull an image and create a container using the docker create command
Docker - Pull an image and create a container using the docker create command

Updated:   |  Docker articles

This assumes you have installed Docker on Linux and Docker is running.

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

Before using the docker create command, you'll want to use the docker images command to return the list of images that are already installed, like this.

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
foo                 latest              105b54dc64f1        32 hours ago        196.7 MB

 

And the docker ps command to list the containers.

docker ps -a

CONTAINER ID     IMAGE              COMMAND                 CREATED         STATUS     PORTS     NAMES
d937372c09ab9    b939aa938add9913   "/docker-entrypoin..."  6 minutes ago   Created              my-container

 

Then, use the docker search command to find the image you want to install.

Let's say there is no image or container for "bar". The docker create command followed by the name of the image will both pull down the image and create a container. In this example, the "bar" image will be pulled down and the "bar" container will be created. Or, you could use the docker pull command to only pull down the image, but not create a container.

docker create bar

 

By default, the latest version of the image will be pulled down. Or, you can explicity include latest.

docker create bar:latest

 

Something like this should be displayed.

Unable to find image 'bar:latest' locally
Trying to pull repository docker.io/library/bar ...
latest: Pulling from docker.io/library/bar
xxxxxxxxxxxxxx: Pull complete
xxxxxxxxxxxxxx: Pull complete
xxxxxxxxxxxxxx: Pull complete
Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxx
...

 

Reissuing the docker images command should now show that the latest bar image has been pulled down.

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
foo                 latest              105b54dc64f1        32 hours ago        196.7 MB
bar                 latest              95ga44bc5582        2 weeks ago         967 B

 

The docker ps command should also show the container associated with the image.

docker ps -a

CONTAINER ID     IMAGE              COMMAND                 CREATED         STATUS     PORTS     NAMES
d937372c09ab9    b939aa938add9913   "/docker-entrypoin..."  6 minutes ago   Created              my-container

 


Name

The --name option can be used to give the container associated with the image a certain name. In this example, the container will be named my-container.

docker create --name my-container bar:latest

 

The docker ps command will show the name.

docker ps -a

CONTAINER ID     IMAGE              COMMAND                 CREATED         STATUS     PORTS     NAMES
d937372c09ab9    b939aa938add9913   "/docker-entrypoin..."  6 minutes ago   Created              my-container

 


Networking

The --network option can be used to configure the image to use a docker network, like this.

docker create --network=foo-network bar:latest

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter afc457 in the box below so that we can be sure you are a human.