Deploying Docker Compose Production YAML on GitLab CI/CD

GitLab CI/CD provides powerful capabilities for automating the deployment of applications. Docker Compose is a popular tool for defining and managing multi-container Docker applications. In this blog post, we’ll explore how to deploy a Docker Compose production YAML file on GitLab CI/CD, even if the runner server and production server are not in the same location.

Prerequisites

  • Understanding of Docker Compose and GitLab CI/CD concepts.

Step 1: Setting up the Runner

To begin, set up a dedicated runner on the runner server. This runner will handle the execution of the deployment pipeline. Ensure that Docker is installed and properly configured on the runner server.

Step 2: Preparing the Docker Compose Production YAML

Modify your Docker Compose production YAML file to suit your deployment requirements. Let’s assume you have the following Docker Compose production YAML:

1
2
3
4
5
6
version: "3"
services:
  web:
    image: registry.example.com/my-app:latest
    ports:
      - 80:80

In this example, the web service pulls the pre-built image registry.example.com/my-app:latest from a container registry. Adjust the image and any other services or configurations according to your application needs.

Step 3: Transferring Files to the Production Server

In your .gitlab-ci.yml file, add a step to transfer the deployment package from the runner server to the production server using a secure file transfer protocol like SSH or SCP. Here’s an example configuration:

1
2
3
4
5
6
deploy:
  stage: deploy
  tags:
    - runner
  script:
    - scp docker-compose-production.yml user@production-server:/path/on/production/

In this example, the scp command transfers the Docker Compose production YAML file (docker-compose-production.yml) from the runner server to the production server at the specified destination path (/path/on/production/).

Step 4: Deployment Script on the Production Server

On the production server, create a deployment script that handles the extraction of the deployment package and executes the necessary commands to deploy the Docker Compose services. Here’s an example deployment script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/bash

# Extract deployment package
tar -xzvf /path/on/production/docker-compose-production.yml

# Change to deployment directory
cd /path/on/production

# Run Docker Compose
docker-compose up -d

Customize the script to suit your specific setup and requirements. Ensure that the production server has Docker installed and properly configured.

Step 5: Configuring the Production Server

Configure the production server to execute the deployment script when the deployment package is transferred. For example, you can use an SSH post-receive hook or a separate process that monitors the deployment directory for changes and triggers the deployment script accordingly.

Ensure that you have appropriate security measures in place, such as secure file transfer protocols, proper access controls, and secure environment variables for any sensitive information required for the deployment process.

Conclusion

Deploying Docker Compose production YAML files on GitLab CI/CD enables you to automate and streamline your application deployment process. By following the steps outlined in this blog post, you can deploy your application across multiple servers, even if the runner and production servers are in different locations.

Remember to prioritize security by using secure file transfer protocols and implementing appropriate access controls. Separating the build process from the production server enhances efficiency and consistency.

GitLab CI/CD and Docker Compose are powerful tools that, when combined, offer a robust solution for deploying and managing containerized applications. Experiment with different deployment strategies to find the approach that best fits your project’s requirements.

0%