前面大多數都在講基本概念,也有實作一些指令給大家看,方便讓大家了解也更知道在做什麼,這次我們結合前面所教的實作起來,剛好可以重新複習讓菜鳥們更熟悉
我們要實作的內容 Django + Nginx + Redis + Mysql 透過 Docker 啟一個網站
先創建 Network Bridgedocker network create -d bridge "Network Name"
Mysql
Mysql Dockerfile
FROM mysql:latest
# 設定環境變數
ENV MYSQL_DATABASE=ItDB
ENV MYSQL_USER=ITUser
ENV MYSQL_PASSWORD='Your Password'
ENV MYSQL_ROOT_PASSWORD='Your Password'
# COPY my.cnf /etc/my.cnf
RUN echo '[mysqld]' >> /etc/my.cnf \
&& echo "general_log = 1" >> /etc/my.cnf \
&& echo "general_log_file = /var/log/mysqld.log" >> /etc/my.cnf \
&& echo "log_output = file" >> /etc/my.cnf
# 指定路徑
WORKDIR /var/lib/mysql
# 指定容器運行時使用的預設命令
CMD ["mysqld"]
建立Mysql Imagedocker build -t "Mysql Image Name" .
啟動Mysql Containrdocker run --name "Mysql Container Name" --network "Network Name" -d "Mysql Imag"
Redis
Redis Dockerfile
FROM redis:latest
# 複製 redis.conf 檔案到容器中
COPY redis.conf /usr/local/etc/redis/redis.conf
RUN mkdir /home/redis/
RUN chmod 777 /home/redis/
# 執行 redis server 並且使用自訂的 redis.conf 檔案
CMD ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis.conf
# Redis 7.0 配置文件
# 監聽的端口號
port 6379
# 以哪個用戶身份運行 Redis 服務
user redis
# 密碼設置,設置密碼後需使用該密碼才能訪問 Redis 服務
requirepass redis12345
# 日誌文件的位置和名稱
logfile /home/redis/redis.log
建立Mysql Imagedocker build -t "Redis Image Name" .
啟動Mysql Containrdocker run --name "Redis Container Name" --network "Network Name" -d "Redis Imag"
Django
Django Dockerfile
FROM python:3.9.6
WORKDIR /web
COPY . /web/
RUN pip install -r requirements.txt
CMD python manage.py runserver 0.0.0.0:8000
修改IT_Project Settings.py
Mysql
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE' : 'django.db.backends.mysql',
'NAME' : 'ItDB',
'USER' : 'root',
'PASSWORD' : 'Mysql Password',
'HOST' :'Mysql Container Name',
'PORT': '3306'
}
}
Redis
CACHES = {
"default": {
# 預設使用redis://<redis_host>:<redis_port>/<db_number>
"BACKEND": "django_redis.cache.RedisCache",
# 指定redis://IP/第幾個DB
"LOCATION" : "redis://'Redis Container Name':6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": "Redis Password",
},
'KEY_PREFIX': 'Cache'
}
}
建立Mysql Imagedocker build -t "Django Image Name" .
啟動Mysql Containrdocker run --name "Django Container Name" --network "Network Name" -d "Django Imag"
Nginx
FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY my_nginx.conf /etc/nginx/sites-available/
RUN mkdir -p /etc/nginx/sites-enabled/\
&& ln -s /etc/nginx/sites-available/my_nginx.conf /etc/nginx/sites-enabled/
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
user root;
worker_processes 1;
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 '$host $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# Gzip Compression
gzip on;
# gzip_min_length 1000;
gzip_types text/plain application/xml;
gzip_proxied expired no-cache no-store private auth;
gzip_vary on;
# include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*;
my_nginx.conf
# the upstream component nginx needs to connect to
upstream uwsgi {
# server unix:/web/dj3/web.sock; # using a file socket
server "Django Container":8003; # using the docker network
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# index index.html;
# the domain name it will serve for
# substitute your machine's IP address or FQDN
server_name twtrubiks.com www.twtrubiks.com;
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /static/ {
alias /itProject/static/; # your Django project's static files - amend as
required
}
location / {
uwsgi_pass uwsgi;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
建立Nginx Imagedocker build -t "Nginx Image Name" .
啟動Mysql Containrdocker run --name "Nginx Container Name" -p 8003:80 --network "Network Name" -d Nginx Name"
呈現結果
輸入127.0.0.1:8003/member/create
輸入127.0.0.1:8003/member/read
重點提醒
今天這篇大致上最主要是在講Docker層面居多,其餘相關的技術就不會細講,當然每個設定檔都有它的意義,我覺得要花時間去了解,可以從我提供的資料去了解,甚至自己去找資料,畢竟每個系統環境一定是不同的設定,當做訓練解決問題
參考資料
DevOps:如何將容器化 (Docker + Nginx + uWSGI + Django + PostgreSQL) 的服務部署在一台主機的根目錄