DEV Community

Aravind kumar TS
Aravind kumar TS

Posted on

Docker concepts with execution in Server

Is it possible to run multiple applications in a single Server
Yes, it is possible, but latency and performance is the drawback, hence Virtual Machine concepts using VMware came into picture but still it had some performance issues hence Docker-containerization came into picture.

Please check - Docker vs Virtual Machine (VM) - Understanding the Differences (geekflare.com)

What is Docker registry?
It is a place where all the Docker images of your project are stored using tags.
What is Docker hub?
It is a Cloud based system of Docker registry where you can store, share and test Docker containerized images.
What is a container?
It is a virtualized operating system where you run micro services, small- or large-scale application.
Can I run multiple applications in a single container?
It is possible, the best practice is to run single application per container.

A simple diagram that explains the difference between Virtual Machine Hypervisor and Docker container

Image description

Steps involved to install Docker

  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt-get install \ca-certificates \curl \gnupg \lsb-release - this command is to allow apt to use a repository over HTTPS
  4. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

    the above command is to add the GPG key for official Docker repository

  5. sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

the above command is used to add the stable repository

  1. sudo apt-cache policy docker-ce -> this command is to add docker repo

output -

Image description

  1. sudo systemctl status docker  to check the status of docker
    Image description

  2. sudo apt-get upgrade

  3. sudo service docker start

  4. sudo usermod -aG docker $USER  this command helps you to execute docker commands without using sudo
    Create a duplicate session now in your putty and login, close the old session and you can start executing docker commands without using sudo

  5. docker info  this command lists out the number of containers running, how many of them in stopped state etc
    The following are the 4 (four) terminologies involved in Docker

  6. Docker host -> Images, Container, Daemon, Volumes
    Daemon is the one that helps docker to create containers. It will first check the docker host for the images to run the container else it will fetch images from Docker Registry

  7. Docker Registry -> It can be an OS image or an application image such as nodejs,php

  8. Docker client - > docker build, run, pull, push

  9. Dockerfile -> this is a text file that we can use to create an image and assemble it
    Let’s see how to create a basic container

  10. docker run hello-world (please read the below screenshot carefully, this command first checks whether the hello-world image is available in local registry, its not available so it created a container from the Docker hub… Docker hub is Docker's official cloud-based registry)

Image description

  1. docker run –it ubuntu (this will create an ubuntu container from the image available in docker hub) We can now see the how many containers are created in our machine
  2. docker ps –a

Image description

  1. Let’s start the container now docker start container-id or docker start container-name For better understanding I created a container using centos using below command docker run centos  this create a new container using centos image from docker hub

Image description

In the above figure docker ps –a lists out the container status with the container id
To run the container, we can use the container id
docker run c3765e84473a
docker run 5258ae771fba
Let’s check the containers running in our machine, if the container is not being used it will exit
Image description

Lets make the containers run now

docker container start c3765e84473a

docker container start 5258ae771fba

docker exec -it 5258ae771fba /bin/bash  this command helps to login to a container
Output of above commands -

Image description

The command to list out the number of images in our docker machine is
docker images
Image description

The command to remove an image available in docker machine is
Docker rmi imagename –force  docker rmi hello-world –force
Output -
Image description

The command to check images available in dockerhub for a particular OS or application
docker search image name or applicationnanme
docker search php

Image description

The command to stop a running container is
docker stop containerid
docker stop 5258ae771fba
Image description

The command to start a container
docker start 5258ae771fba

Image description

Let’s install apache in our ubuntu container and commit the changes to the ubuntu image in our machine, lets login to the container first and install apache

docker exec –it 5258ae771fba /bin/bash
Lets do a update inside the container
apt-get update
apt-get install apache2
service apache2 start
service apache2 status

Image description

Image description

Image description

Let’s exit the container and take a snapshot of the image in which the container runs with apache2
shiva@hypo-cloudeva:~$ docker commit 5258ae771fba ubuntu-apache-april30-2022

in the above command “ubuntu-apache-april30-2022”  this is the snapshot name we are giving for this container/image now

Image description

Now a new snapshot of the ubuntu image is taken, you can see in the above snip.
Let’s see how can we map our local machine port with the container port
docker run –p 5000:80 ubuntu-apache-april30-2022
ubuntu-apache-april30-2022 – this is the name of our image that we built from the container
Let’s see how we can give a new name for our existing image, We need to create a login account in https://hub.docker.com.
I have created a username as mahadev5
The docker images command will list down the images present in our docker machine

Image description

I will choose the image “ubuntu” and create a tag name for it as ubuntupushtodockerhub

command to do the above is – docker tag ubuntu:latest mahadev5/ubuntupushtodockerhub:latest
mahadev5 – is my dockerhub username and ubuntu is my image name
ubuntupushtodockerhub:latest – this is my new tag name

Image description

Let’s see how we can push images to the docker registry now
docker push mahadev5/ubuntupushtodockerhub
output :

Image description

Let’s see how we can pull an image from docker hub registry
docker pull alpine
Here alpine is the image name located in docker hub

Image description

Let’s see about docker Storage

  1. Docker volume – this is mountable and used to store data in docker filesystem

  2. Bind mounts – this mounts a volume of our host machine to the docker container

Please refer google for the commands and execute them in your host machine

Docker Network
It helps us to create a connectivity between docker container and outside world, the below snip shows available network in our docker machine

Image description

We create a network in docker using the command
docker network create “network name”

Image description

You can use the network we created for a container of your choice

Home work for you guys?
Practice everything, we discussed in this document in your host machine.
Explore more about docker storage, docker network, docker swarm, docker file, docker compose.

Happy Learning!!

(The word document I created earlier is replicated here)

Top comments (0)