Learning Docker From ZerosteemCreated with Sketch.

in docker •  4 years ago  (edited)

This post is created to track my Docker learning growth from ZERO. I am learning this from Udemy courses "Docker, From Zero To Hero".

What is Docker?

Docker is a tool designed to make it easier to create, deploy and run applications, microservices using containers.

What is Containers?

We can consider container as one package that allows developers to package up an application with all its parts it needs such as libraries and other dependencies and ship it out.
Basically a container is isolated process inside of a sandbox and has a namespace (ie. id of the process)and a Cgroup to control it. Cgroup. Cgroup is a linux feature to limit, police, and account the resource usage for a set of processes.

Screenshot from 2020-06-09 16-08-40.png

Help Command for docker

docker help 

Command to run new container

docker run

Now lets create a new container which holds a Nginx webserver

docker run -d -p 1010:80 nginx:alpine

Screenshot from 2020-06-09 17-12-45.png

This command has created a container of Nginx webserver in 1010 port

Now lets remove the container and will be able to see at 1010 port the nginx webserver will not run anymore

docker rm -fv <containerid>

Screenshot from 2020-06-09 17-23-33.png

To get the above containerid run the below command

docker ps

Screenshot from 2020-06-09 17-20-54.png

What is docker image?
Image is a template where we find the all the binaries,all the libraries for applications. Containers comes from images. For every image a base should be there which is scratch. Scratch image is always base. On that base there will be the basics of the operating system ex. centos,ubuntu etc. On top of base image's operating system there can be multiple layer of process or services like nginx /apache. When the base is changed all the layers on top of it which are, eventually the all of the child of it, changes.

Difference of Container between Virtual Machines

Container is a sandbox that contains one or more processes.It has layers of infrastructure, OS,Docker installed on OS and process.
Virtual Machine has layers of infrastructure,Hypervisor that manages the VMs. Each one of the VMs need to have individual OS.

Screenshot from 2020-06-10 15-54-01.png

How to build an image?

Using a Dockerfile we can build an image. Dockerfile is a plain file where instructions of to create an image is defined.
It has few commands like below

FROM centos  #OS as a base
RUN yum install httpd -y #install apache webserver
CMD apachectl -DFOREGROUND #starting the apache library in foreground

How docker is structured?

We need to know about Docker host. Once we install docker in a machine it becomes docker host.Docker host is the house of all the containers. So a Docker host is capable to run containers and build images in it. Docker daemon is a server and that is the process that started once docker is installed. Then docker ecosystem has a client. Client talk to the server through an API. And the docker daemon gives an answer back through the same API to the client. So when we write commands in docker we are actually writing REST API calls to the server and the server is giving back its response. And the docker client manages the network, container, image, data volumes.

Screenshot from 2020-06-10 16-22-13.png

Layers of Image
Once an image is created it cannot be modified it can be only deleted. Image is immutable in nature. If we need to change something in image we need to create a new image and version it with an updated version and use the new read only image. Using an image n numbers of containers can be spin up. Once any of the container is deleted the image will exist.

Docker installation in Ubuntu

__Prerequisites __
OS requirements
To install Docker Engine, you need the 64-bit version of one of these Ubuntu versions:
Ubuntu Focal 20.04 (LTS)
Ubuntu Eoan 19.10
Ubuntu Bionic 18.04 (LTS)
Ubuntu Xenial 16.04 (LTS)
Docker Engine is supported on x86_64 (or amd64), armhf, arm64, s390x (IBM Z), and ppc64le (IBM Power) architectures.
Docker Engine on Ubuntu supports overlay2, aufs and btrfs storage drivers. Docker Engine uses the overlay2 storage driver by default. If you need to use aufs instead, you need to configure it manually.Look here for more details
https://docs.docker.com/engine/install/ubuntu/

Where are local docker images ?
Write down the following command to check the existing images

docker images

Screenshot from 2020-06-12 01-58-52.png

Delete images with below command

docker rmi <image_name>:<image_tag>

Screenshot from 2020-06-12 02-02-05.png

Build a custom image

create a folder in which we will create a custom docker image.

mkdir mydocker_images
cd mydocker_images
touch Dockerfile

The above code will create a folder named mydocker_images and inside that folder we will create a text file named Dockerfile.
Now we will write below code into this Dockerfile

FROM centos

RUN yum -y install httpd

The code above will create a base image from centos and after that apache will be installed into the docker container.
Now we will build our custom docker image

docker build --tag centos_apache:v1 .

So this above command will build a custom docker image which I have named as centos_apache and the version is v1.
If I don't pass the tag v1 or anything as specific version of this custom docker image it will build the image with latest tag.

Screenshot from 2020-06-15 12-55-57.png

Screenshot from 2020-06-15 12-56-19.png

Screenshot from 2020-06-15 12-56-31.png

Now to check the image has been built or not we will run the below command

docker images

This will show as below

Screenshot from 2020-06-15 13-06-50.png
Above we can see the image named as centos_apache is built with v1 tag.

Create a container using the custom image

docker run -d --name centos_container centos_apache:v1

Screenshot from 2020-06-15 23-56-34.png

The above command has created a container which has exited as there was no process command written in the Dockerfile.

Now lets edit the Dockerfile

FROM centos

RUN yum -y install httpd

CMD apachectl -DFOREGROUND

The last line will start the apache process in the foreground using the CMD

Now lets build another custom image using this updated Dockerfile

docker build --tag centos_apache:v2 .

Screenshot from 2020-06-16 00-02-42.png

Now lets run a new container with the updated custom image of version v2 and run it in port 9090 of my local machine

docker run -d -p 9090:80 --name centos_test centos_apache:v2

Screenshot from 2020-06-16 00-04-17.png

Now if I check my localhost in port 9090 i will be able to see apache is running on centos as below.
Screenshot from 2020-06-16 00-06-30.png

This is my 1st week of learning docker.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!