简体   繁体   中英

There is possible to cache docker-compose on Gitlab CI/CD?

With each app update, docker-compose is created again. Is there a chance to cache this build on Gitlab CI/CD?

gitlab-ci.yml:

image: docker

services:
  - docker:dind    

stages:
  - build
  - deploy

Build staging:
  stage: build

  before_script:
    - apk update
    - apk upgrade
    - apk add python3 python3-dev py3-pip build-base libffi-dev openssl-dev
    - pip install docker-compose

  script:
    - docker-compose up -d
    - docker exec -i app_php composer install

Deploy staging:
  stage: deploy
...

You would need to create a custom Docker image for that purpose. First, create a Dockerfile with the following content:

FROM docker
RUN apk update && apk upgrade && apk add python3 python3-dev py3-pip build-base libffi-dev openssl-dev
RUN pip install docker-compose

Next, you have three options:

#1 Build the image on your runner machine

If you have access to your runner machine, the simplest is to build the image using the Dockerfile on that machine using command:

docker build -f /path/to/Dockerfile -t your_org/image_name:version

Then, substitute the image name inside .gitlab-ci.yml with the image tag your_org/image_name:version that you specify during docker build .

#2 Build the image on your development machine and push to DockerHub

If you don't have access to your runner machine or your runner machine might have cleanup routine that remove unused Docker images, your next option is to utilize DockerHub.

First, register a DockerHub account if you do not have one yet. Also create a public repository under your account. Next, use the same docker build command as the option #1 above on your development machine. However, ensure that the your_org part of the image tag is the same as your DockerHub account name, while the image_name part matches the new repository name.

Then, use the following command to login and push the Docker image:

docker login
docker push your_org/image_name:version

Then, substitute the image name inside .gitlab-ci.yml with the image tag your_org/image_name:version that you specify during docker build .

#3 GitHub-DockerHub automatic build setup

This option is for more advanced usage for example if your custom Docker image requires additional files to be included in the image. First, you will need to create a GitHub account if you do not have it yet (.), Then, create a public repository and git push the Dockerfile and any additional files. Next, create a DockerHub repository and link it with that GitHub repository. Configure DockerHub repository to auto-build upon git push of the GutHub repository. Finally, trigger the build from your DockerHub repository Builds screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM