Docker in Continuous Integration: Part 2

- By Abhijit Kini

BACK TO BLOG HOME

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:

  1. Use an existing Docker image
  2. Build your own Docker image
  3. 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

 

Sample shippable.yml:

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"

Example:

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.

  1. Log in to Shippable using your GitHub credentials
  2. Skip steps 3 and 4 if you have followed the example of using your own custom Docker image in the previous blog
  3. 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
  4. Enable the 'sample_nodejs' project
  5. Add a  Dockerfile at the root of your repository to specify a base image. Use the sample below for this walkthrough. Customize the Dockerfile based 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

  6. Copy the values of  shippable.yml file from the above sample. Replace the following values: 
    • image_name: In the pre_ci  and pre_ci_boot  sections, replace abhijitkini/testimage with your docker-hub-username/docker-hub-image-repo. For Google Container Registry or Amazon EC2 Container Registry, refer our documentation
    • image_tag: In the pre_ci  and pre_ci_boot  sections, replace latest with the tag of the image you want to use
    • pull: Set to false  to ensure the image built in the pre_ci  section 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 run command. You can also include Home environment variable as shown, if it isn't set in your image
  7. Commit the shippable.yml and the Dockerfilechanges, which automatically triggers a build on Shippable
  8. Shippable builds your custom Docker image from the base image specified in the Dockerfile and uses it in the CI process. A successful build is shown below
    Docker in Continuous Integration process

 

Important

  • The pre_ci section in the shippable.yml file 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
    • Pull an image from a Docker registry by first configuring an integration in the UI and updating the shippable.yml file. Read more details and follow this tutorial
  • Commands included in the pre_ci section 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.

Conclusion

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".


Try Shippable

Topics: Docker, continuous integration (CI), Docker Hub, CI/CD, tutorial, tips