I am trying to run a script (unitest) that uses docker behind the scenes on a CI. The script works as expected on droneci but switching to CloudBuild it is not clear how to setup DinD.
For the droneci I basically use the DinD as shown here my question is, how do I translate the code to Google CloudBuild. Is it even possible?
I searched the internet for the syntax of CloudBuild wrt DinD and couldn’t find something.
2
Answers
I managed to figure out a way to run Docker-in-Docker (DinD) in CloudBuild. To do that we need to launch a service in the background with docker-compose. Your
docker-compose.yml
script should look something like this.In my case, I had no problem using versions 18.03 or 18.09, later versions should also work. Secondly, it is important to attach the container to the cloudbuild network. This way the dind container will be on the same network as every container spawned during your step.
To start the service you need to add a step to your
cloudbuild.yml
file.To validate that the dind service works as expected, you can just create a ping step.
Now if it works you can run your script as normal with dind in the background. What is important is to pass the
DOCKER_HOST
env variable so that the docker client can locate the docker engine.Take note, any container spawned by your script will be located in dind-service, thus if you are to do any request to it you shouldn't do it to
http://localhost
but instead to thehttp://dind-service
. Moreover, if you are to use private images you will require some type of authentication before running your script. For that, you should rungcloud auth configure-docker --quiet
before running your script. Make sure your docker image has gcloud installed. This creates the required authentication credentials to run your app. The credentials are saved in path relevant to the$HOME
variable, so make sure your app is able to access it. You might have some problems if you use tox for example.Cloud Build lets you create Docker container images from your source code. The Cloud SDK provides the
container build
subcommand for using this service easily.For example, here is a simple command to build a Docker image:
gcloud builds submit -t gcr.io/my-project/my-image
This command sends the files in the current directory to Google Cloud Storage, then on one of the Cloud Build VMs, fetch the source code, run Docker build, and upload the image to Container Registry
By default, Cloud Build runs docker build command for building the image. You can also customize the build pipeline by having custom build steps.If you can use any arbitrary Docker image as the build step, and the source code is available, then you can run unit tests as a build step. By doing so, you always run the test with the same Docker image. There is a demonstration repository at cloudbuild-test-runner-example. This tutorial uses the demonstration repository as part of its instructions.
I would also recommend you to have a look at these informative links with similar use case: