'How to run docker-compose automatically on startup lubuntu?

I have lubuntu 21.04 on my old PC. All is up to date. I install docker and docker-compose:

sudo apt install docker docker-compose
sudo systemctl enable --now docker

After that in home dir create folder web with my project. The structure of folder ~/web below:

.
├── docker-compose.yml
├── dockerfiles
│   ├── lg4
│   ├── test
│   └── test2
└── www
    ├── lg4
    ├── test
    └── test2

All services have restart derictive in docker-compose.yml:

version: '3.7'

volumes:
    mysql-volume:
networks:
    app-shared: 
        driver: bridge
    web_app-shared:
        external: true
services:
    php-httpd-lg4:
        restart: always
        build:
            args:
                user: lg4
                uid: 1000
            context: ./dockerfiles/lg4/
        ports:
            - 80:80
        volumes:
            - "./www/lg4:/var/www/html"
        links:
            - database
        networks:
            - app-shared
            - web_app-shared
    php-httpd-test:
        restart: always
        build:
            args:
                user: test
                uid: 1000
            context: ./dockerfiles/test/
        ports:
            - 82:80
        volumes:
            - "./www/test:/var/www/html"
        links:
            - database
        networks:
            - app-shared
            - web_app-shared
    php-httpd-test2:
        restart: always
        build:
            args:
                user: test
                uid: 1000
            context: ./dockerfiles/test2/
        ports:
            - 81:80
        volumes:
            - "./www/test2:/var/www/html"
        links:
            - database
        networks:
            - app-shared
            - web_app-shared
    database:
        restart: always
        image: mysql:5.7
        restart: always
        volumes:
            - mysql-volume:/var/lib/mysql
        ports:
            - 3306:3306
        networks:
            - app-shared
            - web_app-shared
        environment:
            TZ: "Europe/Moskow"
            MYSQL_ALLOW_EMPTY_PASSWORD: "no"
            MYSQL_ROOT_PASSWORD: "root"
            MYSQL_USER: 'admin'
            MYSQL_PASSWORD: 'admin'
            MYSQL_DATABASE: 'lg4'
    phpmyadmin:
        restart: always
        image: phpmyadmin/phpmyadmin
        links:
            - 'database:db'
        ports:
            - 8081:80
        environment:
            UPLOAD_LIMIT: 300M
        networks:
            - app-shared
            - web_app-shared

All works fine when I run comand sudo docker-compose up -d from ~/web dir. But how can I start all this automatically on startup system without typing any commands in terminal every time?



Solution 1:[1]

Yes, docker has restart policies such as docker run --restart=always that will handle this. This is also available in the compose.yml config file as restart: always.

In order to enable a restart policy, you need to use the --restart argument when executing docker run.

In my case what I decided to do is to use the --restart flag with the unless-stopped argument, that way my containers would be restarted in case that they crash or even after a reboot. Here’s an example of the command that I had to use:

docker run -dit --restart unless-stopped httpd

If you had an already running container that you wanted to change the restart policy for, you could use the docker update command to change that:

docker update --restart unless-stopped container_id

For more information you could take a look at the official documentation here:

https://docs.docker.com/config/containers/start-containers-automatically/

Solution 2:[2]

I believe actually that the selected answer is not truly correct if you use docker-compose to start your containers. That answer will start Docker but does nothing for your docker-compose (i have several) containers.

What i did was as follows:

  1. create a very simple start script

    #!/bin/bash cd Docker-compse-project docker-compose up

  2. do the same for a stop script

  3. you can save those wherever you want but i save them alongside the docker-compose.yml

  4. now on modern ubuntu images you create a service image in .etc.systemd/system I named mine for the main services so for me MyWebApp

so MyWebApp.service looks like so

[Unit]
Description=My Web Application
After=network.target

[Service]
Type=forking
User=MyUserToRun
Group=MyGroupToRun

ExecStart=/path/to/startScript
ExecStop=/path/to/stopScript

[Install]
WantedBy=multi-user.target
  1. now you can enable your service

    sudo systemctl enable MyWebApp.service

  2. And you can start and stop the service as usual

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rakesh Gadhwal
Solution 2 Allen Fogleson