How to reload systemd service? - docker

I have the following service defined
[Unit]
Description=Docker Compose Application Service [monitor]
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/eugenekim/Documents/monitor/node-master
ExecStart=/usr/local/bin/docker-compose -f docker-compose.log.yml up -d es-log-00
ExecStop=/usr/local/bin/docker-compose -f docker-compose.log.yml down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
The docker-compose.log.yml file has been changed and even the *.service file changed but system insists running the old definition somehow..
I've tried to run daemon-reload but it has no effect.
when I try to run
sudo systemctl reload docker-log 14469ms
Failed to reload docker-log.service: Job type reload is not applicable for unit docker-log.service.
See system logs and 'systemctl status docker-log.service' for details.
It also does not have any effect..

I think you're missing ExecReload.
[Unit]
Description=Docker Compose Application Service [monitor]
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/eugenekim/Documents/monitor/node-master
ExecStart=/usr/local/bin/docker-compose -f docker-compose.log.yml up -d es-log-00
# docker compose will watch for changes
ExecReload=/usr/local/bin/docker-compose -f docker-compose.log.yml up -d es-log-00
# or you want a force restart
#ExecReload=/usr/local/bin/docker-compose -f docker-compose.log.yml restart es-log-00
ExecStop=/usr/local/bin/docker-compose -f docker-compose.log.yml down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target

Related

systemd takes lots of time to kill containerized service

I'm running a python application in a containerized environment and created a service file which will start and stop this application (under centos)
The service file that I'm using should send NOHUP signal to the container and kill it immediately
Description=py Container
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker exec %n stop
ExecStartPre=-/usr/bin/docker rm %n
ExecStart=/usr/bin/docker run --rm --name %n -t=false -i -e environment=env1 container_name
ExecStop=/usr/bin/docker kill --signal=SIGHUP %n
ExecStopPost=/usr/bin/docker rm -f %n
[Install]
WantedBy=local.target
Systemd should stop the container and remove it in a timely manner, but it takes up tp 2 minutes to kill it.
Could you advise on how to accelerate the termination of the container ?
if that signal SIGHUP is not so important for you , you may try to kill the contaier without it /usr/bin/docker kill %n or simply trying to stop it using /usr/bin/docker stop %n

Connect to remote docker host

