dockeransibledocker-multi-stage-build

How to make multistage Docker build using Ansible?


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?


Solution

  • 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.