2

I am trying to get a startup script (systemd service) for docker-compose containers.

I am struggling to get it working, and have made changes and changes; with some things working, and other things not working as expected, specifically how environment variables work with systemd configurations.

# named `dc-mag235.service`
[Unit]
Description=Magento 2.3.5 Docker-Compose Containers
After=docker.service
Requires=docker.service

[Service]
EnvironmentFile=/var/www/docker/_systemd.startups_/mag2.35.env.file
#PassEnvironment=$DCFOLDER
Type=simple
TimeoutStartSec=0
Restart=always
WorkingDirectory=${DCFOLDER}
ExecStartPre=mkdir -p ${LOGFOLDER}
ExecStartPre=echo ${DCFOLDER}
ExecStartPre=echo Starting Docker-compose service for ${DESCRIPTION}...
ExecStart=/usr/local/bin/docker-compose start
ExecReload=/usr/local/bin/docker-compose restart
ExecStop=/usr/local/bin/docker-compose stop

[Install]
WantedBy=multi-user.target
DESCRIPTION=Magento 2.3.5 Docker-Compose Containers
LOGFOLDER=/var/www/docker/_initlogs_
LOGFILE=$LOGFOLDER/dc-mag235.startup.log
DCFOLDER=/var/www/docker/magento2-docker-full-supervisord-expanded/release-magento2.35

Trying to use WorkingDirectory with the variable $DCFOLDER: When I remove PassEnvironment I get the same error mentioned here:

sudo service dc-mag235 status
/etc/systemd/system/dc-glo-mag235.service:8: Invalid environment name for PassEnvironment, ignoring: $DCFOLDER
Apr 22 12:32:20 glo020-truservcomm-jhb1-001 systemd[1]: /etc/systemd/system/dc-glo-mag235.service:12: WorkingDirectory= path is not absolute: ${DCFOLDER}

Trying to use cd $DCFOLDER && docker-compose restart as ExecStart command instead of WorkingDirectory:

Executable "cd" not found in path "/usr/local/sbin:/usr/local/bin:/usr/sbin:

Please note: When I disable WorkingDirectory and then use cd, when inspecting the /var/log/syslog the ExecStartPre echos actually give me the value of those variables (paths) as they are supposed to be. So ExecStartPre executes fine.

Issue

  • I have custom docker-compose.yml.* files that are running existing containers, which need to be restarted/stopped/started. Since the docker-compose service commands I want to use requires you to be inside the directory where the docker-compose.yml is, I need to be able to use cd or WorkingDirectory so that I can directly use docker-compose start.

  • I have several instances of different sets of docker-compose containers (different folders) which are running, so I wanted to create a "template systemd" file which I can just specify the EnvironmentFile location.

In other words :

Example = each of these have their own Docker containers... so I want to create different systemd services for these:

/docker/folder/docker-compose.yml
/docker/folder2/docker-compose.yml

Questions

I have a few questions here:

  1. Can WorkingDirectory be used with an 'environment variable' and how? As can be seen I tried PassEnvironment but with an environment file. Seems that I do not understand the execution of when systemd sets the Environment WorkingDirectory. It looks like all of these "Service" commands expects static constant values...

  2. 'cd' not found in path - this one is weird because I thought I could simply change directory to the working directory (where the docker-compose.yml file is located so that I can run docker-compose start/restart/stop.

  3. What is the actual use of PassEnvironment?

  4. Finally - trying $(which docker-compose) ps as an example fails because, again like WorkingDirectory it seems to be expecting a 'constant value'. I would have liked to use $(which docker-compose) instead of static locations.

2
  • Dang, I had a very similar question to yours, and was hoping that someone would answer you regarding the WorkingDirectory issue. All I can say is that cd does not work because in most systems that's not an external command, but a shell-specific instruction; to get those working, you'd have to call a shell; of course, if you do that, once the shell finishes, it won't be on that directory any longer... Nov 29, 2022 at 23:15
  • @GwynethLlewelyn This was over a year ago, and cannot remember if or how I got this resolved. I do use sytemd with docker-compose, so I got some things working eventually. At the moment I am at work, so once I have time I will investigate the container content (docker-compose.yml files and systemd entries) to figure out if I used WorkingDirectory or not. Please remind me again this week with an updated comment (I guess bookmark this question). Nov 30, 2022 at 8:21

1 Answer 1

0

Sometimes it might just be as easy as

ExecStart=/bin/bash -c "cd \"/path/${variable_subpath}/\";echo \"${PWD}\";"

and skip WorkingDirectory altogether. In most cases you can only get away with this hacky approach if it's a simple command or script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .