Search code examples
ubuntu-16.04systemdcloud-init

Create systemd unit in cloud-init and enable it


I've created the following systemd unit in the cloud-init file:

- path: /etc/systemd/system/multi-user.target.wants/docker-compose.service
  owner: root:root
  permissions: '0755'
  content: |
      [Unit]
      Description=Docker Compose Boot Up
      Requires=docker.service
      After=docker.service

      [Service]
      Type=simple
      ExecStart=/usr/local/bin/docker-compose -f /opt/data/docker-compose.yml up -d
      Restart=always
      RestartSec=30

      [Install]
      WantedBy=multi-user.target

When I try to run

sudo systemctl enable docker-compose.service

to create the symlink I get this:

Failed to execute operation: No such file or directory

However I'm sure that the file is under /etc/systemd/system/multi-user.target.wants


Solution

  • Check that every file involved is present and valid:

     ls -l  /etc/systemd/system/multi-user.target.wants/docker-compose.service
     ls -l /usr/local/bin/docker-compose
     ls -l /opt/data/docker-compose.yml
     systemd-analyze verify /etc/systemd/system/multi-user.target.wants/docker-compose.service
    

    Also consider the timing. Even if the files exist once they are fully booted, would /etc/systemd/system/multi-user.target.wants/ exist when cloud-init runs?