admin管理员组

文章数量:1315033

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-/my-repo/my-image:latest"
    push: True
    source: local

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-/my-repo/my-image:latest"
    push: True
    source: local
Share Improve this question edited Feb 2 at 7:37 Zeitounator 44.8k7 gold badges63 silver badges76 bronze badges asked Jan 30 at 10:21 oursours 371 silver badge9 bronze badges 4
  • It's a little tricky to use CLI tools to check if the image exists remotely (but see Check if image:tag combination already exists on docker hub) and docker push's usual behavior is to unconditionally push the image, with no option to check first. Are you sure docker_image (or docker_image_push) does a similar check? I don't see it mentioned in its documentation page. – David Maze Commented Jan 30 at 10:59
  • With the code above, I need to manually delete the image with the same tag from the GitHub Package UI before I can push my image. However, if my registry is empty, it will push the image. In both cases, the Ansible step is marked ok. As you said, I didn't find any info on this behavior in the documentation so I'm a bit confused. – ours Commented Jan 30 at 16:30
  • 1 Ansible is meant to achieve a state (that you describe in a task). If you delete something that is not there, you will get an OK state. If it were to delete something, then you would get a CHANGED state. – β.εηοιτ.βε Commented Jan 30 at 17:54
  • 4 Have you tried with force_tag ? – Zeitounator Commented Jan 30 at 18:47
Add a comment  | 

1 Answer 1

Reset to default 1

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/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

本文标签: Is it possible to force push a docker image to a registry using dockerimage Ansible moduleStack Overflow