7

I'm trying to setup a pipeline using Gitlab CI and PHP/Symfony project. My .gitlab-ci.yml file should contain 3 stages:

  • Build - build the image, setup the database, install composer etc
  • Tests
    • Unit tests
    • Other tests (code quality, security checks)

I have it working as it should at the moment, but only by running the build steps alongside each test - this means I'm repeating the same build steps twice and it's taking longer than it should.

I know that GitLab allows you to build a Docker image and store it in the project, but for the time being, is there a mechanism to build the Docker image, store it (as an artifact?), and then pass it onto the test stages so they don't need to repeat the build?

1 Answer 1

7

Although using the registry to store the image is indeed the recommended way there is an answer to your question:

In the build stage save the image as a file:

docker save -o myimage.tar myimage:build123

Define myimage.tar as an artifact, use depends_on in the follow-up stage jobs and load the image there:

docker load -i myimage.tar

4
  • Thanks - I've hit a snag in that my image is based on php7.3-fpm which doesn't contain the docker binary, so it seems the docker save command won't run. I note that you can have docker:dind as a service, but I'm struggling to get everything glued together. Do you know of any more complete examples I could look at?
    – fistameeny
    Commented Jul 17, 2019 at 9:15
  • @fistameeny There is a complete, coded example in the DigitalOcean tutorial. Combine this with Stefan's answer above and the GitLab docs on artifacts, and you should be done.
    – Peterino
    Commented Aug 26, 2019 at 16:26
  • @fistameeny Using sysbox makes sense, see GitLab Runner Deploys Jobs in System Containers
    – Ini
    Commented Oct 27, 2023 at 0:27
  • i think the downside to doing it this way is you don't know if your docker push was properly saved to the repo.
    – dtc
    Commented Dec 20, 2023 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.