Open In App

Docker Compose vs Dockerfile with Code Examples

Last Updated : 19 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Docker is an open-source platform that empowers developers to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate an application and its conditions, ensuring consistency across various conditions, from advancement to production, the Dockerfile and the Docker Compose file are two essential Docker tools that make containerization easier.

Understanding how to make and utilize Dockerfiles and Docker compose files is essential for anybody seeking to influence Docker for proficient application deployment and the executives. This guide will go over the fundamentals of these tools, show you how to make and use them, and give real-world examples to show how to do so.

Dockerfile vs Docker Compose

Feature

Dockerfile

Docker Compose

Purpose

Defines how to build a Docker image.

Defines and manages multi-container applications.

File Type

Single file named Dockerfile.

YAML file typically named docker-compose.yml.

Syntax

Commands for building images (e.g., FROM, RUN, COPY).

YAML syntax for defining services, networks, and volumes.

Usage

Used to create Docker images for applications.

Used to manage and run multiple containers as a single service.

Command to Build/Run

docker build -t <image-name> .

docker-compose up to start all services defined in the YAML file.

Version Control

Changes in a Dockerfile can be tracked via version control.

Changes in the docker-compose.yml file allow easy updates to service configurations.

Dependencies

Can only define dependencies at the image build level.

Manages dependencies between multiple services, allowing them to start in a specific order.

Networking

Individual containers are connected by default to the bridge network.

Allows you to define custom networks for your services easily.

Volumes

Can specify volumes to persist data in the Dockerfile using VOLUME instruction.

Easily define and manage volumes for multiple services in a single YAML file.

Scale

Does not support scaling; each Dockerfile builds a single image.

Supports scaling services with the --scale option to run multiple instances of a service.

What is Dockerfile?

A Dockerfile is a simple text file that outlines a series of steps to build a Docker image. Each step adds a layer to the image, helping Docker keep the build process consistent and making sure the application and its dependencies can be reproduced reliably.

Key Components of Dockerfile

1. Base Image for a Dockerfile: The starting point for creating a Docker image, indicated by the FROM instruction. Base images can be moderate (like alpine) or accompany pre-installed software (like python:3.9-slim).

2. Instruction: Each line in a Dockerfile is a instruction that advises Docker how to build the image. Normal instructions include:

  • FROM: Sets the base image.
  • COPY: Copies files from the host system to the Docker image.
  • RUN: executes commands within the container, like software installation.
  • CMD: specifies the default command that the container will run upon startup.
  • EXPOSE: Informs Docker that the container tunes in on the predefined network ports.
  • ENV: Sets the environment variables.

3. Layer: A new layer in the image is created with each instruction in a Dockerfile. Layers help in reserving and enhancing the form cycle by reusing unaltered layers.

4. Image: A read-only layout used to create Docker containers. It incorporates the application code, runtime, libraries, and conditions.

What is Docker Compose File?

Docker Compose is a tool that makes it easy to set up and run applications that use multiple Docker containers. It helps manage complex setups by allowing you to define containers, networks, and storage volumes all within a simple YAML file.

Key Components of Docker Compose File

  • Service: a single configuration of a container in a Docker Compose file. Services can be defined with attributes like image, build, ports, volumes, and environment.
  • Volume: A capacity component for Docker containers, allowing data to endure in any event, when containers are stopped or recreated. Defined in the volumes part of a Docker compose file.
  • Network: Permits Docker containers to speak with one another. Defined in the networks part of a Docker Compose file.
  • YAML: ( YAML Ain't Markup Language) The syntax utilized for writing Docker compose file. It can be read by humans and is simple to comprehend.

Creating a Dockerfile: Step-by-Step Guide

Step 1: Launch EC2 Instance

Login to AWS Console and launch EC2 Instance

Running EC2 Instance

Step 2: Install Docker

Now install docker in our local machine by using following commands

sudo yum -y install docker
Installing Docker

Step 3: Create Dockerfile

Here is the dockerfile to build a Docker image

FROM amazonlinux:latest

RUN yum update -y

yum install -y git python3-pip

RUN git clone https://github.com/Sada-Siva-Reddy07/fish.git /fish

WORKDIR /fish

RUN pip3 install --no-cache-dir -r requirements.txt

EXPOSE 2000

CMD ["python", "app.py"]

Dockerfile

Step 4: Build the Docker Image

Now run the below commands to build docker image

docker build -t <filename> .
Build the docker image

Step 5: Run the Docker Container

Now the docker container by using following commands

docker run -dt -p 8000:8000 <filename>
Running the docker container

Here docker images list by using docker ps. Docker image successfully build.

Creating a Docker Compose File: A Step by Step Guide

Step 1: Install docker compose

Install docker compose file by using following commands

sudo curl -L 
https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose 
Downloading docker compose

Step 2: Create Docker Compose file

Here is the docker compose file to run container

version: '3.3'

services:

db:

image: mysql:8.0.27

command: '--default-authentication-plugin=mysql_native_password'

volumes:

- db_data:/var/lib/mysql

restart: always

environment:

- MYSQL_ROOT_PASSWORD=somewordpress

- MYSQL_DATABASE=wordpress

- MYSQL_USER=wordpress

- MYSQL_PASSWORD=wordpress

expose:

- 3306

- 33060

wordpress:

image: wordpress:latest

ports:

- 80:80

restart: always

environment:

- WORDPRESS_DB_HOST=db

- WORDPRESS_DB_USER=wordpress

- WORDPRESS_DB_PASSWORD=wordpress

- WORDPRESS_DB_NAME=wordpress

volumes:

db_data:

docker compose file

Step 3: Run Containers by using docker compose up

Once the docker-compose.yml file is configured, you can start the containers using the docker-compose up command. Now start our services by using following command

 docker-compose up -d
Running the docker compose

Conclusion

Mastering Dockerfile and Docker Compose is essential for developers and DevOps experts expecting to advance application development and deployment, dockerfile gives a strong method for prearranging the making of Docker images, ensuring that applications run reliably across various conditions by determining every single important reliance and configurations.

This capability is further enhanced by Docker Compose, which makes it simpler to orchestrate applications that use multiple containers, with Docker Compose, managing complex applications turns out to be more clear, as you can define all services, organizations, and volumes in a single, easy to-read YAML file, this not only makes deployment procedures easier to follow, but it also makes them easier to scale up and maintenance.

Your applications consistency, reproducibility, and isolation are all ensured by Dockerfile and Docker Compose working together, they make it easier to move from development to production without any errors, reducing the issue of "it works on my machine" and encouraging teamwork.

By utilizing these tools, you can accomplish a strong, versatile, and proficient application infrastructure, this prompts more solid organizations, faster recuperation from issues, and a more coordinated improvement process. Dockerfile and Docker Compose are fundamental for any cutting edge programming advancement work process, empowering you to understand the advantages of containerization completely.


Next Article
Article Tags :

Similar Reads