我們的專案用到了go,mysql,nginx,使用docker compose可以很方便的一次全部run起來
docker compose需要寫yaml設定檔來處理,在專案中創建docker-compose.yaml寫入
version: '3'
services:
db:
image: mysql:8.0
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: your_password
MYSQL_DATABASE: app
volumes:
- ./database/maindata:/var/lib/mysql
- ./app/config/database/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
app:
image: local/test #yanbinlin/creater:latest
ports:
- "8000:8000"
- "8001:8001"
- "8002:8002"
depends_on:
- db
volumes:
- ./app/view:/view
- ./app/config/app:/config/app
- ./app/file:/file
- ./log:/log
proxy:
image: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./app/config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
- ./app/log/nginx:/var/log/nginx
- /home/danny/danny/ssl/mkcert:/etc/nginx/certs
depends_on:
- app
解釋:
depends_on
等待指定的image run起來才會開始run,不然docker compose會同時全部一起運行輸入
docker-compose up
就能跑起來了
docker compose預設就會建一個network來處理container間的傳輸,我們已經使用nginx了,所以我們不需要對外註冊那麼多port,現在改寫docker-compose.yaml
version: '3'
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: 59358C6A0FAF01FDB7F37998E7CD85845C5C534E
MYSQL_DATABASE: test
volumes:
- ./database/maindata:/var/lib/mysql
- ./app/config/database/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
app:
image: local/test #yanbinlin/creater:latest
volumes:
- ./app/view:/view
- ./app/config/app:/config/app
- ./app/file:/file
- ./log:/log
depends_on:
- db
proxy:
image: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./app/config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
- ./app/log/nginx:/var/log/nginx
- /home/danny/danny/ssl/mkcert:/etc/nginx/certs
depends_on:
- app
docker compose建立的network可以用設定名去連線到其他container,如http://app:8000就換連到app的8000 port,現在改寫config/nginx/nginx.conf的location
resolver 127.0.0.11;
proxy_pass http://app:8000;
解釋:
我們連到database的host也要修改一下,改寫config/app/app.yaml的databases.main.host為DB
我們的應用已經順利跑起來了,隨時都能上線了
目前的設定檔
log_format logdata $Host _ $request_uri | $http_origin | $cors | $pos | $allow_origin _ $allow_methods _ $allow_headers ;
map $http_origin $cors {
'~*^https?://(account\.|asset\.|)dcreater\.com$' 'true';
}
map $cors $allow_origin {
'true' $http_origin;
}
map $cors $allow_methods {
'true' 'GET, OPTIONS, POST';
}
map $cors $allow_headers {
'true' 'User-Agent,Keep-Alive,Content-Type,Pragma,Cache-Control,Upgrade-Insecure-Requests';
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/certs/dcreater.com.pem;
ssl_certificate_key /etc/nginx/certs/dcreater.com-key.pem;
server_name dcreater.com;
location / {
resolver 127.0.0.11;
proxy_pass http://app:8000;
}
access_log /var/log/nginx/access.log logdata;
error_log /var/log/nginx/error.log;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/certs/_wildcard.dcreater.com.pem;
ssl_certificate_key /etc/nginx/certs/_wildcard.dcreater.com-key.pem;
server_name account.dcreater.com;
location / {
if ($request_method = 'OPTIONS') {
return 204;
}
resolver 127.0.0.11;
proxy_pass http://app:8001;
set $pos "43";
add_header Access-Control-Allow-Origin $allow_origin;
add_header Access-Control-Allow-Credentials $cors;
add_header Access-Control-Allow-Methods $allow_methods;
add_header Access-Control-Allow-Headers $allow_headers;
}
access_log /var/log/nginx/access.log logdata;
error_log /var/log/nginx/error.log;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/certs/_wildcard.dcreater.com.pem;
ssl_certificate_key /etc/nginx/certs/_wildcard.dcreater.com-key.pem;
server_name asset.dcreater.com;
access_log /var/log/nginx/access.log logdata;
error_log /var/log/nginx/error.log;
location / {
if ($request_method = 'OPTIONS') {
return 204;
}
resolver 127.0.0.11;
proxy_pass http://app:8002;
add_header Access-Control-Allow-Origin $allow_origin;
add_header Access-Control-Allow-Methods $allow_methods;
add_header Access-Control-Allow-Headers $allow_headers;
}
}
目前的工作環境
.
├── app
│ ├── apperr
│ │ ├── error.go
│ │ └── handle.go
│ ├── common
│ │ └── cookie.go
│ ├── database
│ │ ├── auth.go
│ │ ├── connect.go
│ │ ├── error.go
│ │ ├── main.go
│ │ └── scheme.go
│ ├── Dockerfile
│ ├── go.mod
│ ├── go.sum
│ ├── log
│ │ ├── logger.go
│ │ └── logging.go
│ ├── main.go
│ ├── middleware
│ │ ├── auth.go
│ │ ├── error.go
│ │ └── log.go
│ ├── router
│ │ ├── account.go
│ │ ├── asset.go
│ │ ├── host_switch.go
│ │ └── main.go
│ ├── serve
│ │ ├── account.go
│ │ ├── asset.go
│ │ ├── auth.go
│ │ ├── main.go
│ │ └── main_test.go
│ ├── setting
│ │ └── setting.go
│ ├── util
│ │ ├── debug
│ │ │ ├── stack.go
│ │ │ └── stack_test.go
│ │ ├── file
│ │ │ └── file.go
│ │ ├── hash
│ │ │ ├── hash.go
│ │ │ └── hash_test.go
│ │ └── random
│ │ └── random.go
│ └── view
│ ├── css
│ ├── html
│ │ ├── component
│ │ │ ├── blogContainer.html
│ │ │ ├── blogList.html
│ │ │ ├── editor.html
│ │ │ ├── navigation.html
│ │ │ └── signContainer.html
│ │ └── meta
│ │ ├── head.html
│ │ └── index.html
│ └── js
│ ├── account.js
│ ├── editor.js
│ ├── main.js
│ ├── owner.js
│ └── sign.js
├── config
│ ├── app
│ │ ├── app.yaml
│ │ └── error.yaml
│ └── nginx
│ └── nginx.conf
├── database
│ └── maindata
└── docker-compose.yaml