奥丁9
奥丁9
后端
数据库
redis
mysql
mongoDB
达梦
php
laravel
laravel-admin
dcat
表单
表格
java
spring
python
go
c
c++
前端
vue
nodejs
sass/less
html/css
前端框架
javascript
微信生态
公众号
小程序
uniapp
typescript
其他
AI
数据结构
安全
linux
seo
git
健身
算法
正则表达式
docker
待分类
其他
/
docker
使用docker-compose搭建laravel环境
3个月前
aoding9
13
laravel
docker
参考:https://learnku.com/articles/24862 创建docker-laravel目录,去https://github.com/laravel/laravel 下载laravel代码,我这里下的laravel 10 创建docker编排文件 docker-compose.yml ```yml # version: '2' services: # The Application app: build: context: ./ dockerfile: app.dockerfile restart: always container_name: tw-app working_dir: /var/www volumes: - ./:/var/www # The Web Server web: image: nginx:1.10 restart: always container_name: tw-web working_dir: /var/www volumes: - ./:/var/www - ./etc/nginx:/etc/nginx/conf.d ports: - 8080:80 # The mysql mysql: image: mysql:5.7 # docker启动时,自动启动该容器 container_name: tw-mysql restart: always volumes: - dbdata:/var/lib/mysql - ./etc/mysql:/etc/mysql/conf.d - /etc/localtime:/etc/localtime:ro # 让容器的时钟与宿主机时钟同步,避免时间的问题,ro是read only的意思,就是只读。 command: [ "--defaults-file=/etc/mysql/conf.d/my.cnf", # "--character-set-server=utf8mb4", # "--collation-server=utf8mb4_unicode_ci", # "--lower_case_table_names=1", # "--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION", ] environment: - "MYSQL_DATABASE=homestead" - "MYSQL_USER=homestead" - "MYSQL_PASSWORD=secret" - "MYSQL_ROOT_PASSWORD=secret" - "TZ=Asia/Shanghai" ports: - "33061:3306" redis: image: redis:6.2.6 # 自定义容器名 container_name: tw-redis # docker启动时,自动启动该容器 restart: always # 挂载映射,可以让数据或配置持久化 volumes: - ./etc/redis:/etc/redis - redisdata:/data command: redis-server /etc/redis/redis.conf ports: # <本地端口> : <docker容器端口> - 63781:6379 volumes: dbdata: redisdata: ``` app.dockerfile ```dockerfile FROM php:8.1.0-fpm # Update packages RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list RUN apt-get update # Install PHP and composer dependencies RUN apt-get install -qq vim curl libmcrypt-dev libzip-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev # Clear out the local repository of retrieved package files # RUN apt-get clean # Install needed extensions # Here you can install any other extension that you need during the test and deployment process # RUN apt-get clean; RUN docker-php-ext-install pdo pdo_mysql gd pcntl opcache zip bcmath # Installs Composer to easily manage your PHP dependencies. RUN curl --silent --show-error https://install.phpcomposer.com/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer config -g repos.packagist composer https://mirrors.tencent.com/composer/ CMD php-fpm ``` ./etc/nginx/default.conf ```conf server { listen 80; index index.php index.html; root /var/www/public; location / { try_files $uri /index.php?$args; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass app:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } ``` ./etc/redis/redis.conf ```conf #开启远程可连接 #bind 127.0.0.1 #自定义密码 requirepass 12345678 #指定 Redis 监听端口(默认:6379) port 6379 #客户端闲置指定时长后关闭连接(单位:秒。0:关闭该功能) timeout 0 # 900s内如果至少一次写操作则执行bgsave进行RDB持久化操作 save 900 1 # 在300s内,如果至少有10个key进行了修改,则进行持久化操作 save 300 10 #在60s内,如果至少有10000个key进行了修改,则进行持久化操作 save 60 10000 #是否压缩数据存储(默认:yes。Redis采用LZ 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大) rdbcompression yes #指定本地数据文件名(默认:dump.rdb) dbfilename dump.rdb #指定本地数据文件存放目录 dir /data #指定日志文件位置(如果是相对路径,redis会将日志存放到指定的dir目录下) # logfile "redis.log" ``` ./etc/my.cnf ```cnf ###### [client]配置模块 ###### [client] default-character-set=utf8mb4 socket=/var/lib/mysql/mysql.sock ###### [mysql]配置模块 ###### [mysql] # 设置MySQL客户端默认字符集 default-character-set=utf8mb4 socket=/var/lib/mysql/mysql.sock ###### [mysqld]配置模块 ###### [mysqld] port=3306 user=mysql # 设置sql模式 sql_mode模式引起的分组查询出现*this is incompatible with sql_mode=only_full_group_by,这里最好剔除ONLY_FULL_GROUP_BY sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock server-id = 1 # MySQL8 的密码认证插件 如果不设置低版本navicat无法连接 default_authentication_plugin=mysql_native_password # 禁用符号链接以防止各种安全风险 symbolic-links=0 # 允许最大连接数 max_connections=1000 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8mb4 #数据库字符集对应一些排序等规则,注意要和character-set-server对应 collation-server=utf8mb4_general_ci # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB # 表名存储在磁盘是小写的,但是比较的时候是不区分大小写 lower_case_table_names=0 max_allowed_packet=16M # 设置时区 default-time_zone='+8:00' ``` laravel项目启动后修改env和路由,应用没有同步:可能有缓存 init.sh ```sh docker-compose exec app composer install # docker-compose exec app cp .env.example .env docker-compose exec app php artisan key:generate docker-compose exec app php artisan optimize docker-compose exec app php artisan config:clear docker-compose exec app php artisan route:clear docker-compose exec app php artisan view:clear docker-compose exec app php artisan migrate ``` mysql容器启动后,laravel连接不到: 将env里面的`DB_HOST=localhost`改为`DB_HOST=database`,docker-compose会自动解析为容器ip Docker mysql 配置文件读取失败 [Warning] World-writable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored. 这个是因为配置文件权限太大,被忽略了,如果代码在linux系统,执行chmod 644 path/my.cnf 如果项目是从windows文件目录映射到虚拟机的,虚拟机内chmod无法修改权限,要在windows里右键该文件-》属性-》只读。 安装predis docker-compose exec app composer install predis/predis .env ```.env APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true APP_URL=http://localhost LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret BROADCAST_DRIVER=log CACHE_DRIVER=file FILESYSTEM_DISK=local QUEUE_CONNECTION=sync SESSION_DRIVER=file SESSION_LIFETIME=120 MEMCACHED_HOST=127.0.0.1 REDIS_HOST=redis REDIS_PASSWORD=12345678 REDIS_PORT=6379 REDIS_CLIENT=predis MAIL_MAILER=smtp MAIL_HOST=mailpit MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS="hello@example.com" MAIL_FROM_NAME="${APP_NAME}" AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=false PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_HOST= PUSHER_PORT=443 PUSHER_SCHEME=https PUSHER_APP_CLUSTER=mt1 VITE_APP_NAME="${APP_NAME}" VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}" VITE_PUSHER_HOST="${PUSHER_HOST}" VITE_PUSHER_PORT="${PUSHER_PORT}" VITE_PUSHER_SCHEME="${PUSHER_SCHEME}" VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" ```
本作品采用
《CC 协议》
,转载必须注明作者和本文链接