Optimizing Bitbucket Pipelines with Custom Docker Images
Tags: webdev, howto, docker, bitbucket
Published: 2023-02-16 02:27:00

In this article, we will discuss the process of customizing an existing [Docker Hub](https://hub.docker.com/) image, installing the required libraries and packages, building the new image, and pushing it to your [Docker Hub](https://hub.docker.com/) account.

Suppose your project requires the [Docker Hub](https://hub.docker.com/) image [`php:8.0.26-cli`](https://hub.docker.com/layers/library/php/8.0.26-cli/images/sha256-00b971a0e1ffbd9c13cfa819638130da9a5b22ac0092369780bfa98868861d5c) 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](https://docs.docker.com/get-started/overview/#the-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:

```bash
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 \
		zlib1g-dev

RUN curl -sSLf \
        -o /usr/local/bin/install-php-extensions \
        https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/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 https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
```

* Save and close the file.
* Login to your [Docker Hub](https://hub.docker.com/) 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:
```bash
docker image build --platform linux/amd64 -t foobar/custom-cicd-php-8-cli:1.0.0 ./
```
  In the above command:

 * `foobar` should be replaced with your [Docker Hub](https://hub.docker.com/) 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](https://hub.docker.com/) account using the following command:

```bash
docker push foobar/custom-cicd-php-8-cli:1.0.0
```


* Upon successful push, confirm the changes in [Docker Hub](https://hub.docker.com/) 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.