Sven van de Scheur Sven van de Scheur - 6 months ago 25
Linux Question

Ansible and docker: locally build image get pulled and causes failure

Im using Ansible to provision my server with anything required to make a my website work. The goal is to install a base system and provide it with docker containers running apps (at the moment it's just one app).

The problem I'm facing is that my docker image isn't hosted at dockerhub or something else. Instead it's being built by an Ansible task. However, when I'm trying to run the built image, Ansible tries to pull it (which isn't possible) and then dies.

This is what the playbook section looks like:

- name: check or build image
docker_image:
path=/srv/svenv.nl-docker
name='svenv/svenv.nl'
state=build

- name: start svenv/svenv.nl container
docker:
name: svenv.nl
volumes:
- /srv/svenv.nl-docker/data/var/lib/mysql/:/var/lib/mysql/
- /srv/svenv.nl-docker/data/svenv.nl/svenv/media:/svenv.nl/svenv/media
ports:
- 80:80
- 3306:3306
image: svenv/svenv.nl


When I run this, a failure indicates that the svenv/svenv.nl get's pulled from the repository, it isn't there so it crashes:

failed: [vps02.svenv.nl] => {"changes": ["{\"status\":\"Pulling repository svenv/svenv.nl\"}\r\n", "{\"errorDetail\":{\"message\":\"Error: image svenv/svenv.nl:latest not found\"},\"error\":\"Error: image svenv/svenv.nl:latest not found\"}\r\n"], "failed": true, "status": ""}
msg: Unrecognized status from pull.

FATAL: all hosts have already failed -- aborting


My question is:

How can I


  • Build a local docker

  • Then start it as a container without pulling it



Any help would be appreciated.

Regards,

Sven van de Scheur

Answer

You are hitting this error:

Ansible is attempting to create a container, but the create is failing with:

docker.errors.InvalidVersion: mem_limit has been moved to host_config in API version 1.19

Unfortunately, there is catch-all except: that is hiding this error. The result is that rather than failing with the above message, ansible assumes that the image is simply missing locally and attempts to pull it.

You can work around this by setting docker_api_version to something earlier than 1.19:

- name: start svenv/svenv.nl container
  docker:
    name: svenv.nl
    ports:
    - 80:80
    - 3306:3306
    image: svenv/svenv.nl
    docker_api_version: 1.18