一 镜像准备。
受限科学上网 + windows 机器无法登私有docker 仓问题,直接将镜像准备好。
用 docker save -o 导出 docker load 导入 。
准备 hyperf ,nginx,redis,mysql 镜像 。
docker images
swr.cn-south-1.myhuaweicloud.com/docker-study/hyperf v3.15 9d447b80181d 7 weeks ago 119MB
swr.cn-south-1.myhuaweicloud.com/docker-study/nginx latest 605c77e624dd 17 months ago 141MB
swr.cn-south-1.myhuaweicloud.com/docker-study/redis latest 3900abf41552 18 months ago 32.4MB
swr.cn-south-1.myhuaweicloud.com/docker-study/mysql 8.0.22 c8552d79a138 2 years ago 405MB
二 生成项目代码,关联至git .
docker run --name hyperf \
-v /home/www/api.xuxing.tech:/data/project \
-p 9501:9501 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.0-alpine-v3.15-swoole
cd /data/project
composer create-project hyperf/hyperf-skeleton
三 把项目跑起来。
3.1 docker-compose.yaml
version: "3"
networks:
xuxing:
driver: bridge
ipam:
driver: default
config:
- subnet: "172.88.0.0/24"
services:
nginx:
image: swr.cn-south-1.myhuaweicloud.com/docker-study/nginx:latest
ports:
- 80:80
- 443:443
volumes:
- ${NGINX_ROOT}:/home/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d/:/etc/nginx/conf.d/
- ./logs:/var/log/nginx
restart: always
container_name: nginx
networks:
xuxing:
ipv4_address: 172.88.0.5
mysql:
image: swr.cn-south-1.myhuaweicloud.com/docker-study/mysql:8.0.22
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR}:/var/lib/mysql
restart: always
container_name: mysql
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=${MYSQL_DEFAULT_PASSWORD}
networks:
xuxing:
ipv4_address: 172.88.0.2
redis:
image: swr.cn-south-1.myhuaweicloud.com/docker-study/redis:latest
ports:
- "6379:6379"
restart: always
container_name: redis
networks:
xuxing:
ipv4_address: 172.88.0.3
hyperf:
build:
context: ./hyperf/
privileged: true
user: root
container_name: hyperf
restart: always
volumes:
- ${HYPERF_DIR}:/data/project/api.xuxing.tech:rw
- ./hyperf/.env:/data/project/.env
ports:
- "9501:9501"
stdin_open: true
tty: true
networks:
xuxing:
ipv4_address: 172.88.0.4
3.2 同步目录下 .env 文件 。
NGINX_ROOT=/www/wwwroot
MYSQL_DATA_DIR=/data/mysql/data
HYPERF_DIR=/www/wwwroot/api.xuxing.tech
MYSQL_DEFAULT_PASSWORD=123456
3.3 nginx 下相关配置 。
nginx.conf
user root;
worker_processes 6;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
gzip_disable "MSIE [1-6]\.";
include /etc/nginx/conf.d/*.conf;
sendfile on;
}
conf.d/hyperf.conf 将 80 proxy_pass 至 9501
server {
listen 80;
access_log /var/log/nginx/hyperf-access.log;
error_log /var/log/nginx/hyperf_error.log;
location / {
proxy_pass http://172.88.0.4:9501;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto https;
proxy_cache_bypass $http_upgrade;
}
}
3.4 hyperf 相关配置
Dockerfile 设好workdir ,entrypoint 和商品。
FROM swr.cn-south-1.myhuaweicloud.com/docker-study/hyperf:v3.15
WORKDIR /data/project/api.xuxing.tech
ENTRYPOINT ["php", "/data/project/api.xuxing.tech/bin/hyperf.php", "start"]
EXPOSE 9501
3.5 将项目跑起来
docker-compose up -d
[+] Building 0.0s (0/0)
[+] Running 5/5
✔ Network compose-test2_xuxing Created 0.1s
✔ Container mysql Started 0.5s
✔ Container redis Started 0.4s
✔ Container hyperf Started 0.4s
✔ Container nginx Started
测一下,是否跑起来了。
curl 127.0.0.1
{"method":"GET","message":"Hello Hyperf."}
3.6 mysql 建用户,建库。
学习用,就随意点,生产时请慎重处理
先登录 docker exec -it mysql mysql -uroot -p123456
create USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'docker@Demo123';
grant all privileges on *.* to root@'%';
flush privileges;
CREATE DATABASE api default character set utf8mb4 collate utf8mb4_general_ci;
4 migration 建一张表。
4.1 项目 .env 文件 。配置连接。
#redis
REDIS_HOST=172.88.0.3
REDIS_AUTH=(null)
REDIS_PORT=6379
REDIS_DB=1
#mysql
DB_DRIVER=mysql
DB_HOST=172.88.0.2
DB_PORT=3306
DB_DATABASE=api
DB_USERNAME=root
DB_PASSWORD=docker@Demo123
DB_CHARSET=utf8mb4
DB_PREFIX=edu_
docker exec -it hyperf bash
php bin/hyperf.php gen:migration create_user_table --create=users
修改迁移文件 。
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
class CreateUserTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("tname",64);
$table->string('pwd', 32);
$table->integer('created_at');
$table->integer('updated_at');
$table->integer('deleted_at')->nullable(true);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
}
运行
php bin/hyperf.php migrate
[INFO] Migration table created successfully.
Migrating: 2023_06_22_231727_create_user_table
Migrated: 2023_06_22_231727_create_user_table
查看结果。
show tables;
+----------------+
| Tables_in_api |
+----------------+
| edu_migrations |
| edu_users |
+----------------+
desc edu_users;
+------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-----------------+------+-----+---------+----------------+
| id | bigint unsigned | NO | PRI | NULL | auto_increment |
| tname | varchar(64) | NO | | NULL | |
| pwd | varchar(32) | NO | | NULL | |
| created_at | int | NO | | NULL | |
| updated_at | int | NO | | NULL | |
| deleted_at | int | YES | | NULL | |
+------------+-----------------+------+-----+---------+----------------+