上一篇介紹了專案Serverless之後,所以一個服務會包含四個 Container 服務 Nginx、Service、Redis、PostgreSQL,每個 Docker 都負責一部分的功能,組成一個完整的服務,這時候我們會使用 Docker Compose 來控制相依性的 Docker 關係.
安裝
docker-compose
docker-compose.yaml
ithome_pellok_2018:
restart: always
build: .
expose:
- "6543"
links:
- postgres:postgres
- redis:redis
volumes:
- /usr/src/app
- /usr/src/app/static
environment:
- SECRET_KEY=235vn1235v123809324c08)(*)(#n09230520385340)
- redis.sessions.host=redis
- redis.sessions.port=6379
- DB_HOST=postgres
- DB_PORT=5433
- DB_NAME=ithome_pellok_2018
- DB_USER=postgres
- DB_PASS=password
postgres:
restart: always
image: postgres:latest
ports:
- "5433:5432"
volumes:
- pgdata:/var/lib/postgresql/data/
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: ithome_pellok_2018
redis:
restart: always
image: redis:latest
ports:
- "6379"
volumes:
- redisdata:/data
nginx:
restart: always
build: ./deploy/docker/nginx/
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- ithome_pellok_2018
links:
- ithome_pellok_2018:ithome_pellok_2018
設定 Nginx Dockerfile
FROM nginx:stable
RUN rm /etc/nginx/conf.d/default.conf
ADD conf.d/ /etc/nginx/conf.d
設定 nginx conf 檔案
upstream ithome_pellok {
server ithome_pellok_2018:6543;
}
server {
listen 80;
# optional ssl configuration
#listen 443 ssl;
#ssl_certificate /etc/nginx/ssl/certificate.crt;
#ssl_certificate_key /etc/nginx/ssl/private.key;
# end of optional ssl configuration
server_name _;
location /robots.txt {
alias /usr/src/app/ithome_pellok_2018/static/robots.txt;
}
location /static {
root /usr/src/app/ithome_pellok_2018;
expires 30d;
add_header Cache-Control public;
access_log off;
}
location / {
proxy_set_header Host $http_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 $scheme;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 60s;
proxy_send_timeout 90s;
proxy_read_timeout 90s;
proxy_buffering off;
proxy_temp_file_write_size 64k;
proxy_pass http://ithome_pellok;
proxy_redirect off;
}
}
修改 main 程式,當有時用 env 帶入 db 參數時,修改 DB 連接資訊
...
# postgres db config
if os.environ.get('DB_HOST'):
db_setting = dict(host=os.environ.get('DB_HOST'),
port=int(os.environ.get('DB_PORT')),
user=os.environ.get('DB_USER'),
passwd=os.environ.get('DB_PASS'),
name=os.environ.get('DB_NAME')
)
DB_FMT_STR = 'postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{name}'
settings['sqlalchemy.url'] = DB_FMT_STR.format(**db_setting)
# redis db config
if os.environ.get('REDIS_HOST'):
settings['redis.sessions.host'] = os.environ.get('REDIS_HOST')
settings['redis.sessions.port'] = os.environ.get('REDIS_PORT')
...
下載映像檔
docker-compose pull
建置映像檔
docker-compose build
啟動 Containers
docker-compose up -d
找出 ithome_pellok_2018_ithome_pellok_2018 container
docker ps -a
進入 Container
docker exec -ti ithome_pellok_2018_ithome_pellok_2018_1 /bin/bash
執行初始化 DB 指令
initialize_ithome_pellok_2018_db development.ini
目前有遇到一個問題還沒有解決,就是要手動進行初始化 Database 的動作,覺得好像可以寫腳本解決,但是又覺得有點奇怪,因為要在系統都準備好的時候去執行初始化 Database 的動作,但是系統啟動就已經執行了程式,這是目前還沒有比較好的解法,所以用手動的方式初始化 Database.
Install Docker Compose
Compose 指令說明