| by Arround The Web | No comments

What is Difference Between Dockerfile and Docker Compose

The Docker platform works with different components and tools for building, sharing, and deploying applications and projects. The Docker platform delivers the software in small executable packages called containers. These containers are built and managed through Docker file instructions and Docker compose tool.

This write-up will demonstrate:

Difference Between Dockerfile and Docker Compose

Dockerfile and Docker Compose are both used to containerize applications and projects. The key difference between these two components is that “Dockerfile” is an instructions file used to specify the Docker container template in the form of a snapshot or image. However, Docker compose is a tool that is being utilized in Docker to fire up the micro-services and multi-container applications.

In Docker compose, the services and multi-container applications are configured through the “docker-compose.yml” file and include the Dockerfile to specify the build context for the container.

How to Create and Use Dockerfile?

To create and use the Dockerfile for building the snapshot for the container, first, create a Dockerfile and include essential instructions like base image, source file and its path, executables, ports, and volume. For the implementation, look at the provided steps.

Step 1: Create a Program File

First, create a program file named “index.html” and add the following code into the file:

<html>

<head>

<style>

body{

background-color: black;

}

h1{

Color: aquamarine;

font-style: italic;

}

</style>

</head>

<body>

<h1> Hello! Welcome to Linuxhint Tutorial</h1>

</body>

</html>

Step 2: Make Dockerfile

Next, create another file named “Dockerfile” that will containerize the “index.html” program. For this purpose, specify the following commands or instructions:

  • FROM” defines the container’s base image.
  • COPY” copies or adds the source file to the container’s path.
  • ENTRYPOINT” defines the executables for the containers:
FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Step 3: Create the Container Snapshot/Image

In the next step, build the container’s snapshot or image by utilizing the “docker build -t <image>” command. The name of the container image is defined through the “-t” option:

docker build -t html-image .

Step 4: Run the Container

Create and start the container through the newly created snapshot by utilizing the “docker run” command. Here, “-p” specifies the exposing port of the container:

docker run -p 80:80 html-image

For the verification, check out the assigned port of the local host and check if the container is executing or not:

How to Create and Use Compose File?

To configure the multiple containers or microservices in Docker compose, first, create a “docker-compose.yml” file and configure the instruction into the file. For the illustration, follow the provided instructions.

Step 1: Create Compose File

First, configure the application in a container or other microservices by specifying the essential instructions in the file. For instance, we have configured the “index.html” program by configuring the following instructions:

  • services” key specifies the services in the compose file. For instance, we have configured “web” and “web1” services to run the HTML program.
  • build” key is used to specify the build context for the container. For this purpose, Dockerfile instructions will be used by the “web” service.
  • ports” key defines the exposed port of containers.
  • image” key is used to specify the base image for service:
version:"3"
services:
  web:
    build: .
    ports:
      - 80:80
  web1:
    image: html-image
    ports:
    - 80

Step 2: Start the Container

Fire up the services in containers by utilizing the “docker-compose up” command. The “-d” option is used to execute the services in detached mode:

docker-compose up -d

For confirmation, visit the local host and check if the service is executing or not:

This is all about the distinction between Dockerfile and Docker compose.

Conclusion

The Dockerfile and Docker compose both are used to configure the application and services in the container. The key difference between these two components is that Dockerfile is simply referred to as an instruction or text file to build the snapshot of the container. In contrast, Docker compose is a microservices configuration tool used to configure multiple containers applications and services in separate containers. This write-up has illustrated the distinction between Dockerfile and Docker compose.

Share Button

Source: linuxhint.com

Leave a Reply