14

If I'd like to build a docker image in one pipeline step, then use it in a following step - how would I do that?

eg

default:
    - step:
        name: Build
        image: 
        script:
          - docker build -t imagename:local .
          - docker images
    - step:
        name: Deploy
        image: 
        script:
          - docker images

In this example, the image shows up in the first step, but not the second

David Alsh
  • 6,747
  • 6
  • 34
  • 60

1 Answers1

19

You would use Docker Save/Load in conjunction with bitbucket artifacts.

Example:

- step:
  name: Build docker image
  script:
    - docker build -t "repo/imagename" .
    - docker save --output tmp-image.docker repo/imagename
  artifacts:
    - tmp-image.docker
- step:
  name: Deploy to Test
  deployment: test
  script:
   - docker load --input ./tmp-image.docker
   - docker images

Source: Link

iceflow19
  • 752
  • 4
  • 12
  • 2
    Say if we would like to make it so that image isnt built everytime from scratch, how would we do it using Bitbucket pipelines? Caches: docker does not seem to work for me, it only caches environment var setup in my Dockerfile but not the apt-get installs or pip installs (which are the ones that take most time) – Elgiz Abbasov Nov 15 '21 at 03:53
  • @ElgizAbbasov You can build your docker pre-image and push it to docker hub. Each time you download it from the docker hub. – Student Apr 10 '23 at 22:56