If you have read Part-1 we are clear:

  • We need docker image to create docker container
  • We can create our own image or use docker images made by others

Where do we find the images built by others:

The place where docker images are stored is called Docker Registry. Few widely used docker registries are:

  • Docker hub
  • AWS ECR (AWS Managed Service)
  • GCP container service (GCP Managed Service)

_Here, we will learn to run docker container pulling pubic docker images from DockerHub _(https://hub.docker.com/) for simplicity purpose.

From docker registry to docker container

fig: How images are pulled and runs as docker container

  • Search for the docker images in DockerHub (hub.docker.com). Type the name of the image you are looking for. For example: nginx, choose the one that best fits your requirement
  • Pull the image
  • Run it.

Note: When running docker container if the image is not found it will automatically look for the image and pull

Docker command to pull image

This command will pull public docker image named as nginx from docker registry.

docker pull nginx

If you want to pull specific version of docker image then we pass it after image name as shown in example bellow, and its called as tag. If no tag is provided then it will use latest as the default tag.

docker pull nginx:1.27.3-alpine
All the available tags will be listed in docker registry

docker command to run docker container from pulled image

docker run -itd --name mynginx nginx
Here:

  • itd refers to: interactive mode, terminal mode and detached mode. (more on this later)
  • mynginx refers to name of the container passed after --nameargument
  • nginx refers to the image name

Few must know docker commands:

Now, you are familiar with how to run docker containers from docker images but that's not enough.

Running a docker container

docker run -itd nginx

Checking running containers

docker ps

Checking locally available docker images

docker images

Assigning ports

docker run -itd -p 8000:80 nginx

Getting inside docker container for debugging or similar to sshing into server

docker exec -it <container-name / id > sh or bash

Checking docker logs

docker logs <container-name / id> -f

Inspecting for getting detailed information about container

docker inspect <container-name / id >

Stopping the container
stopping docker container when not required

docker stop <container-name/id>

Conclusion:

  • We need docker image to create docker container.
  • docker images are stored in registries.

Author Of article : taragurung Read full article