-2

I have a following task in Ansible playbook:

tasks:

  - name: Start container
    docker_container:
      name: ubuntu
      image: ubuntu:latest
      image: python:latest
      state: started
      command: sleep infinity

But when I enter into the container and execute the command:

lsb_release -a

I get a following response:

No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

Does it mean that Ansible installs only python:latest image and ignores Ubuntu one?

If so, how do I solve it?

  • 3
    Multi stage build are done to let you run some steps on the first image then access the resulting artifacts on the second one. If you do not have steps in the first image of the multistage build, the operation becomes pointless. Create yourself a Dockerfile build it, tag it and then, use that image. – β.εηοιτ.βε May 03 '23 at 10:15
  • 1
    @β.εηοιτ.βε Thank you for your comment. It looks like Dockerfile is the only option to put multiple images into one container. – MijatTomić May 03 '23 at 14:55
  • 1
    "_It look like Dockerfile is the only option to ..._", right. How to do so can be found under [Building, saving, and loading container images with Ansible](https://www.redhat.com/sysadmin/container-images-ansible) via [`docker_image` module – Manage docker images](https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html) or [Ansible Playbook to build multiple Docker images without any Dockerfile](https://gist.github.com/jtyr/2676e6fb3ea4bc6d7e62). – U880D May 03 '23 at 23:26

1 Answers1

1

Does it mean that Ansible installs only python:latest image and ignores Ubuntu one?

Right, that's the case and the expected behavior. This is because of the precedence and the fact that the last value of key image wins here.

If so, how do I solve it?

There is no solution that would work in your case without using Dockerfile and building a container out of it. You can use use loop and iterate over a simple list like in

- name: Start container
  docker_container:
    name: "{{ item }}"
    image: "{{ item }}"
    state: started
    command: sleep infinity
  loop:
    - ubuntu
    - python

but it would create two Docker containers, not one.

As according the documentation docker_container module – manage Docker containers - Parameter image the module seems only to take one string.

Repository path and tag used to create the container. If an image is not found or pull is true, the image will be pulled from the registry. If no tag is included, latest will be used.

Similar Other Solution


Regarding the comment

It looks like Dockerfile is the only option to put multiple images into one container.

you may have a look into Building, saving, and loading container images with Ansible and the docker_image module – Manage docker images.

U880D
  • 8,601
  • 6
  • 24
  • 40