[Python] Django deployment procedure with Docker

seohyun Kang·2024년 8월 30일

Python

목록 보기
2/2

File Structure

Here is the file structure of my deployment test project with docker.

ㄴ my_project
	ㄴ source
    	ㄴ my_project
        	ㄴ urls.py      // Router
            ㄴ settings.py  // Project settings
            ㄴ wsgi.py      // Web server package
        ㄴ run.sh           // Execution shell script
        ㄴ manage.py        // Server execution file
        ㄴ requirements.txt // packages info
	ㄴ DockerFile           // Docker build configuration

Settings

  1. Define STATIC_URL to the settings.py
// settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(<ROOT>, 'static')
  1. Add static path information to the urls.py
// urls.py
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

...set urls 

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) || staticfiles_urlpatterns()

References : staticfiles staticfiles-deployment

DockerFile

FROM python:3.10.10
ENV PYTHONUNBUFFERED 1
RUN apt update
RUN pip install --upgrade pip
WORKDIR /src
ADD ./source/requirements.txt /src/requirements.txt
RUN pip install -r requirements.txt
ADD ./source /src/
RUN chmod +x /src/run.sh
CMD ["./run.sh"]

docker build the image through CI

Nginx

upstream testpage {  
  #ip_hash;
  #least_conn;
  server testpage:8080;
}

server {
  location / {
      proxy_pass http://landing/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $host;
      autoindex off;
    }
  
    location /static {
        proxy_pass http://landing/static;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		proxy_set_header Host $host;
    }
}  

docker-compose.yml



testpage:
    image: <DOCKER IMAEG>
    expose: <PORT>
    env_file: .env
    restart: always
    networks:
      - met_network

0개의 댓글