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

I have an amazon EC2 instance running RedHat 7, with docker service installed and enabled. I need a particular container to be started at boot time.
I have done this by installing a service which runs after the docker service is executed.

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

systemctl enable docker.service

Here are the steps.

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

[Unit]
Wants=docker.service
After=docker.service

[Service]
RemainAfterExit=yes
ExecStart=/usr/bin/docker start my_container_name
ExecStop=/usr/bin/docker stop my_container_name

[Install]
WantedBy=multi-user.target

Now I can start the service

systemctl start docker_demo_container

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

systemctl enable docker_demo_container

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

Also take a look at related docker documentation.

 

 

Advertisements
This entry was posted in docker, linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s