Docker essentials-Docker commands for day to day use for beginners

 Useful docker commands for day to day use




Docker is awesome technology  in the virtualization world. If you are willing to learn docker or you just have started learning docker then you should know all these basics commands for docker which will be used in your daily routine. 

First we need to understand what is docker hub. Docker hub is an image repository where everyone pushes their images so that others can use those image. Suppose I want to use sql server on Docker then I'll download the image from docker hub and then I'll use it. 

Wait! did you just say SQL Server on docker? SQL server is only for windows. No, SQL server is now available to use on linux platform as well using docker.

To follow below tutorial make sure you have downloaded the docker and it's up and running.
You can open your terminal or CMD and try running any docker command. If it is not installed then you'll get command not found or if it's installed and docker is not running then also you'll get command not found.

How to download image from docker hub

When you say downloading image using docker then it means pulling the image using docker. Use below command to pull image. By-default docker uses DockerHub .

docker pull alpine

Using default tag: latest

latest: Pulling from library/alpine

Digest: sha256:e1c082e3d3c45cccac829840a25941e679c25d412c2fa221cf1a824e6a

Status: Image is up to date for alpine:latest

docker.io/library/alpine:latest


Docker image has been pulled. Let's check images on your system using below command.

docker images

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE

alpine       latest    bb3de5531c18   3 weeks ago   5.34MB

pankajdwivedi@Pankajs-MacBook-Air ~ % 




You can also use docker image ls command

docker image ls

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE

alpine       latest    bb3de5531c18   3 weeks ago   5.34MB



Running image inside container

docker container run --name alp -it alpine

/ # 



You can omit container. Writing container was old way. Now you don't have to write container to run it

docker run --name alp -it alpine 

/ # 


Here are some flags and keywords in above command:

  • run : giving command to run the container
  • --name alp : giving name to your container (It's always a better idea to give your container name)
  • -it : running container in interactive mode
  • alpine : is image name which you just pulled.
  • -d : detach mode 
Using detach will be running your container in background. You will not be logged in to your container.

docker run --name alp -dit alpine

a6ba34f88715cf3d63879872a65d6a7aeb48572ee7a79fb22ecf751049c49dc2 

To enter in a container's shell you need below command:

docker exec -it alp /bin/sh

/ # 

 Since we have started our alp container, let's check if it is running. Using docker ps

docker ps

CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS         PORTS     NAMES

a6ba34f88715   alpine    "/bin/sh"   4 minutes ago   Up 4 minutes             alp



docker ps -a will give you running as well as stopped containers. Use it accordingly.

How to check logs

check logs using docker logs container_name command.

docker logs alp 

/ # exit


How to start and stop container

docker start container_name_or_id and docker stop container_name_or_id

docker stop alp

alp


docker start alp

alp



How to inspect container

use docker inspect container_name_or_id

docker inspect alp

[

    {

        "Id": "a6ba34f88715cf3d638797a79fb22ecf751049c49dc2",

        "Created": "2021-09-19T08:04:17.202282Z",

        "Path": "/bin/sh",

        "Args": [],

        "State": {

            "Status": "running",

            "Running": true,


You can find all details about the containers here including IPaddress.

docker inspect alp | egrep "IPAddress"

            "SecondaryIPAddresses": null,

            "IPAddress": "172.17.0.2",

                    "IPAddress": "172.17.0.2",




Now very important , you always want to check how many images and containers are running on your system and how much space they are consuming. Use docker system df command

df command is similar to Linux system to check memory utilisation.

docker system df

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE

Images          1         1         5.337MB   0B (0%)

Containers      3         1         15B       10B (66%)

Local Volumes   0         0         0B        0B

Build Cache     0         0         0B        0B



How to check diff of container


There is also very important command to track changes to files or directories on container file system.


docker diff alp

C /root

A /root/.ash_history



How to remove all containers running.

first stop the running containers. 

docker stop $(docker ps -q)

a6ba34f88715



$(docker ps -q)

docker ps -q will give you container_id of running containers.

docker rm alp     

alp


This above will remove alp container.

Remove all stopped containers

docker container prune

WARNING! This will remove all stopped containers.

Are you sure you want to continue? [y/N] y

Deleted Containers:

803ec8603418b8e5ca7cbe9178724ca2714ab3fba48ac406282e78

7a8ee73bef1b766d38594ceeed8bdabf0cbc4e114faa65de71492a


Total reclaimed space: 10B



Remove all images

docker image prune

WARNING! This will remove all dangling images.

Are you sure you want to continue? [y/N] y

Total reclaimed space: 0B


Delete alpine image

docker image rm alpine

Untagged: alpine:latest

Untagged: alpine@sha256:e1c082e3d3c45cccahe679c25d438cc8412c2fa221cf1a824e6a

Deleted: sha256:bb3de5531c18f185667b0be0e400ab244440093de82baf4072e14af3b84

Deleted: sha256:ee420dfed78aeb92dbd73a3fbb59fa5dac4e04639210cc7905



Now check again the space using docker system command

docker system df      

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE

Images          0         0         0B        0B

Containers      0         0         0B        0B

Local Volumes   0         0         0B        0B

Build Cache     0         0         0B        0B



Now you can see no images, no containers are running and space was also reclaimed.

So these were my basics essential commands which could help you in debugging docker containers.
In next article I'll talk about volumes, networks and other stuff.


No comments:

Post a Comment

Quantum Computing: The Future of Supercomputing Explained

  Introduction Quantum computing is revolutionizing the way we solve complex problems that classical computers struggle with. Unlike tradi...