0
0
Jenkinsdevops~5 mins

Docker compose in pipelines in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Running multiple containers together can be tricky. Docker Compose helps by defining and running multi-container Docker apps easily. Using Docker Compose inside Jenkins pipelines automates testing and deployment of these container groups.
When you want to start a database and an app container together during automated tests.
When your app depends on multiple services and you want to run them all in Jenkins before deployment.
When you need to build and test a multi-container app automatically on every code change.
When you want to cleanly start and stop related containers as part of your CI/CD pipeline.
When you want to ensure your app works with its dependencies in a consistent environment during Jenkins runs.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
  redis:
    image: redis:7.0
    ports:
      - "6379:6379"

This file defines two services: web running Nginx and redis running Redis server.

Ports are mapped so Jenkins can access the web server on port 8080 and Redis on 6379.

Version 3.8 is used for compatibility with modern Docker Compose features.

Commands
Starts all services defined in docker-compose.yml in detached mode so Jenkins can continue running other steps.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating redis ... done Creating web ... done
-d - Run containers in the background (detached mode)
Shows the status of all running containers started by Docker Compose to verify they are up.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports -------------------------------------------------------------------------------- example_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp example_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
Stops and removes all containers, networks, and volumes created by docker-compose up to clean up after the pipeline.
Terminal
docker-compose down
Expected OutputExpected
Stopping example_web_1 ... done Stopping example_redis_1 ... done Removing example_web_1 ... done Removing example_redis_1 ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: Docker Compose lets you start and stop multiple related containers together easily inside Jenkins pipelines.

Common Mistakes
Running 'docker-compose up' without the -d flag in Jenkins pipeline
This blocks the pipeline because containers run in the foreground and Jenkins waits indefinitely.
Always use 'docker-compose up -d' to run containers in detached mode so the pipeline can continue.
Not running 'docker-compose down' after tests
Containers and networks remain running, consuming resources and causing conflicts in future pipeline runs.
Always run 'docker-compose down' at the end of the pipeline to clean up resources.
Not mapping ports in docker-compose.yml
Jenkins or test scripts cannot access the services inside containers if ports are not exposed.
Map necessary ports in the docker-compose.yml file under each service.
Summary
Use 'docker-compose up -d' to start all services in the background during Jenkins pipeline runs.
Check running containers with 'docker-compose ps' to verify services are up.
Clean up all resources with 'docker-compose down' after pipeline steps finish.