Skip to content
DOCKER

Docker

Introduction

We looked at different ways companies were actively using Docker in the modern softwaredevelopment world. Deployment, scaling up, and management have increasingly becomethe greatest factors for success in today's software development industry. Docker, asolution that transformed the way we transmit and execute applications, has been the mainplayer in this shift. By the same thought, Docker has managed to simplify the running ofapplications in containers. Consequently, developers have been able to organize theirapplications so that they run smoothly in various environments.

What is Docker?

Docker is basically a tool that allows users to use a hardware resource others know nothing about. In other words, Docker is an open-source platform that automates the deployment of an application using virtualization. Docker containers are the heart of the system. In reality, however, they represent entire configurations along with anything else necessary to run an application. In other words, Docker containers can be used for development, testing, and production without changes.

Suppose, you have a situation, when you develop a program on your personal computer.Everything goes ideally. However, if you attempt to start it on another operating system,the program may exhibit some issues. The reason for this is that the operating system isslightly different, the required libraries may be missing, or there might be versionsclashes. Fortunately, this is where Docker comes into play. It hides the differencesbetween infrastructures by placing the code not only in the container but also listingdependencies in a standardized form so that they are eliminated.

Key Benefits of Docker

  1. Consistency Across Environments: Docker containers encapsulate all the required components, ensuring that applications run the same way, no matter where they are deployed.
  2. portability: Containers can be moved easily between different environments, whether it's your local machine, a data center, or a cloud provider.
  3. Resource Efficiency: Unlike virtual machines that run full operating systems, Docker containers share the host machine's OS kernel, making them lighter and more efficient.
  4. Isolation: Applications run in isolated environments, reducing the risk of conflicts and improving security.

What are Docker Containers?

Docker containers are the most essential element of the Docker ecosystem. To put itsimply, a Docker container is a lightweight, standalone, and executable unit of softwarethat incorporates everything that an application needs to run, for example, the code, onwhich the application depends, system tools, and the configuration file. The fact that thecontainers are stand-alone and isolated from the host system is what gives them theiradvantage—they provide security and stability to the applications.

How Do Containers Work?

Containers and traditional virtual machines (VMs) are different in that containers don'tneed a whole operating system for each instance and instead share the OS kernel ofthe host machine. Therefore, containers have the ability to boot much cleaner and useless system resources than VMs out there.For instance, when running a Node.js application in a Docker container, the containerhas the Node.js runtime and any libraries the application is using. Because thecontainers are isolated, you can have various releases of Node.js running at the sametime on the same node without having disturbances.

Advantages of Docker Containers

  1. Efficiency: Containers are lightweight and less resource-intensive as they use the same OS kernel as the host system and, therefore, are more efficient than traditional VMs.
  2. Isolation: Containers provide process isolation, which means that if one container fails, the rest that is currently running on the same host will create no disturbance to others.
  3. Scalability: Docker containers can be easily replicated across multiple hosts or scaled up as traffic increases. Tools such as Docker Compose and Kubernetes remain in charge of these operations.
  4. Flexibility: You can execute a server that is running more than one container, each of them with its unique version of an application or framework, which will, therefore, prevent any conflicts to occur when different versions or dependencies are requested.

Docker containers are popular because they ensure a consistent environment from development to production, solving the common issue of "it works on my machine" by making sure it works the same everywhere.

What are Docker Images?

Whereas containers are the running state of an application, the Docker image represents the blueprint from which containers are derived. In Docker, a Docker image is the readonly template used to build a container, with a set of instructions. For example, the Docker images include everything needed to run the application: the code, configuration files, environment variables, libraries, and any dependencies.

Layers in Docker Images

Docker images consist of layers. Each instruction in a Dockerfile creates a new layer. So itis telling Docker to take an Ubuntu base image, add libraries, and add application code.Each of those steps is a layer. This is helpful because you make Docker able to reuselayers and speed up the build process and cut down on duplication. If there are somelayers common to two images-for example, the operating system is the same or theruntime environment is the same-Docker will re-use those layers.This layering system makes Docker images efficient and modular. You do not have torebuild the whole image to update your application; docker can just update it by updatingthose changed layers.

Docker Hub and Custom ImagesDevelopers often make use of Docker images, which are located in Docker Hub - a centraland authoritative place that harbors images shared by the community and organizations.For example, you easily find an official image for any of the most popular software:Python, MySQL or Nginx. Such images are a starting point for your applications, but all thecomplexities of the underlying dependencies are not your responsibility to work on.If your application has special needs, you might write a Dockerfile to create a customimage from scratch. In short, a Dockerfile is just a simple text file that contains a list ofinstructions Docker follows in order to build your image. You can then share the imagewith others or deploy to production directly from there.

Docker Files:

A Dockerfile is a simple text file containing a series of commands (instructions) used to assemble a Docker image. It automates the process of creating a Docker image by specifying all the necessary steps, such as installing software, copying files, setting environment variables, and configuring services.

Common Instructions in a Dockerfile:

  1. FROM: Specifies the base image (starting point).
  2. RUN: Executes commands to install software or dependencies.
  3. COPY / ADD: Copies files from your local system into the image.
  4. WORKDIR: Sets the working directory inside the container.
  5. EXPOSE: Declares which network port the container will listen on.
  6. CMD / ENTRYPOINT: Defines the default command or script to run when the container starts.

DOCKER FILE EXAMPLE:

Explanation:

In the root of your VS Code project, create a file named Dockerfile with dockerfile code.

  1. The node:14 image already has Node.js installed, so you don't need to manually install Node.js in the container, you can adjust it to other languages if needed.
  2. The WORKDIR instruction specifies a directory inside the container that will be used as the current working directory for any subsequent commands in the Dockerfile. If the specified directory (/usr/src/app in this case) does not exist, Docker will create it automatically.
  3. This copies the package.json and package-lock.json files to the working directory.
  4. The install command is used to install all the dependencies listed in your package.json file.
  5. COPY . .: Copies all project files into the container.
  6. EXPOSE 3000 instruction is important because it informs users of the Docker image which port your application will listen to for incoming connections. Example: If you have a web application container exposing port 3000, another container can access it via http://:3000.
  7. CMD ["npm", "start"] in the Dockerfile specifies that when the container is run, it should execute the npm start command, which starts the application.

Building the Docker Image:

To build the Docker image from the Dockerfile, you would run:

Few Docker Commands:

  • docker pull [image_name]
    • explanation: Downloads a Docker image from Docker Hub (or any specified registry) to your local system. It%E2%80%99s the starting point for running containers.
  • docker run [image_name]
    • explanation: Runs a Docker container based on the specified image. If the image isn't already downloaded, Docker will pull it first.
  • docker ps
    • Explanation: Lists all currently running Docker containers. You can see container IDs, names, statuses, and more.
  • docker stop [container_id]
    • Explanation: Stops a running container by specifying the container ID or name. This is useful for shutting down containers without deleting them.
  • docker rm [container_id]
    • Explanation: Removes a stopped container. This helps free up resources and avoid clutter in your Docker environment.

Author

Ambika