Dockerfile vs. Docker Compose: What You Should Know

TechOps Examples

Hey β€” It's Govardhana MK πŸ‘‹

Along with a use case deep dive, we identify the top news, tools, videos, and articles in the TechOps industry.

IN TODAY'S EDITION

🧠 Use Case

  • Dockerfile vs. Docker Compose: What You Should Know

πŸš€ Top News

πŸ“½οΈ Videos

πŸ“šοΈ Resources

πŸ› οΈ TOOL OF THE DAY

Viking -  Simple way to manage your remote machines and SSH keys.

🧠 USE CASE

Dockerfile vs. Docker Compose: What You Should Know

When working with Docker, you'll often encounter two important concepts: Dockerfile and Docker Compose.

Although they are complementary, they serve different purposes in your containerized environment.

  • Dockerfile: Used to create and build Docker images.

  • Docker Compose: Used to run Docker containers as part of a multi-container setup or with specific runtime configs.

Dockerfile Overview

A Dockerfile is a script with instructions to build a Docker image, defining the base image, copying files, installing dependencies, and setting environment configurations.

Key Features:

  • Image Creation: Dockerfile builds images layer by layer.

  • Customizability: It defines dependencies, environment variables, and installations.

  • Efficiency: Supports build caching for faster image creation.

  • Version Control: Allows versioned images for different environments.

  • Consistency: By specifying every detail, ensures consistent builds across environments.

Here's an example Dockerfile that builds a Node.js app:

FROM node:16-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]

This Dockerfile performs the following:

  • Uses Node.js 16 on an Alpine base for a smaller image size.

  • Sets /usr/src/app as the working directory.

  • Copies the package.json and installs the necessary dependencies.

  • Copies the application files.

  • Runs the npm start command to launch the application.

Command to Build the Image:

docker build -t techops-node-app .

Output:

Running the command builds the image and tags it as techops-node-app.

Sending build context to Docker daemon 4.096kB

Step 1/4 : FROM node:16-alpine

...

Successfully built 456abc123

Successfully tagged techops-node-app:latest

Docker Compose Overview

Docker Compose defines and runs multi-container applications using a YAML file to configure services, networks, and volumes, managing container interactions.

Since version 1.28.6, Docker Compose defaults to compose.yaml or compose.yml, with backward compatibility for docker-compose.yaml/yml. If both exist, compose.yaml is preferred.

Key Features:

  • Orchestration: Manages container communication, data sharing, and networking.

  • Multi-Container Support: Simplifies managing multiple services.

  • Declarative Configuration: Uses YAML for easy service setup.

  • Resource Limiting: Sets memory and CPU limits for services.

  • Network Management: Handles service networking automatically.

  • Volume Management: Manages shared or persistent data.

Let’s configure a a Node.js service and a PostgreSQL database as an example:

version: '3.9'

services:

app:

build: .

ports:

- "4000:4000"

environment:

NODE_ENV: production

deploy:

resources:

limits:

cpus: "1.0"

memory: "1G"

volumes:

- .:/usr/src/app

networks:

- app-net

db:

image: postgres:13

environment:

POSTGRES_DB: techopsdb

volumes:

- db-data:/var/lib/postgresql/data

networks:

- app-net

networks:

app-net:

volumes:

db-data:

Running and Managing the Application

To build and run the services defined in the compose.yaml file, use the following command: docker compose up

Example Output:

Creating network "techops-app-net" with the default driver

Creating volume "techops-db-data" with default driver

Building app

Creating techops_app_1 ... done

Creating techops_db_1 ... done

Attaching to techops_app_1, techops_db_1

app_1 | Application running on port 4000...

db_1 | PostgreSQL init process complete; ready for startup.

Now, the app runs on port 4000, with PostgreSQL internally on the app-net network, allowing service communication.

In summary, Dockerfile builds the image, while Docker Compose manages multiple containers.

Knowing both is crucial, whether you're running one service or many.

You may even like:

Looking to promote your company, product, service, or event to 16,000+ TechOps Professionals? Let's work together.