DEV Community

loading...
Cover image for Deep dive into docker

Deep dive into docker

rahulku48837211 profile image Rahul Kumar Updated on ・4 min read

What is docker ?

In today's era due to the rise of the microservices need of containerizing applications also increases. Container allows us to package our application with all the libraries and binaries it's needed to run . Docker is one of popular tool which helps to build containerization applications .

Docker

Docker Engine

Docker Engine is a client-sever application with these components:

  • A server with long-running program called daemon.
  • A rest API which establish the communication between the client and host .
  • A command line interface .

Docker Engine

Advantages of Docker and Container :

  • efficient use of system resources.
  • microservice architecture.
  • Provides loose coupling .
  • high performance applications.
  • secure and scalable.

Containers and Virtual machines :

  • A container share the kernel of the host operating system which makes it's lightweight.
  • Where as virtual machines have it's own kernel and runs on top of the host operating system with virtual access to the host operating system through a hypervisor .

For more insights click me ....

Containers and Virtual machines

Docker Architecture :

Docker runs in a client server architecture. The docker client talks to the docker demon and, Which does the heavy lifting of building , running and distributing the docker containers. The docker client and the demon connect using a REST Api using a unix socket or network interfaces .

Docker Architecture

The Docker daemon :

The Docker daemon listens for Docker commands from the client(docker cli) and then manage the services, building, running the container.

The Docker Client :

The Docker Client is the Docker cli you can build the images log the container and manage the docker services with this.

Docker registries :

Docker registries stores the docker images(Both official and unofficial) . Docker hub is the public registry anyone can use.
We can pull , push , the images and this is helpful if we are working on a team (Make sure that you are logged in).

Docker objects :

  • Images
  • Containers
  • Services
  • Networks
  • Volumes

Images :

  • A docker image is a read only configuration file which holds the
    instructions to create a container and often based on some
    another image (Base Image) with some additional customization .
    for example if we are pulling the docker image for ubuntu and we
    are installing nginx web server so the base image image is
    ubuntu and on top of that we are installing nginx web server we
    can also make our own docker image with a Dockerfile.

  • suppose we want run a nginx container

$docker pull nginx

$docker build . -t nginx

$docker images

copy the image id of the running container .

$docker run -p 8080:8080 imgid

To debug the container

$docker exec -it imgid

Dockerfile :

Dockerfile (Make sure the 'D' is capitalized) contains a set of commands which user could run on the command line to assemble the image .

Alt Text

  1. compress version of node.js(Base Image)
  2. folder where to write the code
  3. copy the file and '.' Means root \
  4. Install the packages
  5. copy the rest of the code into the root dir
  6. run the command to start the application
  7. which port to expose the container

Container :

A container is a runnable instance of an image . We can start, stop, delete, remove a container using the api (docker cli).
We can attach one or more containers using a concept called network and build another container based on its current state .

$docker ps

$docker start (container id)

$docker stop (container id)

$docker rm (container id)

$docker rmi (container id)

Services :

Services allows us to scale container across multiple daemons .
Which all work together as a swarm with multiple managers and workers.
Docker Swarm

For More Insights about master and worker click me ..

Network :

Docker Network provides a isolated environment in which the communication between two containers happens .

Volumes :

Docker Volumes is a data persistent used to maintain a unchangeable state inside a container . Helps from disaster recovery more specifically if there is sudden network drop or hard shut down .

Docker Compose :

Docker compose is a configuration file for defining multi-container docker application .

Docker-compose file :

  • Multiple isolated containers on a same host .
  • Avoid repetitive command .
  • Preserve volume data when container are created .
  • Only created container that have changed .

Alt Text

$docker-compose (compose file) up

$docker-compose (compose file) down

Let's take a simple example :

  • If we are not using docker compose we have to write individual command to start the containers .
$docker network create mongo-network

<-- command for mongodb -->
mongodb
docker run -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
--net mongo-network \
--name mongodb \
mongo

<-- command for mono-express -->

docker run -d \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
--net mongo-network \
--name mongo-express \
mongo-express
Enter fullscreen mode Exit fullscreen mode
version: "3"
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environments:
      MONGO_INITDB_ROOT_USERNAME=admin    
      MONGO_INITDB_ROOT_PASSWORD=password
  mongo-express:
    image: mongo-express
    ports:
    - 8081:8081
    environments:
      ME_CONFIG_MONGODB_ADMINUSERNAME=admin    
      ME_CONFIG_MONGODB_ADMINPASSWORD=password    
      ME_CONFIG_MONGODB_SERVER=mongodb 
Enter fullscreen mode Exit fullscreen mode
  • start the container

$docker compose -f mongo.yml up

  • default docker network got assigned

$docker network ls

Discussion (0)

pic
Editor guide