Docker

Docker

1. What is Docker?

  • Docker was released in March 2023 it is developed by Solomon Hykes and Sebastian Paul. it is written in the 'Go' language.

  • Why Docker? : Before Docker, you often heard that particular code is running in the Developer system but not in the user's system. To overcome this issue Docker came into the picture as Docker implements OS level Virtualization or System level virtualization, also known as Containerization

  • It is an open-source centralized platform designed to create, deploy and run the application.

  • Docker uses the container on the host OS to run the application. And it allows the application to use the same Linux kernel of the host machine rather than creating a whole virtual OS That's why Docker is lightweight.

2. What is a Docker image and how we can create a Docker image?

A Docker image is a lightweight, standalone, executable package that contains everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and other configuration files. Docker images are used to create and run Docker containers, which are isolated instances of a software application.

Dockerfile is required to create an image. We collect all the information related to an application like which base version of an environment is required to run an application called a base image then we collect dependencies or libraries and a set of commands to run an application then we write those instructions in a "Dockerfile" (In Dockerfile "D" should be capital and it is case sensitive) in a systematic manner. and post Dockerfile creation we can build an image using that Dockerfile as given below example:

Dockerfile

FROM node:12.2.0-alpine # Node base image
WORKDIR /app  # Container working directory where all app related data will be stored
COPY . .      # Copying all project data from local directory to Container working directory.
RUN npm install  # Command to install node packages in the container working directory.
EXPOSE 8000  # Exposing or whitelisting Container port to access app on this port
CMD ["node","app.js"]  # Command to start application outside of the container

Command to create an image using the above Dockerfile where "." indicates the current directory where "Dockerfile" is present, "-t" indicates the tag name we are giving to the image and "node-app" is a tag name of an image.

docker build . -t node-app

3. What is the Docker Container?

The container is an entity that has all the instructions, required code, and steps to run an application or an image. An image runs in a Docker container.

4. What is the Docker engine and what command to install it?

Docker Engine provides a consistent environment for applications to run, making it easier to develop, test, and deploy applications across different environments.

Command to install Docker engine:

sudo apt install docker.io

It is made of two components:

  • docker d (daemon): It is a background process to help and manage docker containers.

  • docker CLI: Docker command line interface is a tool using it we can manage and perform every operation related to docker using commands on CLI.

5. Important Docker commands and their usage.

  • Important command to add docker in our current user group list so that we can execute the docker command using our current user, This would be very helpful while creating the Jenkins pipeline. (server reboot recommended after adding a user to the group)

      sudo usermode -aG docker $USER
    
  • Below is a command, we use to build an image using Dockerfile.

      docker build . -t <Image-tag-name>
      # Example
      docker build . -t node-todo-app
    
  • docker images command used to list out docker images.

      docker images
    
  • docker run command used to run docker image in a docker container where -d indicated command running in demon mode(background), -p indicates port mapping.

      docker run -d -p <host-port>:<container-port> <image-tag>
      # Example
      docker run -d -p 8000:8000 node-todo-app
    
  • the docker ps command is used to list out all the running containers.

      docker ps
    
  • docker pull command is used to pull the mentioned image from a docker image repository like Dockerhub.

      docker pull <image-tag-name>
      # Example
      docker pull raygurudeo/node-todo-app:latest
    
  • the docker kill command is used to kill or stop the running container.

      docker kill <container-ID>
    
  • the docker container prune command is used to delete all the stopped containers.

      docker container prune
    
  • the docker rmi command is used to remove unused docker images.

      docker rmi <image-ID>
      # Find image ID using "docker images"
    
  • the docker image prune --all command is used to delete all the unused images from the list.

      docker image prune --all
    
  • Below command is used to delete all the intermediate images while building new images to save spaces.

      docker build . --rm -t <image-tag-name>
      # Example
      docker build . --rm -t node-todo-app:latest
    
  • the docker exec -it command is used to access a specific container and its file system. We need to mention specific container ID.

      docker exec -it <container-ID> /bin/bash
    

6. What is the Docker volume, Its purpose and some useful command of docker volume?

A docker volume is an entity that stores the data of a container.

Let's say we have a container where MYSQL DB is running and have some user's information in the DB and because of some modification had to restart the container or agent itself. In this case, our data will be lost. To prevent this data loss we should create a volume where all the application-related or DB-related information will be stored and once after rebuild or restarted the container or data will be secure and saved.

  • Command to create a docker volume.

      docker volume create --name <volume-name>
      # Example
      docker volume create --name node-todo-app-volume
    
  • Command to check volume list.

      docker volume ls
    
  • Command to run docker container by attaching docker volume to it to prevent data loss.

      docker run -d --mount source=<volume-name>,target=<target-location> -p <host-port>:<container-port> <image-tag>
      # Example
      docker run -d --mount source=node-todo-app-volume,target=/data -p 8000:8000 node-todo-app:latest
      # --mount: option used to mount volume
      # source volume name
      # target: Working directory or application data directory in the container which you want to save.
    

7. What is the docker-compose? docker-compose usage and its example.

docker-compose is an entity that is used to run or config multiple docker container configurations. Or we could say docker-compose is used to create multiple containers or microservices.

Command to install docker-compose.:

sudo apt install docker-compose

docker-compose configures multiple containers or microservices using the "docker-compose. yaml" file (docker-compose. yaml is case sensitive) where we write all the configurations with correct indentation.

Below is the example of docker-compose where we have configured two containers. The first one is the backend service and the other is the MySQL database service.

version: '3'  # docker-compose verion 
services:     # defines container or services we are using
    backend:  # service name
        build:    # provides information to docker-compose about image needs to be build.
            context: .  # "." defines or inform docker-compose that Dockerfile is present in the current directory to build an image
        ports:          # provides port information to map port while starting sontainer
            - 8000:8000
    mysql:                # second container or service
        image: mysql:5.7    # docker-compose will pull image of mentoned version.
        volumes:            # provides volumes information to save app data
            - mysql-data:/var/lib/volume/node-todo-app-volume/

Command to up docker-compose which will run the configured docker containers provided in the yaml file.

docker-compose up

Command to down services using docker-compose.

docker-compose down

8. Way to tag a docker image and push it to Dockerhub.

  • First login to Dockerhub.

      docker login -u <Dockehub-username> -p <Dockerhub-Password>
    
  • Tag a source image to push to Dockerhub.

      docker tag <Source-Image>:<Tag> <dockerhub-username>/<Target-Image-Name>:<Tag>
      # Example:
      docker tag node-todo-app:latest raygurudeo/node-todo-app:latest
      # raygurudeo is my dockerhub username
    
  • Push the Tagged image to Dockerhub.

      docker push <target-image-name>
      # Example
      docker push raygurudeo/node-todo-app:latest
    

Thank you for reading my Blog !

Thanks,
Gurudeo ray