How do I create a docker image for Bitbucket Pipelines?

sbirgisson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

My project has very specific requirements to be able to build and deploy. How do I do this?

 

7 answers

1 accepted

12 votes
Answer accepted
sbirgisson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 16, 2016

Using this as an example repository: https://github.com/mark-adams/docker-chromium-xvfb, there are some steps required to create your own docker image.

  1. Have a Docker hub account, https://hub.docker.com/
  2. Install docker
  3. Create a Dockerfile
  4. Build the image from Dockerfile
  5. Push the image to Docker hub
  6. Specify the image in your bitbucket-pipelines.yml file

1 Docker hub account

Well, sign up here =) https://hub.docker.com/

2 Install docker

One way is to use Docker toolbox: https://www.docker.com/products/docker-toolbox

3 Create Dockerfile

Example file here: https://github.com/mark-adams/docker-chromium-xvfb/blob/master/images/base/Dockerfile

#Some smart base image
FROM debian:jessie 
 
# Whatever you need more than what is on the base image required by your project
 
RUN apt-get update && apt-get install -y xvfb chromium
ADD xvfb-chromium /usr/bin/xvfb-chromium
RUN ln -s /usr/bin/xvfb-chromium /usr/bin/google-chrome
RUN ln -s /usr/bin/xvfb-chromium /usr/bin/chromium-browser

4 Build docker image

In the same directory as you have your Dockerfile, run command:

docker build -t yourdockerusername/imagename .

5 Push image to Docker hub

Run command

docker push yourdockerusername/imagename

Specify the image in your bitbucket-pipelines.yml file

As documented here: https://confluence.atlassian.com/display/BITBUCKET/Use+Docker+images+as+build+environments+in+Bitbucket+Pipelines

You can use your image on a top level as well as on a specific step like this. 

image: yourdockerusername/imagename
pipelines:
  default:
    - step:
        script:
          - echo "This script runs on all branches that don't have any specific pipeline assigned in 'branches'."
  branches:
    master:
      - step:
          script:
            - echo "This script runs only on commit to the master branch."
    feature/*:
      - step:
          image: yourdockerusername/imagename # This step uses its own image
          script:
            - echo "This script runs only on commit to branches with names that match the feature/* pattern."

Enjoy Bitbucket Pipelines!

 

dismine August 1, 2016

You forget to add one obvious thing for those who know Docker. If you want to build image you should use 64 bit system. 

Like # people like this
Igor Ganapolsky November 14, 2016

I cannot push.  I get an error: 

> An image does not exist locally with the tag

 

even though I built it.

Bradley.McCrorey February 26, 2019

Kind of want to kiss you for this post. thanks so much! <3 

Gnanamadhu March 22, 2019

@sbirgisson Thank you so much. It helped me a lot. But i have a question, how can i cache dockerhub image in pipeline. Cause it always pulls from dockerhub for each step, which means i have three steps and my docker image size is `600MB`. So it pulls that image each and everytime.

I want to cache this docker image, so the next step wont need to pull again. Any solution for this?

 

Thanks.

Auden RovelleQuartz November 19, 2019

one should note that you should be logged in for STEP 5:

docker login -u {{USERNAME}} -p {{PASSWORD}}

 

where 

{{USERNAME}} is to be replaced by your actual hub.docker.com user name

{{PASSWORD}} is to be replaced by your actual hub.docker.com password

1 vote
sbirgisson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 6, 2016
0 votes
Sodiq Fagbola May 31, 2017

In case anyone is still having this issue, I was able to resolve mine by changing the login command to use short flags instead of full parameters

replace --username with -u and --password with -p as shown below

docker login -u $DOCKER_USERNAME -p $DOCKER_USERNAME
Tran Mai October 1, 2017

May I ask how you connect if the server was not specified in the docker login command ? Let's say it's private registry

randy_burgess February 28, 2018

Have you tried tagging it with the fully qualified name of the repository and then pushing it?

0 votes
King May 23, 2017

For weird reason, the forum won't let me post my question; so posting a link to my question in StackOverflow:

https://stackoverflow.com/questions/44146389/bitbucket-pipelines-docker-unauthorized-authentication-required-docker-push

This issue is really puzzling and I wonder if anyone can drop in an insight. Thanks!

Tran Mai October 1, 2017

May I ask how you connect if the server was not specified in the docker login command ?

0 votes
King May 23, 2017

This issue applies to Bitbucket pipelines. I have seen the other related questions, and frankly the Bitbucket yaml and Dockerfile scripts work great when directly executed in bash terminal.

Just when executing the pipeline, it fails, right after successful login.

Here is my Yaml file:

# enable Docker for the repository
options:
docker: true

# Start the pipeline
pipelines:
default:
- step:
script: # Name the image and create a docker image
- docker build -t "my_docker_id/my_app:latest" . 
- docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
- docker push my_docker_id/my_app:latest

And my Dockerfile looks like:

FROM ubuntu:latest

# Install Python and related tools
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get -y upgrade
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN pip3 install --upgrade pip

# Now use Pip to install Flask
RUN pip3 install flask

# Set the working directory to Ubuntu user home
WORKDIR /home/ubuntu

# Then copy the website folder to home folder
COPY . /home/ubuntu

The docker login succeeds:

docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD<1s
Login Succeeded

The pipeline fails on:

docker push my_docker_id/my_app:latest

Failed with:

 + docker push cbrodock/corpsite:latest
The push refers to a repository [docker.io/cbrodock/corpsite]
bdca855fbe4a: Preparing
d147d767df7b: Preparing
f6aad432f761: Preparing
0da658ad5264: Preparing
84e11e4ededf: Preparing
50fd25888dad: Preparing
2cb0a11f65e0: Preparing
ad8ba289abb4: Preparing
33f1a94ed7fc: Preparing
b27287a6dbce: Preparing
47c2386f248c: Preparing
2be95f0d8a0c: Preparing
2df9b8def18a: Preparing
50fd25888dad: Waiting
47c2386f248c: Waiting
2cb0a11f65e0: Waiting
33f1a94ed7fc: Waiting
2df9b8def18a: Waiting
b27287a6dbce: Waiting
2be95f0d8a0c: Waiting
ad8ba289abb4: Waiting
unauthorized: authentication required

If I execute the same exact steps in my bash terminal on MacOS, they execute fine and push succeeds.

Any ideas?

0 votes
King May 23, 2017

Cannot DOCKER PUSH to hub. I have detailed the issue here >> https://stackoverflow.com/questions/44146389/bitbucket-pipelines-docker-unauthorized-authentication-required-docker-push

Will appreciate a response. 

Copy pasting the issue here:

This issue applies to Bitbucket pipelines. I have seen the other related questions, and frankly the Bitbucket yaml and Dockerfile scripts work great when directly executed in bash terminal.

Just when executing the pipeline, it fails, right after successful login.

Here is my Yaml file:

# enable Docker for the repository
options:
docker: true

# Start the pipeline
pipelines:
default:
- step:
script: # Name the image and create a docker image
- docker build -t "my_docker_id/my_app:latest" .
- docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
- docker push my_docker_id/my_app:latest

And my Dockerfile looks like:

FROM ubuntu:latest

# Install Python and related tools
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get -y upgrade
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN pip3 install --upgrade pip

# Now use Pip to install Flask
RUN pip3 install flask

# Set the working directory to Ubuntu user home
WORKDIR /home/ubuntu

# Then copy the website folder to home folder
COPY . /home/ubuntu

The docker login succeeds:

docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD<1s
Login Succeeded

 The pipeline fails on:

docker push my_docker_id/my_app:latest

With the following error:

+ docker push cbrodock/corpsite:latest
The push refers to a repository [docker.io/cbrodock/corpsite]
bdca855fbe4a: Preparing
d147d767df7b: Preparing
f6aad432f761: Preparing
0da658ad5264: Preparing
84e11e4ededf: Preparing
50fd25888dad: Preparing
2cb0a11f65e0: Preparing
ad8ba289abb4: Preparing
33f1a94ed7fc: Preparing
b27287a6dbce: Preparing
47c2386f248c: Preparing
2be95f0d8a0c: Preparing
2df9b8def18a: Preparing
50fd25888dad: Waiting
47c2386f248c: Waiting
2cb0a11f65e0: Waiting
33f1a94ed7fc: Waiting
2df9b8def18a: Waiting
b27287a6dbce: Waiting
2be95f0d8a0c: Waiting
ad8ba289abb4: Waiting
unauthorized: authentication required

If I execute the same exact steps in my bash terminal on MacOS, they execute fine and push succeeds.

Any ideas?

Sodiq Fagbola May 31, 2017

Hello, 

I think you should change your login line to this

docker login -u $DOCKER_USERNAME -p $DOCKER_USERNAME

I had similar issue with using --username and --password

0 votes
Alexandr Marchenko November 11, 2016

Fighting with same issue, just can not understand what is wrong and where

I did managed to build and publish following docker image:

FROM node
RUN mkdir /code
WORKDIR /code
COPY package.json /code
RUN npm install -s

Idea behind it is to not install node dependencies each time

But after trying to use my image i got "sh: 1: webpack: not found"

I can run it locally like so: docker run --rm -v %cd%/dist:/code/dist foo/bar npm run dist -s

and all works as expected, hope somebody can clarify what is happening

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events