Docker

·

7 min read

Docker

What is Docker?

Docker is an open source platform for developing, shipping, and running applications or we can say docker is a platform or ecosystem about creating and running containers.

image.png

Why do we use docker?

So the main reason behind using docker is that it makes it very easy to run any software by just running a single command without worrying about installing any additional components.

Components of Docker

  • Docker Container
  • Docker Images
  • Docker Client
  • Docker Server
  • Docker Engine
  • Docker daemon

Image:

It is a collection of resources, deployments and configurations within a single file required to run a specific program.

image.png

Container:

Enables you to run a program with the help of images. It is a set of resources that enables users to use and run a program.

image.png

Docker Client:

It is a way to interact with docker. It allows you to perform operations like create, run and stop applications to a docker daemon. It sends the user requests to the docker server.

Docker daemon:

It is a background process to manage docker images, volumes, containers etc. It listens for the API requests and processes them.

Docker server:

It proceeds the requests of Docker clients and actually performs the operations.

How to install Docker :

Docker Installation Guide

Basic docker command :

docker run hello-world

So the above command is a basic command in Docker that will be executed in bunch of steps as follows:

image.png

So in the above process we run a command docker run hello-world. Now this command goes to the docker client and requests to pull the image hello-world. Then the client sends the request to the docker server where the server will check for the image in the memory of your computer. If server got the images of hello-world it will return to the command and run it but if the server is unable to find it locally on the system the it will go to a place i.e. Docker Hub to search for the same image and pull it from Docker Hub and then return to the main command and run it. It should also be noted that after running this command if you try to run it again then it is not going to pull the image from docker hub rather the image will be available on your system locally.

Docker hub:

It is the Hub of all the Docker images to pull and run them.

How does a container actually work?

image.png Look at the above diagram carefully Here you notice that in image section the image is containing two portione i.e.

  1. FS snapshot : File System snapshot to create a snapshot in container.
  2. Startup command : to run a program.

Now when you try to pull any image and run it, then in the container section a snapshot of the image will be created as shown and along that the startup command will execute to run the mentioned image.

Docker basic command layout:

image.png

Docker basic commands:

docker run busybox ls

image.png If we add a command after the image then the default commands in that image are not going to run, rather the command after the image will be executed as you can see in the above img.

Similarly,

docker run busybox echo hi there
docker run busybox echo bye there
docker run busybox echo how are you

image.png

image.png

From this you can get a better idea, just focus on the running process command in the container i.e. ls command.

But,

NOTE: Only those commands can be executed after the image which have their source in that image FS snapshot.

docker run hello-world ls

image.png

Here you got an error because the FS snapshot of the hello-world image doesn't include ls or echo commands in it.

image.png Here the hello-world image only contains a single file system.

Docker commands:

  • docker ps: It will list out the running containers.
  • docker ps –a : it will list out all the containers either running or not.
  • docker ps - -all : it will list out the history of the container ever created by the user i.e. which container was run at which time and at what time it stopped and all.

If you want to see a container running you can simply run a code as:

docker run busybox ping google.com

image.png This command is going to run continuously until you terminate it manually. So without terminating it open a new tab in terminal and type: docker ps And it will list out the running container i.e.

docker ps

image.png And now to terminate the previous command just go to the tab onto which that command is running and press ctrl+C. And your running container will be terminated.

Docker run vs docker create vs docker start:

docker run = docker create + docker start

image.png Let’s take an example to make it clear: First let’s say you want to create a docker image to run a container:

docker  create <image>

It will give you a container id:

image.png

Then if you want to run the container using this image so you will type:

docker start -a  <container id>

image.png

But if you want to do the two steps in just a single cmd you have to type :

docker run <image>

image.png So, it will first pull the image and then run it using a single cmd.

  • docker create = copying FS snapshot of image to the container.
  • docker start = initialising the running process by passing setup command of image to container.
  • docker run = copying FS snapshot + running the container.

image.png

Removing stopped containers:

If you want to remove stopped containers in your system just type:

docker system prune

image.png And here you will get a warning, just type y and you are good to go.

Docker logs :

You can get the output of a previously run container by using logs and it is pretty much simple just type :

docker logs <container id>

Stopping running container in docker :

If you want to stop a running container just type:

docker stop <container-id>

Of if the container is not terminating form above command you can use kill command:

docker kill <container-id>

Container command outside container:

If you want to run an container command outside the container then you have to use :

docker exec -it  <container-id>  <command>

image.png

To exit from this new terminal type : exit or ctrl+C

Use of -it : Here -it is used to provide an interactive terminal for the running container.

Command prompt in a container:

To open a shell in docker use :

docker exec -it  <container-id>  sh

It will open a shell command prompt.

image.png

Creating Docker images using Dockerfile :

Dockerfile: structured format that defines how the container should behave.

image.png

Basic Dockerfile:


FROM alpine

RUN apk add --update redis 

CMD ["redis-server"]

image.png

Base Image:

A base image is something that gives us an initial starting point for further process.

Why do we use alpine?

So Apline is a set of programs that are suitable to fulfil our needs of running that particular image.

RUN command:

Here the run command is nothing to do with docker rather it is used to install/update packages used for the images. Here apk stands for Alpine Linux package keeper.

CMD command:

Command that will run after the execution of Dockerfile.

Docker build process:

Now to build a container from the above dockerfile we just need to run a simple command :

docker build .

Here don’t forget to put a dot(.) at the end of the command.

After running above command you will get an output like this:

image.png Here note the container id at the last line of output. This one is the container id of the container that we have built using the above dockerfile.

Now to run this container just type:

docker run <container id>

image.png

Building a small Dockerfile to print "hi-there":

  • Make the Docker file using the same syntax as mentioned above:

image.png

  • Now run build the image using :
docker build .

image.png

Now run the container using the above image:

image.png

Specifying the name of image while building it:

If you want to add a name to your image while building it, just type :

docker build <image-name> .