Docker series for beginners: How to debug your first container part 1

 Today we are going to learn how to debug your container on docker. Sometimes when you run a container ,your command gets executed successfully but you identify that your container is not running. 




What could be the issues that container is not running or exited abruptly ,we'll see today:

If you haven't gone through previous article where I explained how to run your first container then I'll suggest plz go through that article then start on this page. Here is the link below:

Running first container on docker

Now I am assuming you already know how to pull the image from docker hub and how to run it inside docker container.

Example: 

I am going to run alpine linux image I downloaded on my system. Now I'll use the inspect command to check whats inside the image. First check what are the images and out of those image which one we want to inspect.

docker image ls

docker image ls

REPOSITORY                       TAG           IMAGE ID       CREATED       SIZE

alpine                           latest        bb3de5531c18   2 weeks ago   5.34MB

mcr.microsoft.com/mssql/server   2019-latest   56beb1db7406   7 weeks ago   1.54GB

I have two images now I want to inspect the Alpine image. You can use image name or image id to inspect.

docker image inspect Alpine

or

docker image inspect bb3de5531c18

docker image inspect bb3de5531c18

[

    {

        "Id": "sha256:bb3de5531c18f185667b0be0e400ab24aa40f4440093de82baf4072e14af3b84",

        "RepoTags": [

            "alpine:latest"

        ],

        "RepoDigests": [

            "alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a"

        ],

        "Parent": "",

        "Comment": "",

        "Created": "2021-08-27T17:39:33.633925527Z",

        "Container": "789638f977e407396e28e5c30280983578253237755af8

Just go through all the details. Now let run this image. I am going run this image in container with errors.

So I am going to write a wrong command below:

docker run alpine 

docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

No container is running. Strange that's how we run the container. Lets check the container logs if we see something. First check how many containers are in running state or stopped. If we use below command we'll know.

docker ps -a

docker ps -a

CONTAINER ID   IMAGE                                        COMMAND                  CREATED          STATUS                      PORTS     NAMES

ac001968f4fc   alpine                                       "/bin/sh"                2 minutes ago    Exited (0) 45 seconds ago             confident_heisenberg

Now take the container id and check the logs.

docker container logs 71f2364b452c

$

Nothing is there in logs. Let's inspect the container.

docker container inspect 71f2364b452c

[

    {

        "Id": "71f2364b452c57011481cc6c696e5fdae0af0b31ca8f404f899d474acbe",

        "Created": "2021-09-15T07:26:10.800075211Z",

        "Path": "/bin/sh",

        "Args": [],

        "State": {

            "Status": "exited",

            "Running": false,

            "Paused": false,

            "Restarting": false,

            "OOMKilled": false,

            "Dead": false,

            "Pid": 0,

            "ExitCode": 0,

            "Error": "",

No error!

Now think, what we are trying to do? We are trying to run the Alpine Linux and we need to enter into the Alpine shell. But we are not doing it and then logs were not generated. 

Let's do it right way.

docker run -it alpine /bin/sh 

docker run -it alpine /bin/sh        

/ # whoami

root

/ # ls

bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var

/ # ps

PID   USER     TIME  COMMAND

    1 root      0:00 /bin/sh

   10 root      0:00 ps

/ # 

Check logs:

docker container logs 5cb4aaa4e5c5


docker container logs 5cb4aaa4e5c5

/ # whoami

root

/ # ls

bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var

/ # ps

PID   USER     TIME  COMMAND

    1 root      0:00 /bin/sh

   10 root      0:00 ps

/ # exit

Logs are rolling now.

We have successfully learned what to check when something is not wrong. Always check for docker logs, docker container inspect and image inspect also would be helpful.

Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link

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...