PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

[FIXED] How to run docker-compose automatically on startup lubuntu?

 May 14, 2022     docker, docker-compose, ubuntu     No comments   

Issue

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

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/



Answered By - Rakesh Gadhwal
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing