Configuring the Docker task in Bamboo


The Docker task in Atlassian Bamboo allows you to use Docker images and containers in your Bamboo builds and deployments.

Before you begin

  • Make sure you have Docker installed. We advise to use the most recent version. 
  • Define a Docker capability in Bamboo. See Defining a new Docker capability

  • If you're using Bamboo on Windows, you can't run Docker commands directly from the Windows command line. To use Docker tasks with Bamboo Windows, run Docker Machine.


The Docker task supports the following Docker actions:


Build a Docker image

Builds a Docker image based on the specified Dockerfile. The Dockerfile may be provided as an existing file in the task's working directory or defined in the task configuration.The image is stored in Docker's local image installation directory and can be used by subsequent Docker tasks in the job. You can optionally save the image to a file in the working directory which can then be packaged as a build artifact.

To build a Docker image in Bamboo:

  1. Create a new Docker task for the relevant job. See Configuring tasks.
  2. Add a Task description.
  3. Use the Disable this task checkbox to control whether the task gets run.
  4. Use the Add condition to task checkbox to make task run only when a certain condition is met. 
    You can find conditions on Atlassian Marketplace or implement your own.  
  5. Select the Build a Docker image command and complete the settings. See more information about the settings below:

Build a Docker image...

Repository

Docker task type configuration

The repository name (and optionally a tag) to be applied to the resulting image, following this pattern:

registry.address:port/namespace/repository:tag

Only repository is mandatory.

Dockerfile

Use either an existing Dockerfile (located in the working directory for the task), or specify the contents of the Dockerfile.

Do not use cache when building the image

By default, Docker will reuse a cached build during the next build. See the Docker documentation.

Select Do not use cache... to ensure that the new image will include changes since the last build. Note that this may incur a performance penalty.

Save the image as a file

Specify the directory location and file name. Optionally configure a job artifact to pass it to next stages and deployments.


If required, specify advanced options: 

Environment variables

(Optional) Additional system environment variables that you want to pass to your build. Note that existing environment variables are automatically available to the executable. You can also include Bamboo global or build-specific variables (see Bamboo variables).
Multiple variables should be separated with spaces. Parameters with spaces must be quoted (e.g JAVA_OPTS="-Xms200m -Xmx700m").

Working subdirectory

(Optional) An alternative subdirectory, relative to the job's root directory, where Bamboo will run the executable. The root directory contains everything checked out from the job's configured source repository. If you leave this field blank, Bamboo will look for build files in the root directory. This option is useful if your task has a build script in a subdirectory and the executable needs to be run from within that subdirectory.


Save your changes!

Run a Docker container

Starts a Docker container based on the specified image.

By default, the task's working directory is mounted and used as the Docker container's working directory, but you can specify your own settings.

By default, the container is removed on completion of the task, but you can select Detach container to have the container continue to run after a deployment project completes. Containers can be linked to detached containers started by preceding tasks in a job by selecting the Link to detached containers option.

Note that a non-detached container that fails to start will not be removed when the Bamboo task completes. See this KB article for more details.

To run a Docker container in Bamboo:

  1. Create a new Docker task for the relevant job. See Configuring tasks for details.
  2. Add a Task description to help remind you why you created the task.
  3. Use the Disable this task checkbox to control whether the task gets run.
  4. Use the Add condition to the task checkbox to make task run only when a certain condition is met. 
    You can find conditions on Atlassian Marketplace or implement your own. 
  5. Select the Run a Docker container command and complete the settings. See more information about the settings below:

Run a Docker container...
Docker image 


The image you want to use to instantiate the Docker container.
Detach container

Allows you to run the container in the background, after a deployment project completes.

Specify a Container name that isn't used by other containers in this job.

Select Add port mapping to specify mappings that bind ports inside the container to ports on the host.

Wait for service to start

Allows you to specify how long Bamboo should wait for the service to become available.

You need to specify a pattern for the URL that Bamboo should check, and a timeout period.

Link to detached containers
Allows you to link containers to detached containers started by preceding tasks in a job.
Container environment variables

Allows you to specify parameters to pass to the container, for example JAVA_OPTS="-Xmx256m -Xms128m".

Separate multiple parameters with spaces. Parameters with spaces must be quoted.

Container command
The command to run in the Docker container.
Container working directory
The working directory for the container.
Additional arguments

Additional Docker run options.

Argument strings that have values which contain spaces require the entire argument string be contained within quotes, that is, the parameter marker, the parameter, the equals sign, and the value containing spaces. For example:

"--health-cmd=mysqladmin ping || exit 1"
Volumes

