http://www.technicalpage.net/search/label/SQL

Docker

Docker for Beginners: DockerFiles, Images, Containers, and Docker Compose

What is Docker?

Suppose you create an application and run it on your system. It runs without any issues. Now, when you transfer and run the same application on another system, it shows an error or does not run. Why?

Every machine or environment can be different. Running an application depends on several factors, such as the operating system, dependencies, libraries, tools, and their versions.

For example, suppose a Java application requires a specific version of Java, certain libraries, and some configuration settings. If those requirements are not available or are different on another system, the application may not work as expected.

Docker helps solve this problem by packaging an application along with the dependencies it needs and running it in an isolated environment called a container.

What is a Dockerfile?

A Dockerfile is a text file that contains instructions for building a Docker image.

For example, a Dockerfile might contain instructions such as:

  • Which base image to use
  • Which programming runtime is required
  • Which application files should be copied
  • Which dependencies should be installed
  • Which port the application uses
  • Which command should be executed when the container starts

A Dockerfile is not a YAML or JSON file. It is normally just a text file named Dockerfile, without a file extension.

What is a Docker Image?

When Docker reads the instructions in the Dockerfile and builds it, it creates a Docker image.

A Docker image is a read-only blueprint/package containing the application, its required dependencies, libraries, configuration, and other files needed to run the application.

A Docker image is not simply a single YAML, JSON, or text file. It is a packaged artifact made up of multiple layers.

For example:

Dockerfile → Docker Image

What is a Docker Container?

A container is a running instance of a Docker image.

When we run a Docker image, Docker creates a container from that image, and the application runs inside the container.

So, we can think of it this way:

Dockerfile → Image → Container

  • Dockerfile: Instructions for building the image
  • Image: Read-only blueprint/package
  • Container: Running instance of the image

Why are Docker Images Useful?

Suppose we build an application on our development machine. Instead of manually installing the same dependencies and configuring another machine, we can build a Docker image containing what the application needs.

We can then transfer or pull the same image on another system and create a container from it.

This provides a much more consistent environment for running the application.

The second system does not necessarily need to have the application's runtime, libraries, or dependencies installed directly on the host because the required components can be included in the Docker image.

This is one of the major benefits of Docker: “Build once and run consistently across different environments.”

Is Everything Inside the Container?

Not exactly.

A container contains the application and the dependencies that were packaged into its image. It does not contain an entire physical or virtual machine.

Containers share the host operating system's kernel, which makes them generally lighter and faster to start than traditional virtual machines.

For example:

Virtual Machine

Hardware → Operating System → Virtual Machine → Application

Docker

Hardware → Host Operating System → Docker → Container → Application

What is Docker Compose?

When an application consists of multiple services, managing each container separately can become difficult.

For example, an application might have:

  • Web application
  • Backend/API
  • Database
  • Redis/cache

Docker Compose helps us define and run multiple Docker containers as a group.

Docker Compose commonly uses a YAML file, usually named compose.yaml or docker-compose.yml, to define the services, networks, volumes, ports, and other configuration.

So, this is where YAML commonly comes into the picture.

For example:

Dockerfile → Used to build an image

Docker Image → Used to create a container

Compose YAML → Used to define and run multiple services/containers

A Simple Example

Imagine we have a Java application.

We create a Dockerfile that says, in simple terms:

  1. Start with a Java runtime.
  2. Copy the application into the image.
  3. Include the required dependencies.
  4. Specify how the application should start.

Docker uses this Dockerfile to build an image.

We then run the image, and Docker creates a container.

The application runs inside that container.

The basic flow is:

Application + Dockerfile

↓

Docker Build

↓

Docker Image

↓

Docker Run

↓

Docker Container

↓

Application Running

Docker in Simple Terms

If we want to simplify everything:

Dockerfile = Instructions

Docker Image = Blueprint/Package

Docker Container = Running Instance

Docker Compose = Configuration for running multiple services together

Docker makes it easier to package applications and their dependencies and run them consistently across different environments.

No comments:

Post a Comment