Git Product home page Git Product logo

ansible-docker's Introduction

Description

Ansible playbook for building docker images

Why

While docker containers is useful for various tasks process of their creation leaves something to be desired:

  1. RUN instructions in Dockerfile limited to simple commands
  2. Poor reusability. You can't reuse RUN command from another Dockerfile without inheriting from it or copying it
  3. What if you want recreate docker image on bare metal, VM or some other container runtime?

To solve this problems we could use existing provisioning tools such as Ansible to build docker images.

How

  1. Install Ansible. Version 2.0 required to use new docker connection driver
  2. Modify Dockerfile for your needs: select another base images, add additional packages required by used Ansible modules
  3. In playbook site.yml change image name and container name
  4. Add new Ansible roles/tasks if needed

Run

ansible-playbook ansible/site.yml

after that you should have image with expected name. Tag and push it if needed. Actual only tag=latest is supported with cache layer.

Under the hood

You maybe wondering why not just install ansible in container, mount playbook directory and run it like that

ansible-playbook foo.yml

There is some cons of this approach:

  1. You have to install ansible inside container
  2. You lose all benefits of docker filesystem caching: every time you run Ansible it have to execute all tasks in playbook from the beginning

Approach used in this playbook is a little different.

On start we create named image from some Dockerfile and start named container from this images. After container started we connect to it as if it was usual host. Except instead of SSH we use docker connection driver. This way we don't have to install ssh server in container. Then we run Ansible role and tasks on container and after that we stop it.

To use docker caching mechanism we use additional callback plugin. Every time Ansible task succesfuly changes state of running container we commit it with unique tag. Since Ansible tasks should be idemponent new images would be created only when something changed.

Acknowledgement

Idea and initial implementation of caching plugin from article Provisioning Docker containers with Ansible by Xavier Bruhiere

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.