Docker Compose Tutorial: A Step-by-Step Guide to Create and Connect Containers
Docker is a platform that allows you to build, share, and run applications using containers. Containers are isolated environments that package everything you need to run an application, such as code, libraries, dependencies, and configuration files.
Containers are lightweight and portable, which means you can run them on any machine that has Docker installed.
In this post, I’ll show you how to use some advanced features of Docker, such as:
- Docker Compose: a tool that lets you define and run multi-container applications using a YAML file.
- Docker Networks: a feature that enables communication between containers and external networks.
- Docker Volumes: a feature that allows you to persist data across container restarts and share data between containers.
- Docker Secrets: a feature that helps you protect sensitive data such as passwords and tokens.
Installing Docker
To follow along with this post, you’ll need to have Docker installed on your machine. You can download it from https://www.docker.com/get-started
Let’s get started!
Step 1: Create a simple web application using HTML and CSS
We’ll use the same web application that we created in the previous post. You can use any text editor or IDE of your choice. Save your files in a folder called my-website.
Here’s the HTML file (index.html):
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello, Docker!</h1>
<p>This is a simple web application running inside a container.</p>
</body>
</html>
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
Step 2: Create a Dockerfile in the same folder
A Dockerfile is a text file that contains instructions on how to build an image from your application. An image is a snapshot of your application and its dependencies that can be used to create containers. Here’s an example of a Dockerfile for your web application:
# Use an official nginx image as the base
FROM nginx:alpine
# Copy the web files to the image
COPY my-website /usr/share/nginx/html
# Expose port 80 for the web server
EXPOSE 80
Step 3: Build an image from your Dockerfile using the docker build command
This will create an image called my-website:v1 that you can use to run containers.
docker build -t my-website:v1 .
Step 4: Run a container from your image using the docker run command
This will start a container that runs your web application on port 80.
docker run -dp 80:80 my-website:v1
Step 5: Open your browser and go to http://localhost to see your web application running inside a container
You should see something like this:
Congratulations! You have successfully built and run your first Docker application. 🎉
Now let’s see how we can use some advanced features of Docker to improve our workflow.
Step 6: Define and run a multi-container application using Docker Compose
Docker Compose is a tool that lets you define and run multi-container applications using a YAML file. This makes it easier to manage complex applications that consist of multiple services.
For example, let’s say we want to add a database service to our web application. We can use MongoDB as our database and connect it to our web application using Docker Compose.
First, we need to create a new folder called my-app and move our my-website folder inside it. Then we need to create a new file called docker-compose.yml in the my-app folder. This file will contain the configuration of our multi-container application.
Here’s an example of a docker-compose.yml file:
version: "3"
services:
web:
build: ./my-website
ports:
- "80:80"
depends_on:
- db
db:
image: mongo
volumes:
- db-data:/data/db
volumes:
db-data: