Saturday, April 20, 2024

10 Essential Docker Compose Examples: Simple Setups for Everyday Tasks

10 Essential Docker Compose Examples: Simple Setups for Everyday Tasks


Example 1: Basic Nginx Server

Docker Compose File:

version: '3' services: webserver: image: nginx:latest ports: - "80:80"

Explanation: This configuration runs an Nginx server and maps port 80 on the host to port 80 in the container, allowing you to serve content over HTTP directly from the container.

Example 2: Python Development Environment

Docker Compose File:

version: '3' services: python-dev: image: python:3.8 volumes: - .:/app working_dir: /app command: python -m http.server ports: - "8000:8000"

Explanation: Sets up a Python environment, serves the current directory over a simple HTTP server on port 8000, and maps it to port 8000 on the host.

Example 3: PostgreSQL Database

Docker Compose File:

version: '3' services: db: image: postgres:latest environment: POSTGRES_DB: mydatabase POSTGRES_USER: user POSTGRES_PASSWORD: pass ports: - "5432:5432"

Explanation: Runs a PostgreSQL database with environmental variables to set the database name, user, and password, exposing port 5432 for connection.

Example 4: Redis Cache

Docker Compose File:

version: '3' services: redis: image: redis:alpine ports: - "6379:6379"

Explanation: Deploys a lightweight Redis cache using the Alpine Linux version, exposing the default Redis port for applications to connect to.

Example 5: Node.js Application

Docker Compose File:

version: '3' services: app: image: node:14 volumes: - .:/usr/src/app working_dir: /usr/src/app command: npm start ports: - "3000:3000"

Explanation: Runs a Node.js application, executes npm start within the container, and exposes port 3000 to the host for web access.

Example 6: Elasticsearch Service

Docker Compose File:

version: '3' services: elasticsearch: image: elasticsearch:7.10.0 environment: - discovery.type=single-node ports: - "9200:9200"

Explanation: Sets up a single-node Elasticsearch cluster, exposing port 9200 for applications to perform search and indexing operations.

Example 7: Apache Server

Docker Compose File:

version: '3' services: web: image: httpd:latest ports: - "8080:80"

Explanation: Runs the latest Apache HTTP Server, mapping port 8080 on the host to port 80 in the container for web service.

Example 8: MariaDB

Docker Compose File:

version: '3' services: mariadb: image: mariadb environment: MYSQL_ROOT_PASSWORD: example ports: - "3306:3306"

Explanation: Deploys a MariaDB database, setting the root password for access, and exposes port 3306 for database connections.

Example 9: PHP Development Environment

Docker Compose File:

version: '3' services: php: image: php:7.4-apache volumes: - .:/var/www/html ports: - "8000:80"

Explanation: Sets up a PHP environment with Apache, mounts the current directory into the web root, and exposes it on port 8000.

Example 10: SMTP Server

Docker Compose File:

version: '3' services: smtp: image: namshi/smtp environment: - SMARTHOST_ADDRESS=smtp.gmail.com - SMARTHOST_PORT=587 - SMARTHOST_USER=myemail@gmail.com - SMARTHOST_PASSWORD=mypassword ports: - "25:25"

Explanation: Configures an SMTP server for sending emails using a Gmail account, with SMTP settings specified via environment variables.

Each of these Docker Compose examples demonstrates a specific use case, simplifying the setup and deployment of various types of applications and services using Docker.


No comments:

Post a Comment