Pass arguments to Docker service containers in Bitbucket Pipelines on Cloud
Platform Notice: Cloud Only - This article only applies to Atlassian apps on the cloud platform.
Summary
When using service containers in Bitbucket Pipelines, you sometimes need to pass arguments when starting the service. This article has a workaround to pass variables until this feature is implemented on Bitbucket Cloud.
Solution
Manually start the service container using the Docker service
When using the Docker service in your pipeline, a Docker CLI is mounted in the build container and is connected to a Docker daemon that runs in a separate container. This allows you to run Docker commands as part of your build, and one of the available commands is docker run, which can be used to start new containers. With that in mind, the docker run command can be used as part of your build's script to manually start a new container, which will act as the service container:
docker run -p <port mapping> -e VAR1="VAR_VALUE" <image> <arguments to pass to service container>Any public or private Docker images can be used to start the new container, although for private images it will be necessary to first use docker login to connect to the Docker registry. Also, when starting a new container it will already be connected to the same bridge network as the build container, but a port mapping is necessary to allow a connection from the build container to the "service" container.
Example YAML configuration
In the following example YAML configuration, docker run starts a new database container using the public MySQL image from Dockerhub.
image: atlassian/default-image:3
pipelines:
default:
- step:
name: Testing database service container
script:
- docker run --name mysql-container -p 3306:3306 -e MYSQL_DATABASE="frn_api_test" -e MYSQL_ALLOW_EMPTY_PASSWORD="true" mysql:8.0.28 --default-authentication-plugin=mysql_native_password # starting a new "service" container using mysql image
- apt-get update && apt-get install -y mysql-client # install mysql-client to test the connection
- sleep 30 #wait for 30 seconds for the service container to have enough time to start
- mysql --host="127.0.0.1" --user=root # test connection to the mysql container started previously
services:
- docker #define that this step will use docker serviceThe docker run arguments are:
--name : the name of the new container (optional)
-p : mapping the port 3306 of the build container to the same port on the MySQL container. 3306 is the default port for MySQL database. When starting a new container, it will be already connected to the same bridge network of the build container.
-e : passing environment variables to the new container (optional)
--default-authentication-plugin=mysql_native_password : custom MySQL argument passed to MySQL image's entry point command
After the docker run command is executed, it's recommended to add a sleep command for a few seconds (~30 seconds) so the new container has enough time to start and load all its dependencies.
Following the sleep command, you should be able to connect to the service container using the port that was mapped in the docker run command.
The ability to pass arguments to service containers is being tracked in feature request BCLOUD-21810 - Allow passing arguments to service containers.
Was this helpful?