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:
- Create a new Docker task for the relevant job. See Configuring tasks.
- Add a Task description.
- Use the Disable this task checkbox to control whether the task gets run.
- 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. Select the Build a Docker image command and complete the settings. See more information about the settings below:
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:
- Create a new Docker task for the relevant job. See Configuring tasks for details.
- Add a Task description to help remind you why you created the task.
- Use the Disable this task checkbox to control whether the task gets run.
- 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. Select the Run a Docker container command and complete the settings. See more information about the settings below:
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:
- Create a new Docker task for the relevant job. See Configuring tasks for details.
- Add a Task description to help remind you why you created the task.
- Use the Disable this task checkbox to control whether the task gets run.
- 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. Select the Push a Docker image to a Docker registry command and complete the settings. See more information about the settings below:
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:
- Device-mapper does not release free space from removed images
- Graph deletes are non-atomic, db refs deleted without deleting on-disk entities
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