시수

sisu2 도커 서버 구성

조회: 250 댓글: 0개 2023.08.04 1:53 금요일
  1. WLS2로 윈도우에 리눅스 환경 구성
    1. WSL2/Ubuntu 22.04 LTS 설치
      1. cmd에 wsl --install -d Ubuntu-22.04
      2. 실행했는데 쉘에 검은색만 나오면 컴퓨터 재부팅하셈
      3. WSL2 IP 고정
        링크
  2. 구성된 리눅스 환경에 도커 구성
    1. 도커 설치

      sudo apt update  sudo apt install -y \     net-tools \     ca-certificates \     curl \     gnupg \     lsb-release  sudo mkdir -p /etc/apt/keyrings  curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \     sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg  echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \     https://download.docker.com/linux/ubuntu \     $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null  sudo apt update  sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin 
    2. 다음 날 와서 보니까 시작이 안 돼
      이 오류가 발생하는 이유는 Ubuntu 22.04 LTS가 iptables-nft기본적으로 사용하기 때문입니다. iptables-legacyDocker가 다시 작동하도록 전환해야 합니다 .

      sudo update-alternatives --config iptables  1 legacy  sudo service docker start
  3. 도커 이미지, 컨테이너 구성
    1. Nginx + Python3 + Gunicorn
      1. Dockerfile 작성

        #alpine 리눅스 기반 FROM alpine:latest  #타임존 다운 받고 환경변수 설정 RUN apk add tzdata ENV TZ=Asia/Seoul  #nginx, python3, pip다운 #py3-mysqlclient 없으면 pip mysqlclient 에러남 #내가 봤던 예제에선 gcc도 다운받던데 잘 몰?루 RUN apk add nginx python3 py3-pip py3-mysqlclient  #nginx.conf 맨 민줄에 'daemon off;' 추가 #추가 이유는 foreground background 어쩌구 때문인데 잘 몰?루 RUN echo "daemon off;" >> /etc/nginx/nginx.conf  #내 가상호스트가 설정된 default.conf를 경로로 복사 #왜 /etc/nginx/sites-available이 아닌지 찾아봤는데 몰?루 ADD default.conf /etc/nginx/http.d/default.conf  #현재 폴더 내용을 경로로 복사 ADD ./ /var/www/sisu2 WORKDIR /var/www/sisu2 #python 모듈 다운로드 RUN pip install -r requirements.txt #권한 추가 RUN chmod +x entrypoint.sh  #포트 연결 EXPOSE 80 EXPOSE 443  #./안 붙이니까 안됨 ㅋㅋ; ENTRYPOINT ./entrypoint.sh
      2. requirements.txt 작성

        Django==4.2.4 django-environ gunicorn mysqlclient
      3. Nginx 가상호스트 default.conf 작성

        server {   listen 80;   server_name _;    location /static/ {     alias /var/www/sisu2/static/;   }   location /media/ {     alias /var/www/sisu2/media/;   }     location / {     proxy_set_header Host $host;     proxy_pass http://unix:/tmp/gunicorn_sisu2.sock;   }  } 
      4. entrypoint.sh 작성

        #!/bin/sh  python3 manage.py migrate gunicorn config.wsgi -b unix:/tmp/gunicorn_sisu2.sock -D nginx
      5. 이미지 빌드

        sudo docker build -t sisu2:0.1 .
    2. MariaDB

      sudo docker network create django-network  sudo docker run -d --name db \     -p 3306:3306 \     --network django-network \     -v /home/sisu0814/database/:/var/lib/mysql \     -e MARIADB_ROOT_PASSWORD_HASH=*A4B6157319038724E3560894F7F932C8886EBFCF \     -e MARIADB_DATABASE=sisu2 \     mariadb:10.5
    3. 마무리
      1. 도커 컴포즈 안 쓸 때

        sudo docker run -d -p 80:80 --name sisu2 \     --network django-network \     -e DJANGO_ENV=sisu2_prod.env \     sisu2:0.1
      2. docker-compose.yml

        version: '3.8'
        
        services:
          db :
            image: mariadb:10.5
            ports:    
              - 3306:3306
            volumes:
              - /home/sisu0814/database/:/var/lib/mysql
            environment:
              MARIADB_ROOT_PASSWORD_HASH: "*A4B6157319038724E3560894F7F932C8886EBFCF"
              MARIADB_DATABASE: sisu2
            networks:
              - django-network
            healthcheck:
              test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 -u root --password=$$MARIADB_ROOT_PASSWORD_HASH']
              interval: 10s
              timeout: 2s
              retries: 100
        
          app:
            build:
              context: .
              dockerfile: ./Dockerfile
            ports:
              - 80:80
            volumes:
              - /home/sisu0814/sisu/media/:/var/www/sisu2/media
            environment:
              DJANGO_ENV: sisu2_prod.env
            depends_on:
              db:
                condition: service_healthy
            networks:
              - django-network
        
        networks:
          django-network:
    4. 크악

      sudo docker compose up -d sudo docker compose down  sudo docker compose stop sudo docker compose start sudo docker compose restart
  4. 백업 어케 할건지
    media 파일 같은 경우 /hoem/sisu0814/media/:/var/www/sisu2/media 로 해결
    db 같은 경우
    두 개 남았따 씨씨씨씨씨씨
    1. password 안 남기고 db 백업 방법
    2. sisu.co.kr ㅅㅂ 어케 백업하고있는지..........

참조

https://hyeo-noo.tistory.com/185

https://pyrasis.com/jHLsAlwaysUpToDateDocker

captcha