Skip to content
Advertisement

Start docker containers on linux system startup from user directory

I’ve downloaded two docker containers and already configure them.

So, now all I want is to start them on system startup.

They are in a path like

/home/user/docker-mailserver

/home/user/docker-webserver

Hosted on a Ubuntu 18.04.01 (x64)

On boot those docker containers are not running.

On login, those docker containers are starting.

I already tried to do something like

docker run -it --restart unless-stopped fancydockercontainer:latest

docker run -dit --restart unless-stopped fancydockercontainer:latest

But then when I do docker ps there where new containers added to the pool.

Is there a way to “re-route” the start process of those container to system start without completely delete / remove them?

Addition: I started them like docker-compose up -d mailserver

Advertisement

Answer

After @KamilCuk gave a hint to solve this with service, this was a possible solution.

Looks like this:

  1. Create service file with command: nano /etc/systemd/system/docker-mail.service

  2. Done stuff like that in the file

[Unit]
Description=Docker Mailserver
Requires=docker.service
After=docker.service

[Service]
Restart=always
RemainAfterExit=yes
WorkingDirectory=/home/user/docker-mailserver
ExecStart=/usr/bin/docker-compose up -d mail
ExecStop=/usr/bin/docker-compose stop -d mail

[Install]
WantedBy=default.target
  1. Adding the new service to systemctl with systemctl enable docker-mail.service

After rebooting the server, this mailserver is available.

At this point, I was able to see the startup log with journalctl -u docker-mail.service -b (-b is just “boot”)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement