Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep alive containers? #3200

Open
jhoanborges opened this issue Apr 25, 2022 · 1 comment
Open

How to keep alive containers? #3200

jhoanborges opened this issue Apr 25, 2022 · 1 comment

Comments

@jhoanborges
Copy link

jhoanborges commented Apr 25, 2022

Avoid Downtime

I've been using Laradock for a long time and maybe restart containers with a single command is not a huge deal, but sometimes on weekends, containers just stops. This is a problem whem i'm out of the office.

A workaround that comes to my mind is to restart all containers with an script using the OS, but maybe is not the best choice to accomplish this.

I saw this guide in docker documentation, and also this command CMD tail -f /dev/null but how could we apply it to Laradock.

Is there any way to keep alive docker containers using this package?

@jhoanborges
Copy link
Author

jhoanborges commented Apr 27, 2022

Due to the lack of information avaliable, i took a lot of pieces from everywhere to solve this issue.

First and according to the docker documentation, i created the daemon.json file at directory /etc/docker/daemon.json
If is not created, just create it in that directory as following

{
	"live-restore": true
}

This is just a solution in case the docker daemon stops, containers will not be affected, but it's not the final solution to "keep alive containers".

So according to the Docker Documentation, restart always is the policy we need to add in every container we want to use. In case of laradock, edit your docker-compose.yml file the policy restart : always

Example
I commonly use this container:
docker-compose up -d nginx mysql phpmyadmin redis php-fpm php-worker workspace

So in every yml block i added the policy, example, nginx

### NGINX Server #########################################
    nginx:
      restart: always  #add this line at the top
      build:
        context: ./nginx
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
          - PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
        - ${NGINX_SITES_PATH}:/etc/nginx/sites-available
        - ${NGINX_SSL_PATH}:/etc/nginx/ssl
      ports:
        - "${NGINX_HOST_HTTP_PORT}:80"
        - "${NGINX_HOST_HTTPS_PORT}:443"
        - "${VARNISH_BACKEND_PORT}:81"
      depends_on:
        - php-fpm
      networks:
        - frontend
        - backend


and you need to do that with every container you'll use in your project.

Finally i installed supervisor in the server, which will be different than the containerized supervisor in laradock containers.
Go to /etc/supervisor/conf.d
and create a file, example docker-keep-alive.conf with the following conf

[program:docker-keep-alive]
process_name=%(program_name)s_%(process_num)02d
command=bash -c 'sleep 60 && cd /var/www/laradock && docker-compose up -d nginx mysql phpmyadmin redis php-fpm php-worker workspace'
autostart=true
autorestart=true
numprocs=1
user=root
redirect_stderr=true
stdout_logfile=/var/www/docker-keep-alive.log
stderr_logfile=/var/www/docker-keep-alive.error.log

In this case, the process will run every 60 second, but you can adapt it to your needs.

Finally, everytime this process runs, your output should be like:

laradock_docker-in-docker_1 is up-to-date
laradock_mysql_1 is up-to-date
laradock_redis_1 is up-to-date
laradock_workspace_1 is up-to-date
laradock_phpmyadmin_1 is up-to-date
laradock_php-worker_1 is up-to-date
laradock_php-fpm_1 is up-to-date
laradock_nginx_1 is up-to-date

In case a container stops, the log should say "recreated".
Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant