justin_p.bootstrap_docker

ansible-role-bootstrap_docker

Ansible Role Name Github Actions

A Ansible role I build for quickly configuring docker and apply hardening (to my personal standards).

I do not recommend you use this on a existing infrastructure.

Note: If you where to use this role you probably don't want to use the default password/salt values. Overwrite these in each project with unique values and store them securely with Ansible Vault.

Balancing hardening and usability

UFW and Docker

Docker normally ignores rules created by UFW. This has been a known issue for sometime now. Since I really prefer using UFW to manage my host based firewall this role configures docker and iptables to work nicely with UFW. It does this by utilizing the docker userland-proxy, iptables-persistent and ufw routes. By default the role will add a route that allows docker0 to send traffic out on {{ ansible_default_ipv4.interface }}. This will make docker networking and UFW behave just like any other portbinding from a end user perspective. I did not come up with this workaround fully by myself. This is heavily based of the work done by Mateusz, though improved upon by using more granular routes and iptables-persistent so that the needed nat rule('s) aren't lost during a reboot.

Docker Socket

After enabeling userns-remap you are no longer able to mount the docker sock in side of a container. To solve this we create a copy of the socket with socat under the following path /var/run/docker-userns.sock. This copy gives the root account and dockerremap group read and write on the copied docker socket.
I'd recommend not using this copy directly in target containers (i.e. traefik), but using it in combination with Docker Socket Proxy. This ensures only the bare minumium of what you need is available on the socket in the target container.

Role Variables

Variable Description Default value
bsd_sshkey_folder Local folder where the ssh keypair for bsh_docker_public_key_path is stored. ~/.ssh
bsd_docker_gid The group id of the newly created docker user. 1337
bsd_docker_uid The user id of the newly created docker user. 1337
bsd_docker_password The password of the docker user. 123c2b9e024723391cf60279c5eb84e4!
bsd_docker_password_salt The password salt of the docker user. f92dbbf8d7f268ba
bsd_docker_public_key_path The path to the local public key that should be added to the admin user. {{ bsd_sshkey_folder }}/id_rsa.pub
bsd_docker_volume_path The path to a newly formated disk to mount /var/lib/docker on. Note, if a invalid path is supplied no mount action takes place. /dev/disk/by-id/scsi-0DO_Volume_volume
bsd_docker_remap_socket_template The template file for the docker remap socket. {{ role_path }}/templates/dockerremapsocket.service.j2
bsd_docker_deamon_template The template for the docker daemon. {{ role_path }}/templates/daemon.json.j2
bsd_docker_iptables_template The template for the iptables persistant file. {{ role_path }}/templates/rules.v4.j2
bsd_docker_interface_out The interface to allow outbound routes to coming from the docker0 interface. {{ ansible_default_ipv4.interface }}

Dependencies

robertdebock.bootstrap

robertdebock.epel

robertdebock.buildtools

robertdebock.python_pip

robertdebock.core_dependencies

robertdebock.docker_ce

robertdebock.docker_compose

Example Playbook

---
- hosts: all
  name: Bootstrap docker
  tasks:
      - include_role:
          name: bootstrap-docker
        vars:
          bsd_docker_username: "{{ docker_username }}"
          bsd_docker_password: "{{ docker_password }}"
          bsd_docker_password_salt: "{{ docker_password_salt }}"
          bsd_docker_public_key_path: "{{ docker_public_key_path }}"
          bsd_docker_volume_path: "/dev/disk/by-id/scsi-0DO_Volume_{{ do_docker_volume_name }}"

License

MIT

Author Information

Contributing

Feel free to open issues, contribute and submit your Pull Requests. You can also ping me on Twitter (@JustinPerdok).

About

My docker bootstrap role

Install
ansible-galaxy install justin_p.bootstrap_docker
GitHub repository
License
mit
Downloads
70
Owner
IT Geek, Ethical Hacker, DevOps Nut, PowerShell Fanatic and lover of beers :beers: