Docker Compose is a powerful tool that allows you to define and run multi-container Docker applications with ease. By using a Compose file, you can configure all the services required for your application. In this tutorial, we will walk you through the process of installing Docker Compose on Debian 12 and show you how to use it to create and manage your Docker containers.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A Debian 12 server with root access.
- Internet connectivity.
Now, let’s dive into the installation process!
Step 1: Download Docker Compose
To download Docker Compose, you need to use the curl
command. Open your terminal and enter the following command:
curl -L "https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This command downloads the Docker Compose file and saves it to the /usr/local/bin
directory.
Step 2: Set Permissions
After downloading Docker Compose, you need to set the correct permissions to make it executable. Use the following command to set the permissions:
chmod +x /usr/local/bin/docker-compose
This command makes the docker-compose
command executable.
Step 3: Verify Installation
To verify that Docker Compose was installed successfully, run the following command:
docker-compose --version
If Docker Compose is installed correctly, you should see the version number displayed in the output.
Step 4: Install Docker (Optional)
If you don’t have Docker installed on your Debian 12 server, you can follow these steps to install it. First, check for system updates and install them using the following commands:
apt update apt upgrade
Once the updates are complete, install the basic dependencies required for Docker:
apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Step 5: Import Docker Repository GPG Key
To install Docker, you need to import the Docker repository GPG key. Use the following command to import the key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 6: Add Docker CE Repository
Next, add the Docker CE repository to your system by running the following command:
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Step 7: Install Docker
After adding the repository, you can now install Docker on your Debian 12 server. Use the following command to install Docker:
apt install docker-ce
Step 8: Start Docker
To start the Docker service, use the following command:
systemctl start docker
You can check the status of the Docker service by running the command:
systemctl status docker
If Docker is running, you should see the status as “active (running)”.
Step 9: Create a Docker Compose File
Now that Docker Compose and Docker are installed, you can start creating your Docker application. Begin by creating a new directory for your application and navigate into it:
mkdir my_app cd my_app
Next, create the docker-compose.yml
file using a text editor of your choice:
nano docker-compose.yml
Paste the following content into the docker-compose.yml
file:
version: '3' services: db: image: mysql:5.7 restart: always volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: wordpress wordpress: image: wordpress restart: always volumes: - ./wp_data:/var/www/html ports: - "8080:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: password depends_on: - db volumes: db_data: wp_data:
This docker-compose.yml
file defines two services: db
and wordpress
. The db
service uses the mysql:5.7
image and creates a MySQL database for the WordPress application. The wordpress
service uses the wordpress
image and mounts a volume for the WordPress files. It also maps port 8080
on the host to port 80
on the container.
Step 10: Start the Docker Application
To start the Docker application defined in the docker-compose.yml
file, run the following command:
docker-compose up
This command will download the required images and start the containers for the db
and wordpress
services. You will see the logs for each container as they start up.
Step 11: Access WordPress
After the containers are running, you can access the WordPress installation by opening your browser and entering the following URL:
http://yourserver-ip-address:8080
You should see the WordPress installation screen, where you can set up your WordPress site.
Step 12: Running Docker Compose in Detached Mode
By default, Docker Compose runs in the foreground, which means the terminal is occupied while the application is running. To run Docker Compose in detached mode, use the following command:
docker-compose up -d
This command starts the application in the background and frees up your terminal for other tasks.
Conclusion
In this tutorial, we walked you through the process of installing Docker Compose on Debian 12 and using it to create and manage Docker containers. Docker Compose is a powerful tool that simplifies the deployment and management of multi-container applications. With Docker Compose, you can define your application’s services in a single configuration file and start them with a single command.
If you’re looking for reliable and scalable cloud hosting solutions, Shape.host provides Linux SSD VPS services that cater to your specific needs. With Shape.host, you can enjoy the benefits of efficient and secure cloud hosting.