1
votes

I have a service definition that starts a docker container using systemd on CentOS 7:

[Unit]
Description=MappingService
After=portal.service
Requires=docker.service

[Service]
TimeoutStartSec=3000
Type=forking
WorkingDirectory=/home/user/Downloads/MS_0.3.4_artifact
ExecStartPre=-/bin/docker rm -f eb-mapping-service-container
ExecStartPre=/home/user/Downloads/MS_0.3.4_artifact/deploy.sh /home/user/Downloads/MS_0.3.4_artifact/eb-mapping-service.tgz
ExecStartPre=/bin/docker run -v /dev/log:/dev/log -d -ti --log-driver=journald --network=bridge -p 9090:9090 --name eb-mapping-service-container eb-mapping-service  /bin/bash -c "cd /build/MappingService; ./start_multiple_clients_mapping_service.sh"
ExecStart=/bin/docker start -a eb-mapping-service-container
ExecStop=/bin/docker stop eb-mapping-service-container

[Install]
WantedBy=multi-user.target

This service works. The Docker container it launches is up. Whenever I boot the computer it is running. My problem with this service is that it's never reaching the Active(Running) status. Instead, it is stuck in 'Activating(start)' status. The 'start_multiple_clients_mapping_service.sh' script starts a node.js server and it starts listening, so it isn't exiting directly.

I've searched everywhere and scoured Docker's documentation about this and couldn't find an answer.

Also, if I remove the '-a' from the docker start command, then the status will be Inactive(dead) even though the container will be up and running. Update: After a while, I don't have an exact number, the service fails with the timeout reason. This isn't after 3000 seconds but way earlier. Although the service failed, the docker is still on the air and can be used.
I've verified with docker container ls

Question:
How do I change my service definition to reflect the Active(Running) status for the docker?

1
The problem is that your service is not actually forking, as you specified in Type=. The container process is not a child of the docker start process, it is a child of docker daemon, which runs separately (more formally there is a longer hierarchy, but the main point is that it is not related to docker start process). Because you classified your service as forking, systemd waits for docker start to spawn the child process and exit, leaving the child running, but instead docker start keeps running (because of -a option), so systemd thinks that the service is starting up.Danila Kiver

1 Answers

3
votes

I understood the problem. There were a couple of things wrong:

  1. the docker run command should not be used with the flags -d and -ti.
  2. the Type should be set to exec instead of forking.

After doing these two changes, I got the much sought after Active(Running) status with the Docker successfully launched.