Docker’s Basic Tutorial

 

Step 1) Create account in Docker Hub, url:- https://hub.docker.com/ (Remember Docker ID and Password)

 

Step 2) Install Docker in your system, different installer for multiple OS. url:- https://docs.docker.com/get-docker/

 

Step 3) Login into Docker app using Docker hub account(using Docker ID and Password)

 

Step 4) Open Win PowerShell in Admin mode (I don’t know about MAC....But I am sure there will be something to run commands)

 

Step 5) Now to check installed Docker version

Run cmd => "docker --version"

Result => "Docker version 20.10.7, build f0df350"

Note => You might get different version, depends on what is installed at current time.

 

Step 6) Now let’s try to run an image....but wait I don’t have any image thing in my system....So How can I?...Solution:- Simply run below cmd.

Run cmd => "docker run hellow-world"

Result => Unable to find image 'hellow-world:latest' locally docker: Error response from daemon: pull access denied for hellow-world, repository does not exist or may require 'docker login': denied: requested access to the resource is denied. See 'docker run --help'.

Note => So here we intentionally gave wrong image name "hellow-world" which is not available in Docker Hub current time. By the way it will first check locally then will go for Docker hub.

 

Step 7) Now let’s try with available Docker image of "hello-world"

Run cmd => "docker run hello-world"

Result => Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

b8dfde127a29: Pull complete

Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c

Status: Downloaded newer image for hello-world:latest

Hello from Docker!

This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

 1. The Docker client contacted the Docker daemon.

 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

    (amd64)

 3. The Docker daemon created a new container from that image which runs the

    executable that produces the output you are currently reading.

 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

 https://hub.docker.com/

For more examples and ideas, visit:

 https://docs.docker.com/get-started/

Note => here also it will first check locally then will go for Docker hub and this time we got image. But this "docker run hello-world" cmd not only pulls image but also creates container also. for details read Results yellow highlighted part.

 

Step 8) now we will check how many images de we have in local

Run cmd => "docker images"

Result =>

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE

hello-world   latest    d1165f221234   3 months ago   13.3kB

Note => for you the details might be different. We have container for it right (See Step 7). But how to pull only image without making container. Let’s see it in next step.

 

Step 9) Here we will see how to pull only image of Ubuntu.

Run cmd => "docker pull ubuntu"

Result =>Using default tag: latest

latest: Pulling from library/ubuntu

c549ccf8d472: Pull complete

Digest:sha256:aba80b77e27148d99c034a987e7da3a287ed455390352663418c0f2ed40417fe

Status: Downloaded newer image for ubuntu:latest

docker.io/library/ubuntu:latest

Note => we pulled ubuntu image, for confirmation you can run cmd "docker images".

 

Step 10) it’s time to create container form Ubuntu image.

Run cmd => "docker run -it -d ubuntu"

Result=>360b47ece225145ca4de87f3d294fd0f9a64599d5248deb8567debbb3195d0df

Note => we have created ubuntu container.

 

Step 11) If you want to see created containers details then run below cmd.

Run cmd => "docker ps -a"

Result =>

CONTAINER ID   IMAGE         COMMAND    CREATED             STATUS                         PORTS     NAMES

360b47ece225   ubuntu        "bash"     58 minutes ago      Up 58 minutes                            ecstatic_joliot

63e18d21c924   hello-world   "/hello"   About an hour ago   Exited (0) About an hour ago             stoic_tesla

Note => Here we have two images ubuntu and hello-world with contianer "360b47ece225" and "63e18d21c924" respectively.

 

Step 12) It’s time to execute container and run some commands {Ubuntu container Id: 360b47ece225 and cmd type : bash}

Run cmd => "docker exec -it 360b47ece225 bash"

Result =>

root@360b47ece225:/#

Note => We got "root@360b47ece225:/#" which is a ubuntu terminal to execute command. So you can play with it by printing hello and exit from terminal by using ubuntu terminal cmd "exit" as below.

root@360b47ece225:/# echo hello

hello

root@360b47ece225:/# exit

exit

 

Step 13) How to stop running container? (Used container ID)

Run cmd => "docker stop 360b47ece225"

Result =>360b47ece225

Note => It stoped "360b47ece225" container which was ubuntu container.

 

 

Step 14) What if you have made some changes in your container and want to save a seperte copy? See below...

Run cmd => "docker commit 360b47ece225 rajmote/ubuntu"

Result=>sha256:b814f546463f63d664d094df012ed9679d6e16027199da5bc003a18b752ec526

Note => So here we are creating image of our ubuntu container "360b47ece225" in which we might have done some changes. And saving it in locally. To see newly created image copy run cmd=> "docker images"

 

Step 15) As we can see we have our new ubuntu image locally. Now lets see how to push it to docker hub.{Make sure you are logged into docker hub.... see Step 3) Login into Docker app using dockerhub account }

Run cmd => "docker push rajmote/ubuntu"

Result =>Using default tag: latest

The push refers to repository [docker.io/rajmote/ubuntu]

2eac9d49f440: Pushed

feef05f055c9: Mounted from library/ubuntu

latest: digest: sha256:71fb3ed3424aff1f82705556d94e2a1292fd65482752d78e7fbf854139c784c9 size: 736

Note => We have pushed it successfully.

 

Step 16) Now lets see how to remove container? {Used container Id:- 360b47ece225}

Run cmd => "docker rm 360b47ece225"

Result =>360b47ece225

Note => We have removed 360b47ece225 container successfully. To confirm it run cmd => "docker ps -a"

 

Step 17) Similarly we can remove image also (Only diff is "rm" for Containers and "rmi" for Images).(Used Image Id:- b814f546463f}. If you are not sure then first run "docker images" cmd and pickup desirable image id.

Run cmd => "docker rmi b814f546463f"

Result =>Untagged: rajmote/ubuntu:latest

Untagged: rajmote/ubuntu@sha256:71fb3ed3424aff1f82705556d94e2a1292fd65482752d78e7fbf854139c784c9

Deleted: sha256:b814f546463f63d664d094df012ed9679d6e16027199da5bc003a18b752ec526

Deleted: sha256:1119a46d4ace0e1428bf4ef53c54ff3beaf847d3dcb14683c9fa9d8bd0ac8912

Note => We have removed b814f546463f image successfully. To confirm it run cmd => "docker images"

 

Step 18) Now its time to do a small and simple demo for that I have provided blog and video url below.

Blog Url :- https://thatdevopsguy.medium.com/how-to-create-a-static-web-server-for-html-with-nginx-99bf8226bce6

Youtube Video Url :- https://www.youtube.com/watch?v=qxPdd-geqqA

 

Step 19) While going through above steps, commands, error you might encounter some keywords........please see below explanation for them :-

 

1) The Docker daemon

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

 

2) The Docker client

The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

 

3) Docker registries

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry.

When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

 

4) Docker objects

When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

 

5) Images

An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

 

6) Containers =>

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.

Comments

Popular posts from this blog

How to Transpose DataTable in C# ?

Multiple Inheritance in C#

Process Template Editor Extension Tool in Visual Studio Integration Tutorial