自己制作镜像,并通过docker-compose部署

制作镜像目录结构

  1. [root@harbor larvel-crm-dockfile]# tree .
  2. .
  3. ├── conf
  4. ├── nginx.conf
  5. ├── nginx-site.conf
  6. └── supervisord.conf
  7. ├── docker-compose1.yml
  8. ├── docker-compose.yml
  9. ├── Dockerfile
  10. └── start.sh

nginx.conf

  1. #user nobody;
  2. worker_processes auto;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. client_header_buffer_size 4k;
  10. sendfile on;
  11. #tcp_nopush on;
  12. #keepalive_timeout 0;
  13. keepalive_timeout 2;
  14. client_max_body_size 25m;
  15. server_tokens off;
  16. #gzip on;
  17. include /etc/nginx/conf.d/*.conf;
  18. }

nginx-site.conf

  1. server {
  2. listen 80;
  3. server_name _;
  4. root /var/www/html/laravel-crm/public;
  5. index index.php index.html index.htm;
  6. error_log /var/log/nginx/error_laravel.log error;
  7. access_log /var/log/nginx/access_laravel.log;
  8. location / {
  9. try_files $uri $uri/ /index.php?$args;
  10. }
  11. location ~ \.php$ {
  12. try_files $uri =404;
  13. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  14. fastcgi_pass unix:/var/run/php-fpm.sock;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  17. fastcgi_index index.php;
  18. fastcgi_connect_timeout 300;
  19. fastcgi_send_timeout 300;
  20. fastcgi_read_timeout 300;
  21. include fastcgi_params;
  22. }
  23. location ~ .*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$ {
  24. expires 2d;
  25. }
  26. location ~ .*\.(?:js|css)$ {
  27. expires 8h;
  28. }
  29. location ~ /\. {
  30. log_not_found off;
  31. deny all;
  32. }
  33. }

supervisord.conf

  1. [unix_http_server]
  2. file=/dev/shm/supervisor.sock ; (the path to the socket file)
  3. [supervisord]
  4. logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
  5. logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
  6. logfile_backups=10 ; (num of main logfile rotation backups;default 10)
  7. loglevel=info ; (log level;default info; others: debug,warn,trace)
  8. pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  9. nodaemon=false ; (start in foreground if true;default false)
  10. minfds=1024 ; (min. avail startup file descriptors;default 1024)
  11. minprocs=200 ; (min. avail process descriptors;default 200)
  12. user=root ;
  13. ; the below section must remain in the config file for RPC
  14. ; (supervisorctl/web interface) to work, additional interfaces may be
  15. ; added by defining them in separate rpcinterface: sections
  16. [rpcinterface:supervisor]
  17. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  18. [supervisorctl]
  19. serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL for a unix socket
  20. [program:php-fpm]
  21. command = /usr/local/sbin/php-fpm --force-stderr --nodaemonize --fpm-config /usr/local/etc/php-fpm.d/www.conf
  22. autostart=true
  23. autorestart=true
  24. priority=5
  25. stdout_events_enabled=true
  26. stderr_events_enabled=true
  27. stdout_logfile=/dev/stdout
  28. stdout_logfile_maxbytes=0
  29. stderr_logfile=/dev/stderr
  30. stderr_logfile_maxbytes=0
  31. stopsignal=QUIT
  32. [program:nginx]
  33. command=/usr/sbin/nginx -g "daemon off; error_log /dev/stderr info;"
  34. autostart=true
  35. autorestart=true
  36. priority=10
  37. stdout_events_enabled=true
  38. stderr_events_enabled=true
  39. stdout_logfile=/dev/stdout
  40. stdout_logfile_maxbytes=0
  41. stderr_logfile=/dev/stderr
  42. stderr_logfile_maxbytes=0
  43. stopsignal=QUIT
  44. [include]
  45. files = /etc/supervisor/conf.d/*.conf

start.sh

  1. #!/bin/bash
  2. # Set custom webroot
  3. if [ ! -z "$WEBROOT" ]; then
  4. sed -i "s#root /var/www/html;#root ${WEBROOT};#g" /etc/nginx/conf.d/default.conf
  5. else
  6. webroot=/var/www/html
  7. fi
  8. # Display errors in docker logs
  9. if [ ! -z "$PHP_ERRORS_STDERR" ]; then
  10. echo "log_errors = On" >> /usr/local/etc/php/conf.d/docker-vars.ini
  11. echo "error_log = /dev/stderr" >> /usr/local/etc/php/conf.d/docker-vars.ini
  12. fi
  13. if [ ! -z "$PUID" ]; then
  14. if [ -z "$PGID" ]; then
  15. PGID=${PUID}
  16. fi
  17. deluser nginx
  18. addgroup -g ${PGID} nginx
  19. adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx -u ${PUID} nginx
  20. else
  21. if [ -z "$SKIP_CHOWN" ]; then
  22. chown -Rf nginx.nginx /var/www/html
  23. fi
  24. fi
  25. # Start supervisord and services
  26. exec /usr/bin/supervisord -n -c /etc/supervisord.conf

Dockerfile

  1. FROM richarvey/nginx-php-fpm:latest
  2. RUN apk add --no-cache lua-resty-core nginx-mod-http-lua \
  3. && apk add nodejs
  4. RUN rm -Rf /etc/nginx/nginx.conf
  5. COPY conf/nginx.conf /etc/nginx/nginx.conf
  6. COPY conf/nginx-site.conf /etc/nginx/conf.d/default.conf
  7. COPY conf/supervisord.conf /etc/supervisord.conf
  8. COPY start.sh /start.sh
  9. RUN cd /var/www/html \
  10. && composer create-project krayin/laravel-crm \
  11. && chown -Rf nginx.nginx /var/www/html \
  12. && chmod +x /start.sh
  13. EXPOSE 80
  14. WORKDIR /var/www/html
  15. CMD ["/start.sh"]

docker-compose.yml

  1. version: '3'
  2. services:
  3. web:
  4. image: "larvel-crm:v2"
  5. container_name: "laravel_crm"
  6. restart: always
  7. ports:
  8. - "1800:80"
  9. depends_on:
  10. - mysql
  11. links:
  12. - mysql
  13. volumes:
  14. - "/etc/localtime:/etc/localtime"
  15. mysql:
  16. image: mysql:5.7
  17. container_name: mysql
  18. restart: always
  19. environment:
  20. MYSQL_DATABASE: 'laravel-crm'
  21. MYSQL_USER: 'laravel-crm'
  22. MYSQL_PASSWORD: 'laravel-crm.123'
  23. MYSQL_ROOT_PASSWORD: 'marketin.123'
  24. MYSQL_ROOT_HOST: '%'
  25. volumes:
  26. - "/home/web/mysql/data/:/var/lib/mysql"
  27. - "/etc/localtime:/etc/localtime"

启动

  1. docker-compose up -d

.env 文件配置

  1. [root@harbor larvel-crm-dockfile]# docker exec -it 9692d09d3a48 bash
  2. bash-5.0# ls
  3. index.php laravel-crm storage
  4. bash-5.0# cat laravel-crm/.env
  5. APP_NAME='Krayin CRM'
  6. APP_ENV=local
  7. APP_VERSION=1.2.2
  8. APP_KEY=base64:rZqVFiNwGsEm4ohnoeVYzFTH9uHLJ7b3glvJQNf7w30=
  9. APP_DEBUG=true
  10. APP_URL=http://10.4.7.22
  11. APP_TIMEZONE=Asia/Kolkata
  12. LOG_CHANNEL=stack
  13. LOG_LEVEL=debug
  14. DB_CONNECTION=mysql
  15. DB_HOST=mysql
  16. DB_PORT=3306
  17. DB_DATABASE=laravel-crm
  18. DB_USERNAME=root
  19. DB_PASSWORD=marketin.123
  20. BROADCAST_DRIVER=log
  21. CACHE_DRIVER=file
  22. QUEUE_CONNECTION=sync
  23. SESSION_DRIVER=file
  24. SESSION_LIFETIME=120
  25. MEMCACHED_HOST=127.0.0.1
  26. REDIS_HOST=127.0.0.1
  27. REDIS_PASSWORD=null
  28. REDIS_PORT=6379
  29. MAIL_MAILER=smtp
  30. MAIL_HOST=mailhog
  31. MAIL_PORT=1025
  32. MAIL_USERNAME=null
  33. MAIL_PASSWORD=null
  34. MAIL_ENCRYPTION=null
  35. MAIL_FROM_ADDRESS=laravel@krayincrm.com
  36. MAIL_FROM_NAME="${APP_NAME}"
  37. AWS_ACCESS_KEY_ID=
  38. AWS_SECRET_ACCESS_KEY=
  39. AWS_DEFAULT_REGION=us-east-1
  40. AWS_BUCKET=
  41. PUSHER_APP_ID=
  42. PUSHER_APP_KEY=
  43. PUSHER_APP_SECRET=
  44. PUSHER_APP_CLUSTER=mt1
  45. MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
  46. MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

db 初始化

  1. [root@harbor larvel-crm-dockfile]# docker exec -it 9692d09d3a48 bash
  2. cd /var/www/html/laravel-crm && php artisan krayin-crm:install

登录信息

  1. Email: admin@example.com
  2. Password: admin123