Search code examples
dockeransibledocker-registry

Is it possible to force push a docker image to a registry using docker_image Ansible module?


From what I understand, the docker_image Ansible module pushes an image to a registry only if the image is not already present inside the registry with the same tag. In my context, I'm trying to do a very simple workflow that pushes/pulls an image and I don't care about overwriting the image (for now).

However, I'm not sure it is possible to force the push action with this module. If it is not possible, I'm opened to suggestions.

Context

I'm trying to replace a workflow that does the following actions on every run:

  1. Archive the image into a tar file

  2. Transfer the tar file to a distant machine

  3. Load the image from the tar file

I'd like to replace it with a workflow that does the following actions

  1. Push the image to a private ghcr.io registry

  2. Pull the image from the private ghcr.io registry on the distant machine

I have started to implement the below Ansible steps:

- name: Log into GitHub Container Registry (ghcr.io)
  tags: [docker-registry]
  community.docker.docker_login:
    registry_url: ghcr.io
    username: "{{ ghcr_username }}"
    password: "{{ ghcr_password }}"

- name: Push images to registry
  tags: [docker-registry]
  community.docker.docker_image:
    name: "my-image"
    repository: "ghcr.io/my-org/my-repo/my-image:latest"
    push: True
    source: local

Solution

  • In the docs page you linked they give an example of how to do this using the tag latest as an example https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#ansible-collections-community-docker-docker-image-module

    force_tag - Use with state=present to force tagging an image.

    - name: Add tag latest to image
      community.docker.docker_image:
        name: myimage:7.1.2
        repository: myimage:latest
      # As 'latest' usually already is present, we need to enable overwriting of existing tags:
        force_tag: true
        source: local