Introduction to Docker Part-2

Introduction to Docker Part-2

This is the second part of docker introduction. If you haven't read the part 1 then please read the first part and then continue. In the first part I mentioned about the Dockerfile, Docker Images, Docker Registries. Now we see how it works.

How to run Container on our system:-

Command

 docker run hello-world

Output Screenshot 2022-07-16 012402.png

Now we study every line printed here

  1. Unable to find image 'hello-world:latest' locally :- Meaning of this line is that it search the image named "hello-world" locally and because we don't have in our system so it printed unable to find.

  2. Latest: Pulling from library/hello-world :- Meaning that it is searching in the library for image name "hello-world". Library is nothing but Docker registries(Dockerhub).

  3. 2db29710123e: Pull complete :- Meaning after finding the image named "hello-world" in registry now it download to our local system.

    • One single image is made of different layers or files.
    • Images contains small part of OS (on top of which application is running), different dependencies. So it is possible that two different images needs some of the same dependencies.
    • If second image having some of the content same(in layers), then we don't need to download that whole image.
    • Instead we skip that same layer from downloading and use that same layer from our local system(Because it is downloaded already for the first image).
    • This makes docker faster.
    • Every layer has a hash like 2db29710123e.
    • Meaning of line is that layer having hash 2db29710123e is downloaded from registry.
  4. Container run at line Number 4. Container Id is represented as first few character of that hash.

  5. Status shows the image is downloaded or the version of that image.

  6. Next is the output from the container. Screenshot 2022-07-16 012551.png

  7. When we run the same command 2nd time then there is no need to download image from the registry. Hence container will run directly.

Whole process can be explained through this image:-

Screenshot 2022-07-18 170213.png

  • Client makes a request request.
  • It will search in local machine.
  • If not found then it will search in online registry and download it.

Structure & How to create Dockerfile :-

  • Create a file named "Dockerfile".
  • By default on building, docker search for "Dockerfile".
  • During building, commands in "run" section will execute from Dockerfile.
  • The commands in CMD section of Dockerfile will get executed after creating image from the container.

Example

FROM ubuntu
MAINTAINER Atharv Bobade <atharvbobade181@gmail.com>
RUN apt-get update
CMD ["echo", "Hello World!"]

Output :- Hello World!

This Maintainer line is not compulsory. and the RUN apt-get update just basically download the latest version of Image.

How to create Image from Dockerfile :-

  • First create a separate folder which contains only Dockerfile.
  • Change the directory and go inside that folder using cd command.
  • Then run command :-
    docker build -t imageName .
    

Basic Commands :-

docker pull ubuntu:15.02 (it will download specific version)
docker images (List of Docker Images)
docker run image (Creates a container from image)
docker rmi image (Deletes a Docker Image if no container is using it)
docker rmi $(docker images -q) (deletes all Docker images)
docker images -q --no-trunc (Listing the hash values of Images)

Container is the running instance of image.

Basic Commands Related to the Containers :-

 docker ps (list all containers)
 docker run ImageName/ID 
 docker start ContainerName/ID
 docker kill ContainerName/ID (Stops a running container) 
 docker rm ContainerName/ID (Deletes a stopped container)
 docker rm $(docker ps -a -q) (Deletes all stopped containers)

If you run image of ubuntu it will run and stops immediately. To use ubuntu as a OS you need to give -it tag(interactive environment) while starting the container.

docker run -it ubuntu

Thank you so much for taking your valuable time for reading

Inspired by Kunal Kushwaha tutorial's on Docker. I tried my best to give the basic idea of docker.

Feedback is highly appreciated!

Thank you !!

Did you find this article valuable?

Support Atharv Sunil Bobade by becoming a sponsor. Any amount is appreciated!