Skip to content

Commit

Permalink
Fix custom docker image build
Browse files Browse the repository at this point in the history
Systemd path is lauched as PID 1

Commands can be run pre-entrypoint in dockerfile (IE: dnf install -y sudo)
  • Loading branch information
syndr committed Jan 10, 2025
1 parent f6d62be commit 9c5c587
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 42 deletions.
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: syndr
name: molecule

# The version of the collection. Must be compatible with semantic versioning
version: 1.6.0
version: 2.0.0

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
4 changes: 4 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ platforms:
image: rockylinux/rockylinux:9-ubi
systemd: True
exec_systemd: True
exec_systemd_build_commands:
- dnf install -y sudo
privileged: False
hostvars:
test_hostvar: test
- name: ansible-collection-molecule-docker-fedora41
type: docker
image: geerlingguy/docker-fedora41-ansible:latest
Expand Down
12 changes: 10 additions & 2 deletions roles/docker_platform/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ docker_platform_container_defaults:
command: ""

# Container hostname
hostname: molecule-ci-{{ __docker_platform_instance.name }}
hostname: molecule-ci-{{ __docker_platform_instance.name | default('instance') }}

# Number of CPUs to allocate to the container
cpus: 2
Expand Down Expand Up @@ -101,12 +101,20 @@ docker_platform_container_defaults:
# WARNING:
# - This can cause issues with some containers
# - Not required if the container is already built with systemd running as PID 1
# - Expects the container to have systemd installed
# - Expects the container to have systemd packages present
# - Rebuilds the container with a custom entrypoint, provided by 'exec_systemd_path'
exec_systemd: false

# Path to the systemd binary in the container
# - This is only used if 'exec_systemd' is true
exec_systemd_path: /usr/lib/systemd/systemd

# List of commands to run as part of the docker build process to enable systemd
# - This is only used if 'exec_systemd' is true
# - Each command should be a string
# - Commands are run in the order they are defined, using the docker RUN directive
exec_systemd_build_commands: []

# Tmpfs mounts to add to the container
tmpfs: []

Expand Down
3 changes: 1 addition & 2 deletions roles/docker_platform/tasks/custom_image/buildfiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
mode: "0755"

- name: Exec Systemd | Build file exists
# Add additional build files to this list as needed
loop:
- bash.service.j2
- entrypoint.sh.j2
- Dockerfile.j2
loop_control:
loop_var: __docker_platform_item
Expand Down
14 changes: 6 additions & 8 deletions roles/docker_platform/templates/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

FROM {{ __docker_platform_definition.image }}
COPY bash.service /etc/systemd/system/bash.service
COPY entrypoint.sh /entrypoint.sh
RUN chown root:root /entrypoint.sh \
&& chmod 755 /entrypoint.sh \
&& chown root:root /etc/systemd/system/bash.service \
&& chmod 644 /etc/systemd/system/bash.service \
&& systemctl enable bash.service
ENTRYPOINT ["/entrypoint.sh"]

{% for __run_command in __docker_platform_definition.exec_systemd_build_commands %}
RUN {{ __run_command }}
{% endfor %}

ENTRYPOINT ["{{ __docker_platform_definition.exec_systemd_path | default(docker_platform_container_defaults.exec_systemd_path) }}"]

12 changes: 0 additions & 12 deletions roles/docker_platform/templates/bash.service.j2

This file was deleted.

17 changes: 0 additions & 17 deletions roles/docker_platform/templates/entrypoint.sh.j2

This file was deleted.

0 comments on commit 9c5c587

Please sign in to comment.