Using docker service to pass arguments to service containers

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問


プラットフォームについて: Cloud のみ - この記事は、 クラウド プラットフォームのアトラシアン製品にのみ適用されます。

要約

When using service containers in Bitbucket Pipelines, your use case might require passing some arguments when starting the service. Currently, the ability to pass arguments to service containers is being tracked in the feature request BCLOUD-21810 - Allow passing arguments to service containers. This article describes a workaround approach to pass variables to the service container while the native feature is not implemented.

環境

Bitbucket Pipelines on Bitbucket Cloud

回避策

As passing arguments to service containers is not yet natively supported, the workaround is to manually start the service container using the docker service. When using the docker service in your pipeline, a docker CLI is mounted into 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 docker login to the docker registry. Also, when starting a new container, it will be already connected to the same bridge network of the build container, but a port mapping is necessary to allow a connection from the build container to the "service" container.

In the following example YAML configuration, docker run is used to start 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 service

Where the 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 usually 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 already be able to connect to the service container using the port that was mapped in the docker run command.


最終更新日: 2024 年 1 月 23 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.