3

I've got a docker-compose file that I use to start up several containers for my service. One of these containers requires an environmental variable to be set (the public ip of the server, to be exact). The OS is a freshly installed Ubuntu 15.10 Server vm.

I'm trying to work out a way to provide this with systemd. The best I've come up with so far is this:

[Unit]
Description=service for my product
Requires=docker.service
After=docker.service

[Service]
Environment=MY_MASTER_IP=`/sbin/ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'`
Environment=MY_NETWORK_NAME="mynetwork"

Restart=always
ExecStart=/usr/local/bin/docker-compose -f /app/master/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /app/master/docker-compose.yml stop

[Install]
WantedBy=local.target

However, when I use systemd-analyze verify myapp.service systemd responds by telling me the line with the crazy shell command is invalid and will be ignored.

[/lib/systemd/system/myapp.service:8] Invalid environment assignment, ignoring: MY_MASTER_IP=`/sbin/ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')`

I also tried putting the variable assignment directly berfore the docker-compose command (MY_MASTER_IP=blah blah blah /docker-compose yadda yadda) like I was able to do with upstart, but systemd liked this even less.

So can the environment line be used like this and if not, what would be the best/most elegant/easiest way to handle this?

5

No, As systemd units are not shell scripts nor there is command substitution support. this is an intentional design decision.

  • 2
    Yeah, I ended up creating a script that gets called in an ExecStartPre statement that writes all the files to an EnvironmentFile. This seemed to be the most effective and easiest way to handle it. – Kevin Milner Dec 18 '15 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.