You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generic role for creating systemd services to manage docker containers.
5
4
6
-
7
5
## Example
8
6
9
7
Example of a Systemd unit for your app "myapp" that links to an already existing container "mysql":
@@ -15,65 +13,61 @@ Example of a Systemd unit for your app "myapp" that links to an already existing
15
13
vars:
16
14
container_name: myapp
17
15
container_image: myapp:latest
18
-
container_links: [ 'mysql' ]
16
+
container_links: ["mysql"]
19
17
container_volumes:
20
-
- '/data/uploads:/data/uploads'
18
+
- "/data/uploads:/data/uploads"
21
19
container_ports:
22
-
- '3000:3000'
20
+
- "3000:3000"
23
21
container_hosts:
24
-
- 'host.docker.internal:host-gateway'
22
+
- "host.docker.internal:host-gateway"
25
23
container_env:
26
24
MYSQL_ROOT_PASSWORD: "{{ mysql_root_pw }}"
27
25
container_labels:
28
-
- 'traefik.enable=true'
26
+
- "traefik.enable=true"
29
27
```
30
28
31
29
This will create:
32
30
33
-
* A file containing the env vars (either `/etc/sysconfig/myapp` or `/etc/default/myapp`).
34
-
* A systemd unit which starts and stops the container. The unit will be called `<name>_container.service` to avoid name clashes.
35
-
31
+
- A file containing the env vars (either `/etc/sysconfig/myapp` or `/etc/default/myapp`).
32
+
- A systemd unit which starts and stops the container. The unit will be called `<name>_container.service` to avoid name clashes.
36
33
37
34
### Role variables
38
35
39
-
* `container_name` (**required**) - name of the container
40
-
36
+
- `container_name`(**required**) - name of the container
41
37
42
38
#### Docker container specifics
43
39
44
-
* `container_image` (**required**) - Docker image the service uses
45
-
* `container_args` - arbitrary list of arguments to the `docker run` command as a string
46
-
* `container_cmd` (default: _[]_) - optional list of commands to the container run command (the part after the image name)
47
-
* `container_env` - key/value pairs of ENV vars that need to be present
48
-
* `container_volumes` (default: _[]_) - List of `-v` arguments
49
-
* `container_host_network` (default: _false_) - Whether the host network should be used
50
-
* `container_ports` (default: _[]_) - List of `-p` arguments
51
-
* `container_hosts` (default: _[]_) - List of `--add-host` arguments
52
-
* `container_links` (default: _[]_) - List of `--link` arguments
53
-
* `container_labels` (default: _[]_) - List of `-l` arguments
54
-
* `container_docker_pull` (default: _yes_) - whether the docker image should be pulled
55
-
* `container_docker_pull_force_source` (default: _yes_) - whether the docker image pull should be executed at every time (see [`docker_image.force_source`](https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#parameter-force_source))
56
-
* `container_cap_add` (default _[]_) - List of capabilities to add
57
-
* `container_cap_drop` (default _{}_) - List of capabilities to drop
* `container_hostname` (default _""_) - Container host name: `--hostname` flag
61
-
* `container_devices` (default _[]_) - List of devices to add
62
-
* `container_privileged` (default _false_) - Whether the container should be privileged
63
-
* `container_start_post` - Optional command to be run by systemd after the container has started
64
-
40
+
- `container_image`(**required**) - Docker image the service uses
41
+
- `container_args`- arbitrary list of arguments to the `docker run` command as a string
42
+
- `container_cmd` (default: _[]_) - optional list of commands to the container run command (the part after the image name)
43
+
- `container_env`- key/value pairs of ENV vars that need to be present
44
+
- `container_volumes` (default: _[]_) - List of `-v` arguments
45
+
- `container_host_network` (default: _false_) - Whether the host network should be used
46
+
- `container_ports` (default: _[]_) - List of `-p` arguments
47
+
- `container_hosts` (default: _[]_) - List of `--add-host` arguments
48
+
- `container_links` (default: _[]_) - List of `--link` arguments
49
+
- `container_labels` (default: _[]_) - List of `-l` arguments
50
+
- `container_docker_pull` (default: _yes_) - whether the docker image should be pulled
51
+
- `container_docker_pull_force_source` (default: _yes_) - whether the docker image pull should be executed at every time (see [`docker_image.force_source`](https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#parameter-force_source))
52
+
- `container_cap_add`(default _[]_) - List of capabilities to add
53
+
- `container_cap_drop`(default _{}_) - List of capabilities to drop
- `container_devices`(default _[]_) - List of devices to add
58
+
- `container_privileged`(default _false_) - Whether the container should be privileged
59
+
- `container_start_post`- Optional command to be run by systemd after the container has started
65
60
66
61
#### Systemd service specifics
67
62
68
-
* `service_enabled` (default: _yes_) - whether the service should be enabled
69
-
* `service_masked` (default: _no_) - whether the service should be masked
70
-
* `service_state` (default: _started_) - state the service should be in - set to
63
+
-`service_enabled` (default: _yes_) - whether the service should be enabled
64
+
-`service_masked` (default: _no_) - whether the service should be masked
65
+
-`service_state` (default: _started_) - state the service should be in - set to
71
66
`absent`to remove the service.
72
-
* `service_restart` (default: _yes_) - whether the service should be restarted on changes
73
-
* `service_name` (default: `<container_name>_container`) - name of the systemd service
74
-
* `service_systemd_options` (default: _[]_) - Extra options to include in systemd service file
75
-
* `service_systemd_unit_options`: (default `{"After": "docker.service", "PartOf": "docker.service", "Requires": "docker.service"}`), key/value defining the content of the `[Unit]` service section.
76
-
67
+
- `service_restart` (default: _yes_) - whether the service should be restarted on changes
68
+
- `service_name` (default: `<container_name>_container`) - name of the systemd service
69
+
- `service_systemd_options` (default: _[]_) - Extra options to include in systemd service file
70
+
- `service_systemd_unit_options`: (default `{"After": "docker.service", "PartOf": "docker.service", "Requires": "docker.service"}`), key/value defining the content of the `[Unit]` service section.
77
71
78
72
## Installation
79
73
@@ -87,16 +81,15 @@ Put this in your `requirements.yml`:
87
81
88
82
and run `ansible-galaxy install -r requirements.yml`.
89
83
90
-
91
84
## Gotchas
92
85
93
-
* When the unit or env file is changed, systemd gets reloaded but existing containers are NOT restarted.
94
-
* Make sure to quote values for `container_ports`, `container_hosts`, `container_volumes` and so on, especially if they contain colons (`:`). Otherwise YAML will interpret them as hashes/maps and ansible will throw up.
95
-
86
+
- When the unit or env file is changed, systemd gets reloaded but existing containers are NOT restarted.
87
+
- Make sure to quote values for `container_ports`, `container_hosts`, `container_volumes` and so on, especially if they contain colons (`:`). Otherwise YAML will interpret them as hashes/maps and ansible will throw up.
96
88
97
89
## About orchestrating Docker containers using systemd.
98
90
99
91
The concept behind this is to define `systemd` units for every docker container. This has some benefits:
92
+
100
93
- `systemd`is a well-known interface
101
94
- all services are controllable via the same tool (`systemctl`)
102
95
- all logs are accessible via the same tool (`journalctl`)
0 commit comments