In order to test Ansible roles with the help of Ansible
Molecule, Docker images are required that contain a bash shell. By
default, Alpine Images only contain the Bourne shell sh. That's why the
standard Alpine Images have been expanded to include the Bourne-again shell
bash.
These Alpine images were built exclusively with Ansible without the use of Dockerfiles, see the example Ansible playbook below.
3.22, latest: Alpine Linux version 3.223.21, Alpine Linux version 3.213.20, Alpine Linux version 3.203.19, Alpine Linux version 3.193.18, Alpine Linux version 3.183.17, Alpine Linux version 3.173.16, Alpine Linux version 3.163.15, Alpine Linux version 3.153.14, Alpine Linux version 3.143.13, Alpine Linux version 3.13Ansible and Docker must be installed.
---
- name: Create Alpine Linux container
hosts: localhost
connection: local
gather_facts: false
vars:
alpine_version: "{{ lookup('env','ALPINE_VERSION') | default('3.22', true) }}"
base_image: alpine
container_name: "alpine_bash"
image_namespace: coglinev3
image_name: alpine
pre_tasks:
- name: Make the latest version of the base image available locally.
community.docker.docker_image:
name: '{{ base_image }}:{{ alpine_version }}'
source: pull
force_source: true
- name: Create the Docker container.
community.docker.docker_container:
image: '{{ base_image }}:{{ alpine_version }}'
name: '{{ container_name }}'
container_default_behavior: no_defaults
command: sleep 1h
- name: Add the newly created container to the inventory.
ansible.builtin.add_host:
hostname: '{{ container_name }}'
ansible_connection: docker
tasks:
- name: "Install Bash"
ansible.builtin.raw: apk add --update-cache bash && rm -rf /var/cache/apk/*
args:
creates: /bin/bash
delegate_to: '{{ container_name }}'
post_tasks:
- name: Commit the container.
ansible.builtin.command: >
docker commit
--author "Cogline.v3"
{{ container_name }} {{ image_namespace }}/{{ image_name }}:{{ alpine_version }}
register: docker_commit
changed_when: docker_commit.rc == 0
failed_when: docker_commit.rc != 0
- name: Remove the container.
community.docker.docker_container:
name: '{{ container_name }}'
state: absent
container_default_behavior: no_defaults
!!! note
Of course, this playbook is more complex than a simple Dockerfile that
installs the Bash package, but it shows that it is possible to create
container images with Ansible. Many thanks to [Jeff
Geerling](https://www.jeffgeerling.com/) for his introduction to Ansible
with Docker in his book [Ansible for
DevOps](https://www.jeffgeerling.com/ project/ansible-devops).
You can build any of these Alpine Images as follows:
git clone https://github.com/coglinev3/container-alpine-bash.git
cd container-alpine-bash
ALPINE_VERSION=3.21 ansible-playbook container-alpine.yml
I use these images as base images to build other Alpine Linux images with Python and Ansible to test my Ansible roles with these images.
Release: 1.12.0
GNU GPL Version 3
Copyright © 2020 - 2025 Cogline.v3.
Content type
Image
Digest
sha256:4ddf090f7…
Size
4.4 MB
Last updated
8 months ago
docker pull coglinev3/alpine