4

I am new on creating pipelines on bitbucket to automate building a specific branch after merge. The project is written in C++ and has the following structure:

PROJECT FOLDER
    - .devcontainer/
        - devcontainer.json
    - bin/
    - doc/
    - lib/
    - src/
        - CMakeLists.txt
        - ...
    - CMakeLists.txt
    - clean.sh
    - compile.sh
    - configure.sh
    - DockerFile
    - bitbucket-pipelines.yml

We created a DockerFile with all the settings required to build the project. Is there any way I can reference the docker image on bitbucket-pipeline.yml to the DockerFile from the repository?

I have been able to upload the docker image on my docker hub and use it with my credentials by defining:

image:
  name: <dockerhubname>/<dockername>
  username: $DOCKER_HUB_USERNAME
  password: $DOCKER_HUB_PASSWORD
  email: $DOCKER_HUB_EMAIL

but I am not sure how to do so bitbucket takes the DockerFile from the repository and uses it to build the image, and if by doing it like this, the build time will increase.

Thanks in advance!

laurapons
  • 971
  • 13
  • 32
  • I didn't understand the question. Do you want to build a docker image and use it in a step in bitbucket? or do you want to build the image at run time and have access to it? – sebassebas1313 Jun 07 '21 at 13:45
  • I want to build a docker image and use it in a step in bitbucket unless it is not advised to do so. Is there any difference between asking bitbucket to build an image from a DockerFile or getting the build image from DockerHub (in terms of building/downloading time)? I was also think it would be good to compile the image as a step only when there are differences in the DockerFile and then upload the new image to docker (but I am too new to do that by myself) – laurapons Jun 09 '21 at 10:10
  • I believe building an image at run time may take longer than getting the image from docker hub. Unless necessary (e.g. you need a dynamic element from your pipelines) I would separate both things. – sebassebas1313 Jun 10 '21 at 16:08

1 Answers1

2

In case you want to build your image during your pipelines process you need the same steps as if your image was built in your machine:

  1. Build your image docker build -t $APP_NAME .
  2. Push it to your repo (e.g. docker hub) docker push $APP_NAME:$VERSION

You can do something like this:

steps:
  - step: &build
    name: Build Docker Image
    services:
      - docker
    script:
      - docker build -t $APP_NAME .
      - docker push $APP_NAME:$VERSION

Think that every step in your pipelines runs in a docker container and that allows you to do whatever you want. The docker service allows you to use a out of the box docker client. Then after pushed you can use the image in another step. You just need to specified the image for the step.

sebassebas1313
  • 363
  • 2
  • 8