The Ultimate Guide to Docker

By Kapil Maheshwari Last Updated 17 Days Ago 6 Minutes Read Technology 0
Smart Entrepreneurs

Consistency and efficiency are important part of today’s fast development and deployment process. Docker is a powerful tool that simplifies the deployment process by packaging applications into containers. Docker ensures that applications run across different environments like development, testing or production.

What is Docker?

  • Docker is an open-source platform that enables developers to build, deploy, run, and manage containers.
  • Basically, docker is for running applications inside the containers.
  • Like Git provides tools to manage code versions, Docker provides tools to manage containers and their environments.

Installing Docker

Follow these steps to install Docker on your preferred platform:

For Windows:

  • Download Docker Desktop from the official Docker site. Docker Desktop: The #1 Containerization Tool for Developers | Docker
  • Follow the installation instructions and ensure that WSL 2(Windows Subsystem for Linux) is installed.
  • Then we will need to restart the system, so click on “Close and restart”button in the Docker installer window.
  • Once installed, open Docker Desktop and verify the installation by running:
    docker –version

For macOS:

For Linux (Ubuntu):

  • For updating package index run this command: sudo apt update
  • Install Docker using: sudo apt install docker.io
  • Enable and start the docker service by using: sudo systemctl enable docker –now
  • Verify the installation: docker –version

Benefits of Docker

  1. We can bundle all the necessary libraries, components, and dependencies of applications as a package. These packages are nothing but containers. So docker allows us to run this application across multiple environments.
  2. Docker provides some tools like docker-compose, and Kubernetes which improved the scalability by deploying multiple instances of containers easily.
  3. We can use CI tools like Jenkins, GitLab CI of Docker. These tools provide easy and smooth CI/CD process.
  4. Docker is compatible for microservices where application is running on multiple ports. We can use docker compose to run different services into different ports without affecting each other.
  5. Using docker we do not need full Operating System to run the applications. Containers uses smaller parts of OS for running the application, that is one of the reason of containers are faster than the virtual machine.

Docker Components

  1. Docker Containers: We can package application code and dependencies as a single unit. This single unit is a Docker container. we do not need to install dependencies into machines and servers every time, whenever we package everything as a single unit.
  2. Docker Images: We can consider the relationship between Docker images and docker containers as objects and classes. A docker file is a blue print, how docker containers are created. Docker image has instructions for creating the containers. Docker image is a YMAL file.
  3. DockerFile: A Docker file is nothing but a Docker Image. Where we can define instructions related to the application. Below is a simple example of the docker file for nodejs ‘s project.
    Docker
  4. Docker Hub: We all know about git hub where we can push the repositories or we can pull the code. So, the docker hub is just like git-hub, where we can pull and push the docker images. Developers can also share their images.
  5. Docker Desktop: When we download docker into our systems, at that movement docker desktop will install into local machine. We can interact with the docker hub with the use of docker desktop.
  6. Docker Compose: Docker provides supports for the microservices architecture. Where multiple services are running on different ports. In that case we can define and use docker compose file. Using docker compose file multiple services can run without affecting to each other. Below is a simple example of the docker compose file:

docker-img

We need to run docker-compose-up command to running containers.

List of some Important Commands

  • For Downloading Images from docker hub: docker pull image-name
  • For Running the downloaded image as a container: docker run image-name
  • For building images: docker build -t image-name.
  • For Removing Images: docker image rm image-name.
  • For Running compose file: docker-compose up
  • To stop compose file: docker-compose down
  • Building MERN application and start all the applications: docker-compose up –build

How we can build docker container

  • Step 1: First we need to create the docker file which is docker image. This file includes some instructions related to which dependencies were used, on which port the application is running, commands to start the server file etc.
  • Step 2: We need to use the docker build -t my-app command for building the docker image file.
  • Step 3: For Running the image: docker run my-app.

Docker Networking Basics

  • Containers in Docker can communicate with each other over networks. Docker provides an autoconfigured bridge network that connects containers on the same host by default. The network is isolation-providing, yet allows containers to contact each other when they need it. This chapter will discuss port mapping, one of the most important features, which will enable us to expose a container’s internal services to our host system or to an external user. For example, to get a container running and map the container’s internal port to our host, we would do the following:

docker run -p 8080:80 nginx

  • It maps port 8080 on the host machine to port 80 in the Nginx container, so we can access the web server by going to http://localhost:8080 through the browser. Docker networking simplifies the communication between the containers. We very easily manage our internal and external connectivity with less hassle.

Conclusion

  • Docker provides the facility to Containerize the application.
  • By Containerize the application docker simplifies the process of development and deployment.
  • Docker and it’s components provide compatibility for microservice architecture. Also simplifies the CI/CD pipeline with some tools for example Jenkins.
  • Here above example demonstrates how we can create the docker file, docker Compose file and build that image.
  • In summary, docker simplifies the process of development and deployment by excluding the replication process of local environments into different machines, operating systems or servers. Docker compose makes development and deployment process, where multiple services run into different ports.
Social Media :

Join 10,000 subscribers!

Join Our subscriber’s list and trends, especially on mobile apps development.

I hereby agree to receive newsletters from Mobmaxime and acknowledge company's Privacy Policy.