Bitbucket Pipelines - How to use the same Docker container for multiple steps? Bitbucket Pipelines - How to use the same Docker container for multiple steps? docker docker

Bitbucket Pipelines - How to use the same Docker container for multiple steps?


I've had the same issue a while ago and found a way to do it and I'm using it successfully right now.

You can do this using Docker's save and load along with BitBucket's Artifacts. You just need to make sure that your image isn't too large because BitBucket's Artifacts limit is 1GB and you can easily ensure this using multi stage-builds and other tricks.

- step: name: Build app script: - yarn run build - docker save --output <backup-file-name>.tar <images-you-want-to-export> artifacts: - <backup-file-name>.tar- step: name: Deploy to production trigger: manual deployment: production script: - docker load --input <backup-file-name>.tar - yarn run deploy

You might also like to use BitBucket's caches which can make building Docker images much faster. For example, you can make it so that NPM packages are only installed when package.json and yarn.lock files change.

Further Reading


Each step runs in its own Docker container, and its own volume. So you cannot have two steps running on the same build container.

Diving deeper into your problem

Are you trying to optimize for build minute consumption, or how long it takes for your build to complete?

If you're optimizing for build minutes, stick with what you have now. As the overhead of using multiple steps and artifacts will add some build minutes. But you'll lose out on the flexibility these features provide. Additionally, you can try ensure you're using a small Docker image for your build environment, as that will be pulled faster.

If you're optimizing for pipeline completion time, I'd recommend you go with your idea of using artifacts and parallel steps. While the total execution time is expected to be higher, you will be waiting for less time to see the result of your pipeline.


Possible solution which i recommend:

  - step:        name: Install dependencies        script:          - yarn install          - yarn global add gulp-cli

Your first step above should be in a pre-build docker container, which you host on Docker Hub and use for deployment via image: username/deployment-docker:latest.

Then, both steps can use this container for their tests.