You can mount additional host directories as data volumes inside the container.


If required, specify advanced options: 

Environment variables
(Optional) Additional system environment variables that you want to pass to your build. Note that existing environment variables are automatically available to the executable. You can also include Bamboo global or build-specific variables (see Bamboo variables). 
Multiple variables should be separated with spaces. Parameters with spaces must be quoted (e.g JAVA_OPTS="-Xms200m -Xmx700m").
Working subdirectory
(Optional) An alternative subdirectory, relative to the job's root directory, where Bamboo will run the executable. The root directory contains everything checked out from the job's configured source repository. If you leave this field blank, Bamboo will look for build files in the root directory. This option is useful if your task has a build script in a subdirectory and the executable needs to be run from within that subdirectory.


Save your changes!

Push a Docker image to a Docker registry

Pushes a Docker image to a Docker registry. This may be the central Docker Hub registry or a custom registry.

To push a Docker repository from Bamboo to a registry:

  1. Create a new Docker task for the relevant job. See Configuring tasks for details.
  2. Add a Task description to help remind you why you created the task.
  3. Use the Disable this task checkbox to control whether the task gets run.
  4. Use the Add condition to the task checkbox to make task run only when a certain condition is met. 
    You can find conditions on Atlassian Marketplace or implement your own. 
  5. Select the Push a Docker image to a Docker registry command and complete the settings. See more information about the settings below:

Push a Docker image to a Docker registry...
Registry

Select to push to either Docker Hub or a custom registry.
Repository
For Docker Hub you must specify the repository name and optionally a tag, for example: namespace/repository:tagFor a custom registry you must specify the registry address, repository name and optionally a tag, for example: registry.address:port/namespace/repository:tag
Authentication type

Select how you'd like to authenticate:

  • use agents' dockercfg file
  • use username and password
  • select predefined shared credentials from the drop-down list


If required, specify advanced options: 

Environment variables
(Optional) Additional system environment variables that you want to pass to your build. Note that existing environment variables are automatically available to the executable. You can also include Bamboo global or build-specific variables (see Bamboo variables).
Multiple variables should be separated with spaces. Parameters with spaces must be quoted (e.g JAVA_OPTS="-Xms200m -Xmx700m").
Working subdirectory
(Optional) An alternative subdirectory, relative to the job's root directory, where Bamboo will run the executable. The root directory contains everything checked out from the job's configured source repository. If you leave this field blank, Bamboo will look for build files in the root directory. This option is useful if your task has a build script in a subdirectory and the executable needs to be run from within that subdirectory.


Save your changes!

Advanced authentication

The push task allows you to define username, password and email for authentication purposes. 

If the other tasks require authentication, or if you want to share credentials between all builds for certain agents, it's possible to create the docker configuration file on the agent itself, named ~/.dockercfg:

{
  "https://index.docker.io/v1/": {
    "auth": "xXxXxXxXxXx=",
    "email": "username@example.com"
  },
  "https://index.example.com": {
    "auth": "XxXxXxXxXxX=",
    "email": "username@example.com"
  }
}

When using the push task, leave authentication fields empty in order to use dockercfg instead. 

Docker command updates ~/.dockercfg file; if you have any configuration management tool in place updating this file (like puppet, chef or ansible), make sure it's not executing while you are running a docker build. 

Troubleshooting

No space left on device

Docker stores its images in a local image installation directory. Over time this directory may grow to consume all of the available disk space. When this occurs you should remove unused images by running the docker rmi command.

The following Docker issues affecting disk space may provide further information:

Permission denied on files created within a Docker container

Docker runs processes inside containers as the root user. This means files created on mounted volumes are owned by the root user and not by the user running the Docker command (the bamboo agent user). This may cause an issue if a subsequent task requires access to those files on the host.

Docker plans to allow mapping between container and host users in the future. Until then, you can work around this issue by changing the owner of the files in the mounted volume to the host user:

  • Supply the host user's id and group id to the container by setting the following environment variables in the Docker run task configuration:
    • HOST_UID=$UID
    • HOST_GID=$GID
  • Run a script inside the container to change the owner of the files in the mounted volume: 

    chown -R $HOST_UID:$HOST_GID /<path_to_mounted_volume>

Permission denied when running Docker

When attempting to run a Docker container you may see a permission denied issue:

2015/02/10 06:35:31 Post http:///var/run/docker.sock/build?rm=1&t=docker-toy-demo: dial unix /var/run/docker.sock: permission denied

The solution is to add the Bamboo user agent to the Docker group on the agent.

Getting execution errors for valid docker files or unable to start docker container