I have following scenario.
Two Machine ( Physical Machine)
One is Windows 10 With Docker On Windows Installer and same way ubuntu 18.04 with docker-ce installed.
I can run command on individual and that is fine.
I want to connect Ubuntu Docker Host from Docker on Windows machine. So Docker CLI on Windows Point to deamon at Ubuntu Host.
You will need to enable docker remote API on Ubuntu Docker Host by adding below settings in daemon.json or your startup script
[root#localhost ~]# cat /etc/docker/daemon.json
{
"hosts": [ "unix:///var/run/docker.sock", "tcp://0.0.0.0:2376" ]
}
Once you restart docker you can connect to docker host locally by socket file and remotely by listening port (2376).
Verify the listening port of docker on Ubuntu
[root#localhost ~]# netstat -ntlp | grep 2376
tcp6 0 0 :::2376 :::* LISTEN 1169/dockerd
Now you can connect to this docker from Windows machine by setting the DOCKER_HOST env variable in Windows like this
PS C:\Users\YellowDog> set DOCKER_HOST=tcp://<Ubuntu-Docker_Host-IP>:2376
PS C:\Users\YellowDog> docker ps
It will list docker containers running on Ubuntu Docker Host
You can also do this through additional options to the service:
Find original ExecStart line docker.service:
systemctl status docker | grep load | grep -oP "\/.+service"
# --> /lib/systemd/system/docker.service
cat /lib/systemd/system/docker.service | grep ExecStart
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
Create a new file to store the daemon options:
sudo mkdir /etc/systemd/system/docker.service.d/
Add next lines with -H unix:// -H tcp://0.0.0.0:2375 options to /etc/systemd/system/docker.service.d/options.conf:
cat <<EOF > /etc/systemd/system/docker.service.d/options.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// \$DOCKER_OPTS -H unix:// -H tcp://0.0.0.0:2375
EOF
Here you need to pay attention to the escaping $DOCKER_OPTS variable if it exists.
Or using your favorite editor, for example vim.
Now, reload the systemd daemon and restart the docker service:
# Reload the systemd daemon.
sudo systemctl daemon-reload
# Restart Docker.
sudo systemctl restart docker
Configuring your dev box to connect to the remote Docker daemon:
If you want to set DOCKER_HOST by default so it always connects remotely you can export it in your ~/.bashrc file.
Here’s an example of that as a 1 liner:
echo "export DOCKER_HOST=tcp://X.X.X.X:2375" >> ~/.bashrc && source ~/.bashrc
Or use it all at once:
DOCKER_HOST=tcp://X.X.X.X:2375 docker ps

Docker service doesn't start after boot

I am trying to figure out why my docker service doesn't run automatically on reboot.
Here it is:
$ sudo cat /etc/systemd/system/docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --insecure-registry=some-registry
When I try:
$ sudo systemctl enable docker.service
nothing happens.
The status of this service under list-unit-files:
$ sudo systemctl list-unit-files | grep docker
docker.service static
If I start the service manually (sudo systemctl start docker.service) it works as expected though.
Any ideas why?
The issue is because you have not specified any target in your service. You should change the service file as below
$ sudo cat /etc/systemd/system/docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --insecure-registry=some-registry
[Install]
WantedBy=multi-user.target
After that run the below commands
systemctl daemon-reload
systemctl disable docker
systemctl enable docker
And restart the system

How do i expose the Docker Remote API on Centos 7?

On ubuntu i can go into /etc/init/docker.conf and put in DOCKER_OPTS='-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock' to get the json data to display on my browser but how can i do it for Centos?
I have tried creating a file in /etc/sysconfig/docker and placing other_args="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock" inside the file and restarting docker but it doesn't do anything.
The systemd unit installed by the Docker corp package hardcodes the command line used to start the docker daemon:
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
[...]
There is no support for reading a file from /etc/sysconfig or elsewhere to modify the command line. Fortunately, systemd gives us the tools we need to change this behavior.
The simplest solution is probably to create the file /etc/systemd/system/docker.service.d/docker-external.conf (the exact filename doesn't matter; it just needs to end with .conf) with the following contents:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
And then:
systemctl daemon-reload
systemctl restart docker
This is actually documented on the Docker website in this document, which includes instructions for a more flexible solution that will allow you to use files in /etc/sysconfig to control the daemon.
Yes, you can do the configuration thing. But how about a docker solution to a docker problem?
docker run -d \
--name sherpa \
-v /var/run/docker.sock:/tmp/docker.sock \
-p 2375:4550 \
djenriquez/sherpa --allow
Proxies access to the socket through port 2375 on localhost.
1、edit /usr/lib/systemd/system/docker.service to add two params in the service section:
# vim /usr/lib/systemd/system/docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
2、reload the configuration, and then restart docker。
# systemctl daemon-reload
# systemctl restart docker
3、to check for success, see if the return the following response。
# ps -ef|grep docker
root 26208 1 0 23:51 ? 00:00:00 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
reference from Expose the Docker Remote API on Centos 7?

Issues Launching Image with Data Volume using CoreOS and Fleet

I have been trying to use FleetCtl to launch docker images one is a Data Volume Image and one is a Nginx Image launched with the --volumes-from option. The Nginx Image will not continue to run on the CoreOs server, but if I go to the server and type the command docker start the image starts and runs. Is there an image with launching Docker images that use a data volume with Fleet?
Docker File for Volume:
FROM busybox
MAINTAINER Zombie Possum
VOLUME ["/usr/share/nginx/html", "/usr/share/nginx/conf"]
COPY dist /usr/share/nginx/html
COPY dist_nginx.conf /usr/share/nginx/conf/dist_nginx.conf
CMD ["/usr/bin/true"]
Fleet File For Volume nginxData.service:
[Unit]
Description=Data Container
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
KillMode=none
User=core
WorkingDirectory=/home/core
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill DATA_NGINX
ExecStartPre=-/usr/bin/docker rm DATA_NGINX
ExecStartPre=-/usr/bin/docker pull private_repo/data_nginx:latest
ExecStart=/usr/bin/docker run --name DATA_NGINX private_repo/data_nginx:latest
ExecStop=/usr/bin/docker stop DATA_NGINX
Fleet File for nginx.service:
[Unit]
Description=Nginx Container
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
KillMode=none
User=core
WorkingDirectory=/home/core
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill NGINX
ExecStartPre=-/usr/bin/docker rm NGINX
ExecStartPre=-/usr/bin/docker pull private_repo/nginx:latest
ExecStart=/usr/bin/docker run -rm -p 80:80 --name NGINX --volumes-from DATA_NGINX private_repo/nginx:latest
ExecStop=/usr/bin/docker stop NGINX
[X-Fleet]
MachineOf=nginxData.service
Fleet Commands:
fleetctl submit nginxData.service
fleetctl submit nginx.service
fleetctl start nginxData.service
fleetctl start nginx.service
The Dockerfile you provided runs with an error on my Docker host (without using fleet); maybe when fleet does detect that error it removes the container for you, while on a Docker host the container still exists in stopped state despite the error.
Here's the error:
$ docker build --force-rm -t so-26469566 .
$ docker run --name DATA_NGINX so-26469566
exec: "/usr/bin/true": stat /usr/bin/true: no such file or directory2014/10/20 16:59:54 Error response from daemon: Cannot start container 767562758b9f30097a5ed16b98fe818d9c9574bb82b1cfd502bc3403e97d5b0
e: exec: "/usr/bin/true": stat /usr/bin/true: no such file or directory
make: *** [run] Error 1
Try the following CMD statement in your Dockerfile and see if it does changes the behavior of fleet.
CMD ["/bin/true"]
If your nginx.service start and run runing the docker run command directly in CoreOS server, mayby your problem is not in docker image but nginx.service.
Notice you configure your service with:
ExecStartPre=-/usr/bin/docker kill NGINX
ExecStartPre=-/usr/bin/docker rm NGINX
trying to kill and remove NGINX container but you run the container with --rm which remove it automatically when it fail or exit.
Maybe your service not start because its ExecStartPre is failing
Try include
Requires=nginxData.service
After=nginxData.service
too.

Resources