I have a repo that has two microservices (command & query) with their related Dockerfile:
├── command
│ ├── DockerFile
│ ├── main.py
│ ├── requirements.txt
│ └── server
│ ├── app.py
│ ├── env.py
│ ├── logger.py
├── docker-compose.yaml
└── query
├── DockerFile
├── main.py
├── requirements.txt
└── server
├── app.py
├── database.py
├── env.py
I have a gitlab runner in a VPS, and another VPS where I deploy the services. I would like to deploy both microservices separately using gitlab-ci. Here is how I do usually when I have only one image:
stages:
- publish
- deploy
variables:
TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA
publish:
image: docker:latest
stage: publish
services:
- docker:19.03-dind
script:
- docker build -t $TAG_COMMIT -t $TAG_LATEST .
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- docker push $TAG_COMMIT
- docker push $TAG_LATEST
deploy:
image: alpine:latest
stage: deploy
tags:
- deployment
script:
- chmod og= $ID_RSA
- apk update && apk add openssh-client
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker pull $TAG_COMMIT"
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker container rm -f my-app || true"
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker run -d -p $INTERNAL_SERVER_IP:80:80 --name my-app $TAG_COMMIT"
The problem when having 2 images, is that the build stage will create an image for the entire repo. Is it possible to do it individually for each service ? Can I use docker-compose on the deployment server ?
CodePudding user response:
You can create two sets of jobs (publish-query, publish-command) that each build and push up the two docker images. If they don't depend on each other, they will even build at the same time depending on the runner. They would have the same stage (publish
in your case).
Likewise, you can create two deploy jobs (deploy-query, deploy-command) that does each deployment. Both in the deploy
stage.
I haven't used docker-compose
on the build server, but I have created images, pushed them up, and then ran docker-compose
on the deployment machine that pulls in the multiple images. Probably not the most graceful way of handling that, but it worked.