Example build output:

 Driver devicemapper failed to get image rootfs 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158: Error mounting '/dev/mapper/docker-202:16-17252355-511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158' on '/mnt/docker/devicemapper/mnt/511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158': invalid argument
simple    12-Feb-2015 12:12:14    Failing task since return code of [/usr/bin/docker build --no-cache=true --tag="docker.atlassian.io/dk:9.3" /home/bamboo/bamboo-agent-home/xml-data/build-dir/dkr-build-JOB1] was 1 while expected 0
error    12-Feb-2015 12:12:14    Error occurred while running Task 'Build docker image(5)' of type com.atlassian.bamboo.plugins.bamboo-docker-plugin:task.docker.cli.
error    12-Feb-2015 12:12:14    com.atlassian.bamboo.task.TaskException: Failed to execute task
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.plugins.docker.service.BuildService.execute(BuildService.java:53)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.plugins.docker.tasks.cli.DockerCliTask.execute(DockerCliTask.java:60)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.task.TaskExecutorImpl$3.call(TaskExecutorImpl.java:281)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.task.TaskExecutorImpl$3.call(TaskExecutorImpl.java:278)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.task.TaskExecutorImpl.executeTaskWithPrePostActions(TaskExecutorImpl.java:198)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.task.TaskExecutorImpl.executeTasks(TaskExecutorImpl.java:278)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.task.TaskExecutorImpl.execute(TaskExecutorImpl.java:105)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.build.pipeline.tasks.ExecuteBuildTask.call(ExecuteBuildTask.java:75)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.v2.build.agent.DefaultBuildAgent.build(DefaultBuildAgent.java:188)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.v2.build.agent.BuildAgentControllerImpl.waitAndPerformBuild(BuildAgentControllerImpl.java:112)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.v2.build.agent.DefaultBuildAgent$1.run(DefaultBuildAgent.java:110)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.utils.BambooRunnables$1.run(BambooRunnables.java:49)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.security.ImpersonationHelper.runWith(ImpersonationHelper.java:31)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.security.ImpersonationHelper.runWithSystemAuthority(ImpersonationHelper.java:20)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.security.ImpersonationHelper$1.run(ImpersonationHelper.java:52)
error    12-Feb-2015 12:12:14        at java.lang.Thread.run(Thread.java:745)
error    12-Feb-2015 12:12:14    Caused by: com.atlassian.bamboo.plugins.docker.client.DockerException: Error running Docker build command
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.plugins.docker.client.DockerCmd.build(DockerCmd.java:149)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.plugins.docker.service.BuildService.execute(BuildService.java:40)
error    12-Feb-2015 12:12:14        ... 15 more
error    12-Feb-2015 12:12:14    Caused by: com.atlassian.utils.process.ProcessException: Error executing /usr/bin/docker build --no-cache=true --tag="docker.atlassian.io/dk:9.3" /home/bamboo/bamboo-agent-home/xml-data/build-dir/DDT-REP-JOB1
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.plugins.docker.process.DockerTaskProcessService.execute(DockerTaskProcessService.java:57)
error    12-Feb-2015 12:12:14        at com.atlassian.bamboo.plugins.docker.client.DockerCmd.build(DockerCmd.java:145)
error    12-Feb-2015 12:12:14        ... 16 more


If the agent consistently fails executing docker run commands, either when building an image or running an instance there is a risk that you've run into https://github.com/docker/docker/issues/4036. To help diagnose this you can SSH to the agent and look at the kernel messages by running:

dmesg

There are several possible messages that indicate this problem. Some of those are listed here:

[83471099.881879] JBD2: no valid journal superblock found
[83471099.881883] EXT4-fs (dm-2): error loading journal
[88401612.723018] EXT4-fs (dm-1): warning: mounting fs with errors, running e2fsck is recommended
[88401612.724764] EXT4-fs (dm-1): mounted filesystem with ordered data mode. Opts: discard
[88401612.744549] EXT4-fs error (device dm-1): ext4_lookup:1448: inode #2: comm docker: deleted inode referenced: 131073


There is a big risk that the device mapper is corrupt. This means that you need to stop Docker and remove the files used by devicemapper, then restart Docker. If running on an elastic agent, terminating the agent and starting a new one is also a viable option.

To stop Docker and remove the files, run the following:

sudo -i
#stop the docker daemon
service docker stop
#remove the broken devicemapper files
rm -rf /var/lib/docker 
service docker start

The location of the devicemapper files may differ from the example above. Run the following to find the exact path:

docker info
Last modified on Jun 2, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.