Run Docker commands in Bitbucket Pipelines

Bitbucket Pipelines allows you to build a Docker image from a Dockerfile in your repository and to push that to a Docker registry, by running Docker commands within your build pipeline. Dive straight in – the pipeline environment is provided by default and you don't need to customize it!

Enable access to Docker

To enable access to Docker daemon, you can either add docker as a service on the step (recommended), or add the global option in your bitbucket-pipelines.yml.

Add Docker as a service in your build step (recommended)

pipelines:
  default:
    - step:
        script:
          - ...
        services:
          - docker

Note that Docker does not need to be declared as a service in the definitions section. It is a default service that is provided by Pipelines without a definition.

Add Docker to all build steps in your repository

options:
  docker: true

Note that even if you declare Docker here, it still counts as a service for Pipelines, has a limit of 1 GB memory, and can only be run with two other services in your build step. This setting is provided for legacy support, and we recommend setting it on a step level so there's no confusion about how many services you can run in your pipeline.

動作の仕組

Configuring Docker as a service will:

  • mount the Docker CLI executable in your build container
  • run and provide your build access to a Docker daemon

You can verify this by running docker version:

pipelines:
  default:
    - step:
        script:
          - docker version
        services:
          - docker

You can check your bitbucket-pipelines.yml file with our online validator.

Running Docker commands

Inside your Pipelines script, you can run all Docker commands including:

  • docker container
  • docker image
  • docker network
  • docker system
  • docker volume
  • docker attach
  • docker build
  • docker commit
  • docker cp
  • docker create
  • docker diff
  • docker events
  • docker exec
  • docker export
  • docker history
  • docker images
  • docker import
  • docker info
  • docker inspect
  • docker kill
  • docker load
  • docker login
  • docker logout
  • docker logs
  • docker pause
  • docker port
  • docker ps
  • docker pull
  • docker push
  • docker rename
  • docker restart
  • docker rm
  • docker rmi
  • docker run
  • docker save
  • docker search
  • docker start
  • docker stats
  • docker stop
  • docker tag
  • docker top
  • docker unpause
  • docker update
  • docker version
  • docker wait

Pipelines prevents the execution of Docker swarm-related commands, docker run --privileged, and mapping volumes with a source outside $BITBUCKET_CLONE_DIR for security reasons on our shared build infrastructure.

See the Docker command line reference for information on how to use these commands.

Using an external Docker daemon

If you have configured your build to run commands against your own Docker daemon hosted elsewhere, you can continue to do so. In this case, you should provide your own CLI executable as part of your build image (rather than enabling Docker in Pipelines), so the CLI version is compatible with the daemon version you are running.

Authenticate when pushing to a registry

To push images to a registry, you need to use docker login to authenticate prior to calling docker push

For example, add this to your pipeline script:

docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD
最終更新日: 2017 年 12 月 4 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.