mirror of
https://github.com/imjasonh/terraform-playground
synced 2026-07-20 13:29:20 +00:00
61 lines
1.8 KiB
Text
61 lines
1.8 KiB
Text
#cloud-config
|
|
# This cloud-init configuration defines systemd services to run Docker containers
|
|
# on Container-Optimized OS (COS).
|
|
|
|
write_files:
|
|
%{ for name, container in containers ~}
|
|
- path: /etc/systemd/system/${name}.service
|
|
permissions: 0644
|
|
owner: root
|
|
content: |
|
|
[Unit]
|
|
Description=Containerized application: ${name}
|
|
# Wait for the Docker socket to be ready
|
|
Wants=docker.socket
|
|
After=docker.service
|
|
|
|
[Service]
|
|
# Crucial for persistence: ensure the service restarts on exit.
|
|
Restart=${container.restart_policy}
|
|
RestartSec=5s
|
|
|
|
# Stop and remove any pre-existing container with the same name.
|
|
ExecStartPre=/usr/bin/docker stop ${name} || true
|
|
ExecStartPre=/usr/bin/docker rm ${name} || true
|
|
|
|
# Pull the image before starting.
|
|
ExecStartPre=/usr/bin/docker pull ${container.image}
|
|
|
|
# The main execution command: docker run.
|
|
ExecStart=/usr/bin/docker run \
|
|
--name ${name} \
|
|
%{ for e in container.env ~}
|
|
-e ${e.name}="${e.value}" \
|
|
%{ endfor ~}
|
|
${container.image} \
|
|
%{ if container.command != "" ~}
|
|
"${container.command}" \
|
|
%{ endif ~}
|
|
%{ for arg in container.args ~}
|
|
# Escape single quotes: end quoted string, add escaped quote, resume quoted string
|
|
'${replace(arg, "'", "'\\''")}' \
|
|
%{ endfor ~}
|
|
|
|
|
|
# ExecStop is called when the service is stopped manually.
|
|
ExecStop=/usr/bin/docker stop ${name}
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
|
|
%{ endfor ~}
|
|
|
|
runcmd:
|
|
# Reload the systemd configuration to recognize the new service files.
|
|
- systemctl daemon-reload
|
|
%{ for name, container in containers ~}
|
|
# Enable and start ${name} service.
|
|
- systemctl enable ${name}.service
|
|
- systemctl start ${name}.service
|
|
%{ endfor ~}
|
|
|