Getting Started with Docker and Docker Compose ๐Ÿ‹

Docker has revolutionized the way developers build, ship, and run applications. With Docker, you can package your applications along with their dependencies into lightweight containers that run consistently across different environments. In this guide, we'll cover:

  • Installing Docker and Docker Compose
  • Running your first container
  • Creating a Dockerfile to build custom images
  • Managing multi-container applications with Docker Compose

By the end of this tutorial, you'll be able to containerize your own applications and run them efficiently.


Prerequisites

Before we dive in, make sure you have:

To verify your installation, run:

docker --version
docker compose version

If you see version numbers for both, you're good to go!


Running Your First Docker Container

Let's start by running a simple container. Docker has a library of pre-built images on Docker Hub. To run an Nginx web server in a container, execute:

docker run -d -p 8080:80 --name my-nginx nginx

Now, open your browser and go to http://localhost:8080. You should see the default Nginx welcome page.

To list running containers, use:

docker ps

To stop and remove the container:

docker stop my-nginx
docker rm my-nginx

Building a Custom Image with Dockerfile

Let's create a simple web application using Python and Flask, then containerize it.

  1. Create a project directory:
mkdir my-flask-app && cd my-flask-app
  1. Inside the directory, create a app.py file:
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Docker!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
  1. Create a requirements.txt file:
flask
  1. Create a Dockerfile to build a custom image:
# Use an official Python runtime as base image
FROM python:3.9

# Set the working directory in the container
WORKDIR /app

# Copy the application files to the container
COPY . /app

# Install dependencies
RUN pip install -r requirements.txt

# Expose the port Flask runs on
EXPOSE 5000

# Command to run the application
CMD ["python", "app.py"]
  1. Build and run the container:
docker build -t my-flask-app .
docker run -d -p 5000:5000 --name flask-container my-flask-app

Now, visit http://localhost:5000, and you should see Hello, Docker!.


Managing Multi-Container Applications with Docker Compose

Docker Compose allows you to define and run multi-container applications using a simple YAML file.

Let's say we want to run our Flask app with a PostgreSQL database. Create a docker-compose.yml file:

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db
  
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydatabase
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Now, start the application with:

docker compose up -d

To stop and remove containers, run:

docker compose down

Conclusion

By now, you should have a solid understanding of Docker and Docker Compose. Youโ€™ve learned how to:

  • Run Docker containers
  • Build custom images with Dockerfiles
  • Manage multi-container applications using Docker Compose

Now it's your turn! Try containerizing one of your own projects and see how easy deployment becomes.

Happy coding! ๐Ÿš€