Search code examples
dockeransibledockerfiledockerpy

Building Dockers with Ansible docker_image


I'm trying to build a bunch of dockers on a machine using Ansible's docker_image module.

I build 1 "base" docker image, which is used as the FROM image in all subsequent dockers images. This works when issuing the build commands manually as:

sudo docker build -t base .
sudo docker build -t postgres .

But when I try to do the same with the Ansible module the second image (and all subsequent images that uses the "base" image) fails with the following error:

TASK: [Docker | Build postgres] ************************************ 
failed: [192.168.1.120] => {"changed": true, "failed": true, "image_id": null}
msg: Error: Error: image base:latest not found
Log:Step 0 : FROM base


FATAL: all hosts have already failed -- aborting

The entries in my Playbook is:

  - name: Docker | Build base
      docker_image: path="/home/xx/data/dockers/base/" name="base" state=present

    - name: Docker | Build postgres
      docker_image: path="/home/xx/data/dockers/postgresql/" name="postgres" state=present

When it fails the "base" image exists on the machine and I can verify it by checking docker images. The follow up images (in this case postgres) also builds without fail when doing a manual build.

Relevant extracts from the Dockerfiles:

Base Dockerfile:

FROM ubuntu

MAINTAINER me

RUN apt-get update

RUN apt-get install -y \
    software-properties-common \
    wget \
    git \
    unzip \
    nano \
    vim-tiny

CMD bash

Postgres Dockerfile:

FROM base

MAINTAINER me

RUN groupadd -r postgres && useradd -r -g postgres postgres

...

So Ansible struggles to build an image using another image as a base image. I'm sure that the issue isn't with the Dockerfiles because I can build the images manually. I'm just trying to automate the build with Ansible and that's giving me the issue.

Any advice?


Solution

  • The issue here is with the docker-py module which has had some update recently that will probe the hub for the image first. docker-py does not use the command-line tools, it uses its own implementation of the docker api and does things in its own way.

    I would recommend you set up a private hub. It will take you an afternoon to get used to the idea, and start prefixing your container images with it. Push as you build your images. Pull as you start them up. Separate these concerns in your playbooks. In some cases forget using the ansible docker modules and just go with calling docker cli through ansible's shell module. Here is a good article on ansible and docker. http://opensolitude.com/2015/05/26/building-docker-images-with-ansible.html