Django Websocket Container 구성 설정
docker container로 websocket통신이 backend를 구현하였는데 로컬 환경과 다르게 설정할게 여러가지가 있었다.
결론부터 말하면 redis-server 통신이 안되었고 docker bridge 네트워크 설정을 통해 container 외부로 통신을 연결해주니 쉽게 해결되었다. django에러 메시지가 친절하지 않으니 찾는데 어려움이 많다.ㅜㅜ
내부 내용 정리용으로 작성하였습니다. 설명이 많이 부족하지만 관련 핵심만 보시기 바랍니다.
1. redis-server 설치
- 접근 권한 허용 필요
$ redis-cli
> CONFIG SET protected-mode no
2. docker container bridge network 설정
$ docker network create --driver bridge [브릿지 이름]
3. container 내부 apache 설정
/etc/apache2/site-available/000-default.conf
WSGIPythonPath /var/www/html
<VirtualHost *:80>
...
# 생략
...
ProxyPass /ws/ ws://127.0.0.1:8000/ws/
ProxyPassReverse /ws/ ws://127.0.0.1:8000/ws/
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost>
4. django setting.py 설정
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'debug_toolbar',
]
##'''
## 생략
##'''
# Channels
ASGI_APPLICATION = 'wmata_was.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
# redis-server ip
'hosts': [('172.20.30.180',6379)],
},
},
}
use_websockets = True
5. django routing 설정
my_project.routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import wmata_afc.routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
wmata_afc.routing.websocket_urlpatterns
)
),
})
6. Dockerfile
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y apt-utils vim curl apache2 apache2-utils
RUN apt-get -y install python3 libapache2-mod-wsgi-py3
RUN ln /usr/bin/python3 /usr/bin/python
RUN apt-get -y install python3-pip
RUN ln /usr/bin/pip3 /usr/bin/pip
RUN pip install --upgrade pip
RUN pip install django ptvsd
RUN pip install django-cors-headers
RUN pip install django-debug-toolbar
RUN pip install pyOpenSSL
RUN pip install -U Twisted[tls,http2]
#Web-socket module
RUN pip install channels
#asgi_redis==1.0.0 asgiref 와 충돌이 안나는 버젼으로 설치 필요
RUN pip install asgi_redis==1.0.0
RUN pip install channels_redis
RUN a2enmod proxy_wstunnel
ADD ./000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ./settings_devkr.py /var/www/html/wmata_was/settings.py
EXPOSE 80 3500
WORKDIR /var/www/html/
CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "wmata_was.asgi:application"]
7. docker run
docker run -ti --name mysite -p 8000:8000 --net [브릿지 이름] mycontainer:0.1
'docker 마이크로서비스' 카테고리의 다른 글
Docker alpine + django 에 오라클 클라이언트 설치 (0) | 2022.08.23 |
---|---|
docker engine API 사용하기 (0) | 2022.01.13 |
docker EE DTR 설치 (0) | 2020.08.07 |
Docker for windows ca.pem 에러 해결 (0) | 2019.12.18 |
docker-machine SCP 명령어 (0) | 2019.09.26 |