How to connect a gitlab-runner with a gitlab service via docker-compose?
How to use Docker Compose In Gitlab CI
Gitlab Runner can execute jobs with specified docker-compose files in series, but not concurrently (Solved, it totally can)
Use gitlab-runner with docker-compose
Videos
I’ve just started using gitlab runners, and I’m stuck in a point related to running docker-compose.
I don’t know if my approach ie correct, but I have a docker-compose file that runs multiple docker images on my system, and I want the gitlab runner to pull the code and run the docker-compose.yml file on the host once I push my changes to the repo.
The docker-compose.yml file is not included in the repo. I think that might be the problem, if it is, is there anyway to make the runner use the host’s docker-compose that is already running?
Thank you in advance.
Hi there, I have a docker-compose with a node server and a nginx server and I would like to build these images and deploy them on a server. So how do I need to configure my gitlab runner in order to do so? I do not find any tutorial suited for this, I don't know what I am not getting here. Do I build and push my images individually and then somehow need to get the docker-compose to my server? Or is there a simpler way to do this? Thanks in advance!
EDIT (2023): Do not keep using docker-compose! Please migrate to docker compose, which is now part of the docker image and a 99% drop-in replacement of docker-compose. Ironically, if you (now) use docker:latest, the question does not apply. See Docker's documentation on Compose V2. The answer below only applies to docker-compose, AKA Compose V1, which is to be discontinued/become unsupported.
Docker also provides an official docker-compose image: docker/compose
This is the ideal solution if you don't want to install it every pipeline.
Note that in the latest version of GitLab CI/Docker you will likely need to give privileged access to your GitLab CI Runner and configure/disable TLS. See Use docker-in-docker workflow with Docker executor
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
# Official docker compose image.
image:
name: docker/compose:latest
services:
- docker:dind
before_script:
- docker version
- docker-compose version
build:
stage: build
script:
- docker-compose down
- docker-compose build
- docker-compose up tester-image
Note that in versions of docker-compose earlier than 1.25:
Since the image uses
docker-compose-entrypoint.shas entrypoint you'll need to override it back to/bin/sh -cin your.gitlab-ci.yml. Otherwise your pipeline will fail withNo such command: sh
image:
name: docker/compose:latest
entrypoint: ["/bin/sh", "-c"]
Following the official documentation:
# .gitlab-ci.yml
image: docker
services:
- docker:dind
build:
script:
- apk add --no-cache docker-compose
- docker-compose up -d
Sample docker-compose.yml:
version: "3.7"
services:
foo:
image: alpine
command: sleep 3
bar:
image: alpine
command: sleep 3
We personally do not follow this flow anymore, because you loose control about the running containers and they might end up running endless. This is because of the docker-in-docker executor. We developed a python-script as a workaround to kill all old containers in our CI, which can be found here. But I do not suggest to start containers like this anymore.