This blog explains "Build your own Docker image" scenario in the Docker in Continuous Integration (CI) series. The three scenarios covered in this series are:
- Use an existing Docker image
- Build your own Docker image
- Push a Docker image to a registry of your choice
We'll go over the reasons to build your own Docker image, steps to do so and an example to follow along.
Build your own Docker image
Shippable has 50+ standard images that supports multiple combinations of langauage, service, platform and tools. Use standard images when it fulfills your CI requirements or if you don't use Docker in your CI process. Shippable also provides the option of building your own Docker image as part of your CI/CD workflow.
When to use this option
Users need to build their own Docker image for the following reasons:
- Need to optimize your Docker image to speed up builds for your applications
- Need to use a language, service or version that is not included in the standard images
- Need to use multiple languages in your CI process
language: node_js node_js: - 0.12 build: pre_ci: - docker build -t abhijitkini/testimage:latest . pre_ci_boot: image_name: abhijitkini/testimage image_tag: latest pull: false env: FOO=bar options: "-e HOME=/root" ci: - echo "CI is running"
In this example, we'll build a docker image using a base ubuntu 14.04 image, prior to running CI. We'll use this image to boot the build container. We'll end by running CI with a simple echo.
Want to take it beyond the example? Use Shippable Pipelines to rebuild all dependent images, applications and trigger the rest of your pipeline, everytime your base image is updated.
- Log in to Shippable using your GitHub credentials
- Skip steps 3 and 4 if you have followed the example of using your own custom Docker image in the previous blog
- Follow along by forking this Node.js sample application (sample_nodejs), available for free on GitHub. Alternatively, follow along based on your own repo. If you have never used Shippable, take 30 mins to complete the getting started tutorial
- Enable the 'sample_nodejs' project
- Add a
Dockerfileat the root of your repository to specify a base image. Use the sample below for this walkthrough. Customize the
Dockerfilebased on your requirements.
FROM ubuntu:14.04 RUN mkdir -p /tmp/logs ADD . /src
- This base image is used to build your custom image in the next step
- Copy the values of
shippable.ymlfile from the above sample. Replace the following values:
image_name: In the
abhijitkini/testimagewith your docker-hub-username/docker-hub-image-repo. For Google Container Registry or Amazon EC2 Container Registry, refer our documentation
image_tag: In the
latestwith the tag of the image you want to use
pull: Set to
falseto ensure the image built in the
pre_cisection is used to boot the build container
env: Specify environment variables you want inside your build container
options: Enter Docker options for use in the
docker runcommand. You can also include Home environment variable as shown, if it isn't set in your image
- Commit the
Dockerfilechanges, which automatically triggers a build on Shippable
- Shippable builds your custom Docker image from the base image specified in the
Dockerfileand uses it in the CI process. A successful build is shown below
pre_cisection in the
shippable.ymlfile is used either to build an image (as shown above) or pull an image from a Docker registry to customize the environment for your build container
- Commands included in the
pre_cisection are run outside the build container. This means commands and environment variables used in this section are not available within your build container
- After building your own image, you can "Push the Docker image to a registry of your choice" (Scenario three) in your CI process. Learn more about pushing the Docker image in the next post.
Build your own Docker image to customize it and speed up builds, or when your requirement is not included in Shippable's default images, or when you use multipe langauges in your CI process. Use this built image, to override the default images, and set specific options to boot the build container.
In the next blog, we'll go over scenario three - "Push a Docker image to a registry of your choice".