DEV Community

Ibrar Hussain
Ibrar Hussain

Posted on • Originally published at Medium

Optimizing Bitbucket Pipelines with Custom Docker Images

In this article, we will discuss the process of customizing an existing Docker Hub image, installing the required libraries and packages, building the new image, and pushing it to your Docker Hub account.

Suppose your project requires the Docker Hub image php:8.0.26-cli for your Bitbucket pipeline, but the installation of required libraries and packages, such as composer, git, curl, rsync, zip, mysql-client, gd, and grpc, is taking too long. In such a situation, you need to create a custom image and pre-install all the required packages, which will reduce the pipeline build time.

Here are the steps to create a custom Docker image:

  • Make sure that you have installed Docker Client, if not, then you need to install it first.
  • Create a new folder foo-custom-image.
  • Inside this folder create a new file Dockerfile.
  • Open the Dockerfile and add the following:
From php:8.0.26-cli

RUN apt-get update && apt-get install -qy \
        unzip \
        git \
        curl \
        rsync \
        libmcrypt-dev \
        libzip-dev \
        zip \
        default-mysql-client \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \

RUN curl -sSLf \
        -o /usr/local/bin/install-php-extensions \ && \
    chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions grpc-1.45.0

RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_mysql zip gd

RUN curl -sS | php -- --install-dir=/usr/local/bin --filename=composer
Enter fullscreen mode Exit fullscreen mode
  • Save and close the file.
  • Login to your Docker Hub account using the command docker login. This command will ask for your credentials, and if all goes well, then you will get a success message

  • Build the docker image using the following command:

docker image build --platform linux/amd64 -t foobar/custom-cicd-php-8-cli:1.0.0 ./
Enter fullscreen mode Exit fullscreen mode

In the above command:

  • foobar should be replaced with your Docker Hub account username.
  • custom-cicd-php-8-cli is the name of the image, which can be changed to anything.
  • 1.0.0 is the tag version

NOTE: This command can take up to 45 minutes, as grpc take quite a long time to install.

  • Once build is successfully finished, you can push the image to your Docker Hub account using the following command:
docker push foobar/custom-cicd-php-8-cli:1.0.0
Enter fullscreen mode Exit fullscreen mode
  • Upon successful push, confirm the changes in Docker Hub account.

  • You can delete your local docker image(s) by following these steps:

    • Run docker images to list the images on your local machine.
    • From the list, copy the Image ID for your docker image that you want to delete.
    • Run docker rmi [Image ID]
    • This command should delete the docker image with provided Image ID
  • Once everything is ready, now you can use this custom image in your Bitbucket pipeline and test your pipeline for build time.

Top comments (0)