Skip to content
프로젝트

konstruktoid/ansible-role-docker-rootless

GitHub에서 보기

소개

Ansible role to install a rootless Docker server

시스템 및 인프라

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
기록 수집 중
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

Docker rootless Ansible role

An Ansible role to configure install and configure a Docker daemon running as a non-root user.

Do not use any of this without first testing in a non-operational environment.

Rootless mode allows running the Docker daemon and containers as a non-root user to mitigate potential vulnerabilities in the daemon and the container runtime. (docker)

Note There is a SLSA artifact present under the slsa action workflow for verification.

Requirements

The role requires ansible-core 2.17 or later.

It also uses the ansible.posix and community.general collections, which are not installed together with the role. Install them using the versions listed in requirements.yml:

ansible-galaxy collection install -r requirements.yml

Install the role with a requirements.yml entry such as:

---
roles:
  - name: konstruktoid.docker_rootless
    version: v2.10.0
    src: https://github.com/konstruktoid/ansible-role-docker-rootless.git
    scm: git

Playbook example

---
- hosts: all
  any_errors_fatal: true
  tasks:
    - name: Include the konstruktoid.docker_rootless role
      ansible.builtin.import_role:
        name: konstruktoid.docker_rootless

Role Variables with defaults

---
configure_sysctl: true
create_docker_user: true
install_dependencies: true

docker_arch: "{{ ansible_facts.architecture }}"
docker_sysctl_file: /etc/sysctl.d/90-docker-rootless.conf
docker_add_alias: true
docker_allow_ping: false
docker_allow_privileged_ports: false
docker_compose: false
docker_compose_release: v5.8.0
docker_compose_url: https://github.com/docker/compose/releases/download
docker_daemon_json_template: daemon.json.j2
docker_driver_network: slirp4netns
docker_driver_port: builtin
docker_migrate_legacy_bin: true
docker_release: 29.10.1
docker_rootful_enabled: false
docker_rootful: false
docker_rootful_opts: false
docker_rootful_service_template: docker_rootful.service.j2
docker_rootless_script_template: docker_rootless.sh.j2
docker_rootless_service_template: docker_rootless.service.j2
docker_service_restart: true
docker_unattended_upgrades: false
docker_url: "https://download.docker.com/linux/static/stable/{{ docker_arch }}"
docker_user_bashrc: false
docker_user: dockeruser
docker_user_uid: false
docker_user_gid: false
shasums:
  docker_release:
    aarch64: a38c8a9ef6d17bce07499bfd3a3688542274769dc3b4f75c06ea2705fe65053e
    x86_64: 061642059fa7f2ed9b2f04a327a951cff4249585cf04b2661c2a6539197c9962
  docker_rootless_release:
    aarch64: 9b677eb3bb0ce21c4e6aae3e44678309250f1aa6fbc95f66a94de7b88b04e45b
    x86_64: 11eb5fa9a88e5971e71713aa277dbd916b7d368c6625844cacffa1c42b8df60c
  docker_compose_release:
    aarch64: 1a9c918b2fb49b9c87f0018c64b5abd0d30715155e98dc939779fbaee6c414d3
    x86_64: b8840f5008c447f41fce0a57ae3adeab42c89b1eb6e6012524bcabbfb4ffbaf6

Before using this role you first have to decide if you want to install Docker using the packages available to the distribution, also known as the "rootful" installation since it requires root permissions and installs the upstream Docker daemon or if you want to download the static binaries and do a manual install.

If you run the role on a system without having root permissions, you will need to set configure_sysctl: false, create_docker_user: false and install_dependencies: false and ensure that the system has the required dependencies installed and the docker user created beforehand.

If you set docker_rootful: false you will download the static binaries and do a manual install into the home directory of the docker_user. root permissions are still needed for the user, dependency and sysctl tasks unless those are disabled as described above.

If docker_rootful: true, then docker_rootful_enabled will decide if the daemon should be enabled as a service or not. The Docker packages and the rootful service are only managed if install_dependencies: true.

docker_service_restart will restart the rootless service when the Docker binaries, the systemd service or override files, or the user daemon.json change. This may affect any running containers.

Using docker_rootful: true and docker_rootful_enabled: true, will result in a standard Docker installation, with an additional Docker daemon, running as a non-root user.

Note that Debian 10 and earlier requires docker_rootful: false due to missing dependencies.

The docker_url, docker_release, docker_compose_url and docker_compose_release variables define where you find the relevant binaries and which version you should use when doing a manual installation, i.e. with docker_rootful: false.

The docker and docker-compose binaries, along with the rootless install scripts, are installed into ~/.local/bin of the docker_user. The legacy ~/bin directory is still kept in every PATH the role configures, after ~/.local/bin.

If docker_migrate_legacy_bin: true (the default) and docker_rootful: false, everything in ~/bin is moved into ~/.local/bin on every run, including files you have placed there yourself. Files with the same name in ~/.local/bin are overwritten, while a directory ends up nested as ~/.local/bin/<dir>/<dir>. A dangling symlink in ~/bin makes the role fail. Set it to false to leave the contents of ~/bin untouched.

You define the name of the Docker user that will be created with the docker_user variable. This user will download and install the binaries if docker_rootful: false or else the user will be the one running the rootless installation script and starting an isolated daemon. UID and GID can be set using the docker_user_uid and docker_user_gid variables.

Note that the sole purpose of the docker_user is to run the Docker daemon and related containers, and not for system administration or used as a regular user.

The shasums dictionary holds the SHA-256 checksums, per architecture, used to verify the files when downloaded using the get_url module. shasums.docker_release is used for the Docker .tgz file, shasums.docker_rootless_release for the docker-rootless-extras package and shasums.docker_compose_release for the docker-compose binary.

