iT邦幫忙

2023 iThome 鐵人賽

0
DevOps

跟著菜鳥手把手用Docker建立服務系列 第 20

Day20 - 菜鳥們把所學的實作做起來 DockerFile 篇

  • 分享至 

  • xImage
  •  

前面大多數都在講基本概念,也有實作一些指令給大家看,方便讓大家了解也更知道在做什麼,這次我們結合前面所教的實作起來,剛好可以重新複習讓菜鳥們更熟悉/images/emoticon/emoticon12.gif


我們要實作的內容 Django + Nginx + Redis + Mysql 透過 Docker 啟一個網站

  1. 先創建 Network Bridge
    docker network create -d bridge "Network Name"
    https://ithelp.ithome.com.tw/upload/images/20240118/20158512iMNfCDR7tc.png

    • 這邊我是創一個 Network 名為mybirdge Drive為bridge
  2. 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 Image
    docker build -t "Mysql Image Name" .

    啟動Mysql Containr
    docker run --name "Mysql Container Name" --network "Network Name" -d "Mysql Imag"

  3. 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 Image
    docker build -t "Redis Image Name" .

    啟動Mysql Containr
    docker run --name "Redis Container Name" --network "Network Name" -d "Redis Imag"

  4. 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
    
    • 這邊必須把Django Project(IT_Project) 複製到 web資料夾裡面

    修改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'
     }
    }
    
    • HOST 部分要設定Mysql Container

    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'
     }
    }
    
    • LOCATION 要設定Redis Container

    建立Mysql Image
    docker build -t "Django Image Name" .

    啟動Mysql Containr
    docker run --name "Django Container Name" --network "Network Name" -d "Django Imag"

  5. 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
       }
    
    }
    
    • server 要寫Django Container

    建立Nginx Image
    docker build -t "Nginx Image Name" .

    啟動Mysql Containr
    docker run --name "Nginx Container Name" -p 8003:80 --network "Network Name" -d Nginx Name"


呈現結果
輸入127.0.0.1:8003/member/create
https://ithelp.ithome.com.tw/upload/images/20240118/20158512kPjqQuTqeF.png

輸入127.0.0.1:8003/member/read
https://ithelp.ithome.com.tw/upload/images/20240118/20158512s58ka05NZN.png


重點提醒

  1. 先確保Django、Nginx、Redis、Mysql是否都設定同一個Network
  2. 關於Django所需的的相關檔案,都包在IT_Project
  3. 至於有些為何要需要額外的設定檔,那每個參數又代表什麼可以參考DevOps:如何將容器化 (Docker + Nginx + uWSGI + Django + PostgreSQL) 的服務部署在一台主機的根目錄

今天這篇大致上最主要是在講Docker層面居多,其餘相關的技術就不會細講,當然每個設定檔都有它的意義,我覺得要花時間去了解,可以從我提供的資料去了解,甚至自己去找資料,畢竟每個系統環境一定是不同的設定,當做訓練解決問題


參考資料
DevOps:如何將容器化 (Docker + Nginx + uWSGI + Django + PostgreSQL) 的服務部署在一台主機的根目錄


上一篇
Day19 - 菜鳥們來研究 Docker Compose 指令
下一篇
Day21 - 菜鳥們把所學的實作做起來 Docker Compose 篇
系列文
跟著菜鳥手把手用Docker建立服務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言