- Docker Compose: A tool for defining and running multi-container Docker applications. You define your application's services, networks, and volumes in a single
docker-compose.ymlfile. docker-compose.yml: The core configuration file for Compose, written in YAML.- Services: Individual containers that make up your application. Each service corresponds to a container that will be run.
upanddowncommands: How to start and stop your entire application stack.- Automatic Network Creation: Compose automatically creates a default user-defined bridge network for all services defined in the
docker-compose.ymlfile, allowing them to communicate by service name. - Port Mapping in Compose: Defining host-to-container port mappings in the
ymlfile.
- Create a new directory for your Compose project:
my-first-compose-app. app.py(Simple Flask app for web service):
from flask import Flask
from redis import Redis
import os
app = Flask(__name__)
# 'redis' will be the hostname for the Redis service as defined in docker-compose.yml
redis_host = os.getenv('REDIS_HOST', 'redis')
redis = Redis(host=redis_host, port=6379)
@app.route('/')
def hello():
try:
count = redis.incr('hits')
return f'Hello from Docker! I have been seen {count} times.\n'
except Exception as e:
return f"Error connecting to Redis: {e}\n", 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)requirements.txt:
Flask
redis
Dockerfile(for the Flask app - placed inmy-first-compose-app):
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]docker-compose.yml(inmy-first-compose-appdirectory):
version: '3.8' # Specify Compose file format version
services:
web: # Define a service named 'web'
build: . # Build the image from the Dockerfile in the current directory
ports:
- "8000:5000" # Map host port 8000 to container port 5000
environment:
REDIS_HOST: redis # Pass environment variable to the container
depends_on: # Ensure redis service starts before web (does not wait for readiness)
- redis
redis: # Define a service named 'redis'
image: "redis:alpine" # Use the official Redis Alpine image from Docker Hub- Start the application:
Navigate to the
my-first-compose-appdirectory in your terminal.
sudo docker-compose up -d(The -d flag runs containers in detached mode.)
-
Access the application: Open
http://localhost:8000in your browser and refresh to see the counter increase. -
View logs for all services or specific services:
docker-compose logs
docker-compose logs web
docker-compose logs redis- Check running services:
docker-compose ps- Stop and remove containers and network created by Compose:
docker-compose down- Create a new directory for this challenge.
- Create a simple
index.htmlfile (e.g., "Hello from Nginx!"). - Write a
docker-compose.ymlfile to:- Define a service for Nginx using the
nginx:latestimage. - Map a host port (e.g., 8080) to Nginx's default web port (80).
- Use a bind mount to serve your
index.htmlfrom your host machine into the Nginx container's web root (/usr/share/nginx/html).
- Define a service for Nginx using the
- Bring up the service with
docker-compose up -dand verify you can access your static page.