Since Docker 29.10.0, a custom docker_daemon_json_template that sets insecure-registries no longer implicitly trusts loopback registries. Use trusted TLS, or list the local registries, such as localhost:5000, explicitly.

docker_rootful_opts is the options to apply to the Docker daemon if running in rootful mode, if unset the settings in docker_rootful_service_template will be used.

If docker_add_alias: true and docker_rootful: false, then a docker alias will be added to either .bashrc or .bash_aliases of the Ansible user. If docker_add_alias: false and docker_rootful: true, a shell script named docker_rootless.sh is created in the Ansible user home directory. This works as a substitute to the docker command so that the Ansible user can execute the rootless Docker installation from the docker_user.

If docker_compose: true, then Docker Compose will be installed. With docker_rootful: true the docker-compose-plugin package is installed, which requires install_dependencies: true. Otherwise the docker-compose binary for the architecture defined by docker_arch is downloaded and installed as a docker CLI plugin.

If docker_user_bashrc: true, a .bashrc with completion for the docker and docker compose command will be placed inside the docker_user home.

If docker_unattended_upgrades: true, docker_rootful: true, install_dependencies: true, the host belongs to the Debian family and the unattended-upgrades package is installed, the Docker repository is added to the unattended-upgrades origins so that the Docker packages are updated automatically.

The docker_allow_privileged_ports and docker_allow_ping variables only take effect if configure_sysctl: true. The sysctl settings are written to docker_sysctl_file.

The docker_allow_privileged_ports variable configures if exposing privileged ports (< 1024) is allowed.

The docker_allow_ping variable configures if unprivileged users can open ICMP echo sockets. On some distributions, this is not allowed, and thereby containers cannot ping to the outside.

The docker_driver_network and docker_driver_port variables configure RootlessKit's network driver or port driver, respectively. This is useful for optimising network performance and necessary if source IP propagation is required. By default, the builtin port driver does not expose the actual source IP; instead, all connections appear to the container as originating from the Docker gateway (e.g. 172.19.0.1). Set docker_driver_port: slirp4netns to enable source IP propagation.

The variables named *_template are the locations of the templates in use, this to make it easier to replace them with custom ones.

The most important template is most likely docker_daemon_json_template: daemon.json.j2, which is the location of the Docker daemon.json configuration file template.

Container management

Standalone container

Running containers is not that much different from when a rootful Docker daemon is used, but you still need to become the unprivileged user and adapt any paths to the user working directores.

If docker_add_alias: true is used with docker_rootful: false, the docker command will be available as usual for the Ansible user, too. Type alias in the shell to see the keyword configuration.

- name: Register Docker user info
  become: true
  ansible.builtin.user:
    name: "{{ docker_user }}"
  check_mode: true
  register: docker_user_info

- name: Example container block
  environment:
    XDG_RUNTIME_DIR: "/run/user/{{ docker_user_info.uid }}"
    PATH: "{{ docker_user_info.home }}/.local/bin:{{ docker_user_info.home }}/bin:{{ ansible_env.PATH }}"
    DOCKER_HOST: "unix:///run/user/{{ docker_user_info.uid }}/docker.sock"
  block:
    - name: Nginx container
      become: true
      become_user: "{{ docker_user }}"
      community.docker.docker_container:
        name: nginx
        image: konstruktoid/nginx
        state: started
        cap_drop: all
        capabilities:
          - chown
          - dac_override
          - net_bind_service
          - setgid
          - setuid
        pull: true
        hostname: "{{ ansible_nodename }}"
        container_default_behavior: compatibility

Docker compose service

This requires the role to be run with docker_compose: true.

- name: Register Docker user info
  become: true
  ansible.builtin.user:
    name: "{{ docker_user }}"
  check_mode: true
  register: docker_user_info

- name: Example docker compose block
  become: true
  become_user: "{{ docker_user }}"
  environment:
    XDG_RUNTIME_DIR: /run/user/{{ docker_user_info.uid }}
    PATH: "{{ docker_user_info.home }}/.local/bin:{{ docker_user_info.home }}/bin:{{ ansible_env.PATH }}"
    DOCKER_HOST: "unix:///run/user/{{ docker_user_info.uid }}/docker.sock"
  block:
    - name: Create and start services
      community.docker.docker_compose_v2:
        project_src: /var/tmp/
        files: "{{ docker_user }}-docker-compose.yml"
      register: compose_output

Testing with molecule

If Ansible Molecule and related software is installed, running molecule test is supported.

Both scenarios use the built-in default (driverless) Molecule driver:

  • the default scenario boots QEMU/UEFI cloud images (almalinux10, resolute, resoluteroot and trixie) using Molecule's ansible-native driver (driver: name: default, no Docker/Vagrant plugin required), and
  • the docker scenario provisions containers through the community.docker collection.

Both converge by running the role, and verify.yml checks rootless and rootful Docker installation, service state, container execution, and docker compose.

uv pip install ansible-core ansible-lint molecule
ansible-galaxy install --force -r requirements.yml
molecule test
molecule test -s docker

The default scenario requires qemu-system-x86_64, qemu-img, genisoimage, and OVMF firmware (/usr/share/OVMF/OVMF_{CODE,VARS}_4M.fd) on the host running the tests; base cloud images are downloaded once and cached under ~/.cache/molecule-qemu/images.

tox -l will list all available tox test environments.

Contributing

Do you want to contribute? Great! Contributions are always youlcome, no matter how large or small. If you found something odd, feel free to submit a issue, improve the code by creating a pull request, or by sponsoring this project.

License

Apache License Version 2.0

Author Information

https://github.com/konstruktoid

관련 프로젝트 살펴보기

불러오는 중…