November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Start a docker container on CentOS at boot time as a linux service

Note: If docker daemon does not start at boot, you might want to enable the docker service

1
systemctl enabledocker.service

Here are the steps.

Create the file /etc/systemd/system/docker_demo_container.service

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Wants=docker.service
After=docker.service
[Service]
RemainAfterExit=yes
ExecStart=/usr/bin/dockerstart my_container_name
ExecStop=/usr/bin/dockerstop my_container_name
[Install]
WantedBy=multi-user.target

Now I can start the service

1
systemctl start docker_demo_container

And I can enable the service so it is executed at boot

1
systemctl enabledocker_demo_container

That’s it, my container is started at boot.

 

[Unit]
Description=MariaDB container
Requires=docker.service
After=docker.service
[Service]
User=php
Restart=always
RestartSec=10
# ExecStartPre=-/usr/bin/docker kill database
# ExecStartPre=-/usr/bin/docker rm database
ExecStart=/usr/bin/docker start -a database
ExecStop=/usr/bin/docker stop -t 2 database
[Install]

WantedBy=multi-user.target

 

 

#!/bin/bash
exec "$@"

and I have the following in my Dockerfile:

ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["date"]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>