Learning Docker As a Newbie: Part 2

Photo by Ian Taylor on Unsplash

Learning Docker As a Newbie: Part 2

Docker Images, Containers, Volumes

Brief

Before Getting Started with this, I would highly recommend folks to check my First Blog in Docker. There I have explained why we need Docker and What Problems it solves at the industry level. You can also check a blog on this. In this blog, I will be explaining Docker in more depth which includes Docker Images, Containers etc.

Docker Images

In the case of running an application, we must need its dependencies and configuration files. Now we know in the case of Docker, applications are run in Docker Containers. So how are we going to get all the dependencies or configuration files to run the code? This is where Docker Images comes into the picture.
Docker Image is a Package which contains application/software binaries with all its dependencies and configuration files needed to run the code. Also, it is just a read-only template which is immutable.
A Docker Image can have a series of layers. Every layer can represent an instruction that we run in it.
Here's another question where are these images stored?
These Images are stored in a Docker Registry like DockerHub, Elastic Container Repository, etc. A Docker image will have a base OS layer in it always, and we can install software and its dependencies above the underlying OS layer in the image.

Docker Registry

Docker registry is just a storage for Docker Images. These are kept in the form of repositories, where it has different versions of that specific image. Default Docker engine talks with DockerHub (Docker Public Registry Instance).
Sometimes you don't want your repositories to be public, in DockerHub you can have only 1 private image repository. But in real-world scenarios, you might need more private registries. It is possible to run on-premise private repositories.
Example -> Amazon Elastic Container Registry, Google Container Registry, Azure Container Registry. You can visit all the official Docker images like Ubuntu, Nginx, etc in DockerHub.
Now to pull any Docker image you can use this command:

docker pull <Docker Image Name>

By default, it will pull the latest image present in the DockerHub repository. For example -> if we do docker pull ubuntu it will pull the image with the tag latest. Now to pull a particular version of an image for that we need to add that specific tag, like docker pull nginx:alpine it will pull the alpine version of the nginx image.

Docker image commands

There are a few commands which are used quite often while dealing with Docker images:

  1. For viewing the images on your local you can run this command:

    docker image ls
    

    This command will list all the images available in the local system. image.png

  2. If we want to check the history of each layer in the images :

    docker history <image_name>
    

    image.png

  3. If you want to remove an image we can use the following command:

    docker image rm <image_name>
    

    There's another command:

    docker rmi <image_name>
    
  4. We can get cases where we want to get rid of many unused images, which can be removed using:
    docker image prune -a
    
    Now By default, you will be prompted to continue. In this case, use the -f or --force flag. We can limit which images are pruned using filtering expressions with the --filter flag. Example -> docker image prune -a --filter "until=24h"
  5. We can tag an image by using this:
    docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
    
  6. We can push an image to the DockerHub repository:
    docker image push <image>
    
    Remember to log in before pushing the images using docker login -u <username>.

Docker Containers

A Container is a runtime instance of a Docker image. It is the actual instantiation of the image just like Classes and Objects. We can create any number of containers from a single Docker image. Docker containers cannot change their underlying docker image. The best thing I like about containers is that containers are:

  • Lighter
  • Faster
  • Efficient than VMs
    For more info check out the official documentation of Docker.

    Docker Container Commands

    Here are some important Docker Commands:
  • If you want to run a Container you can use:
    docker container run <Image name>
    
    of
    docker run <image name>
    
    An example can be ->
    docker run ubuntu
    
    It will get the latest image of ubuntu from DockerHub and run the container.
  • We can run the container in the interactive mode:
    docker run -it ubuntu
    
    It may not work for the first time for the ones who don't have iputils or figlet installed in their system. To install it u can follow the below instructions:
    apt update -y
    
    apt-get install -y iputils-ping
    
    apt-get install figlet
    
    This helps you run the container in the interactive mode
  • Most of the time you may want our container run in a specific PORT like PORT-80 or PORT-8080. To configure that we can use the -p flag to specify the PORT
    docker run -p HOSTPORT:CONTAINERPORT
    
    Example ->
    docker run -p 80:3000
    
  • If you want to run a container in the background you can use the -d flag to run in detached mode.
    docker run -d ubuntu
    
    The container will run in Detached mode.
  • If you want to add some environment variables to your container then you can add them using the -e flag.
    docker run -it -e  HOSTNAME=<container_name>
    
  • There will be a time you want to stop the container then we can run:
    docker container stop <container_id>
    
  • To get the list of all containers:
    docker ps
    
    It will list all the running containers
    docker ps -a
    
    Lists all the containers (running and exited) with unique names
    docker ps -a -q
    
    Lists the container IDs only.
  • You might want to specify a name for your container this can be achieved using --name flag.
    docker run -d --name <name> <imagename>
    
  • To remove a container we can use
    docker rm <container_id>
    
    OR
    docker rm <container_name>
    If you want to remove a running container then you need `-f` command to forcefully stop the container
    
    docker rm -f
    To remove all the containers we can use this:
    
    docker rm -f $(docker ps -a -q )
    To remove all the exited containers:
    
    docker rm -f $(docker ps -a -q -f status=exited) ```
  • If you want to look at a specific process in a specific container
    docker top <container>
    
  • To check all the configs of the container we can use :
    docker inspect <container_name>
    
  • If you want to check all the logs of the container :
    docker logs <container_name>
    
    If you want to check the live logs of the container
    docker logs -f <container_name>
    
  • If you want to get live info on CPU, Memory Usage or the I/O of the containers then we need :
    docker stats
    

Conclusion

In this Blog, I have covered almost all the important commands required in Docker Images and Containers. In the next Part, I will be covering the remaining important topics of Docker like Docker Volumes, Network, and DockerFile. Feel free to share any feedback.

Did you find this article valuable?

Support Arnab Maity by becoming a sponsor. Any amount is appreciated!