Create TOC

2020년 9월 11일

python개발을 위한 code-server 설정

code-server를 이용해 웹 브라우저로 접속하는 vscode 환경을 만든다.

https 연결은 caddy을 사용한다.

docker-compose.yml

cdr-python 이미지 까지 빌드할 수 있게 docker-compose.yml파일을 구성한다.

version: "3"

services:
    app:
        build:
            context: ./cdr_build
            dockerfile: Dockerfile
        image: cdr-python
        user: "1001:1001"
        expose:
            - 8080
        environment:
            - PASSWORD=비밀번호
        volumes:
            - '작업폴더:/home/coder/project'
        restart: unless-stopped
    caddy:
        image: caddy
        ports:
            - '포트:포트/tcp'
        environment:
            - ACME_AGREE=false
            - DOMAIN=도메인
            - CADDY_PORT=포트
        volumes:
            - ./Caddyfile:/etc/caddy/Caddyfile:ro
            - ./caddy_data:/data
            - /etc/letsencrypt:/etc/letsencrypt:ro
        restart: unless-stopped

Caddyfile

https 접속을 위해 Caddyfile을 만든다.

(forwardheaders) {
    header_up X-Forwarded-Ssl on
    header_up Host {host}
    header_up X-Real-IP {remote}
    header_up X-Forwarded-For {remote}
    header_up X-Forwarded-Port {server_port}
    header_up X-Forwarded-Proto {scheme}
    header_up X-Url-Scheme {scheme}
    header_up X-Forwarded-Host {host}
}

{$DOMAIN}:{$CADDY_PORT} {
    tls /etc/letsencrypt/live/{$DOMAIN}/fullchain.pem /etc/letsencrypt/live/{$DOMAIN}/privkey.pem

    header {
        Strict-Transport-Security "max-age=15552000; includeSubDomains; preload;"
        X-XSS-Protection "1; mode=block"
        X-Content-Type-Options "nosniff"
        X-Robots-Tag "none"
        X-Frame-Options "SAMEORIGIN"
        X-Download-Options "noopen"
        X-Permitted-Cross-Domain-Policies "none"
    }

    root * /var/www
    file_server

    rewrite /cdr /cdr/

    route /cdr/* {
        uri strip_prefix /cdr
        reverse_proxy app:8080 {
            import forwardheaders
        }
    }

    respond /robots.txt "User-agent: *\nDisallow: /" 200 {
        close
    }
}

cdr-python 이미지 빌드

Dockerfile

cdr_build 디렉토리를 만들고 아래와 같은 DockerFile을 만든다. 필요한 extension은 미리 cdr_build에 복사한다.

FROM codercom/code-server:latest

COPY *.vsix /tmp/
RUN sudo chmod 666 /tmp/*.vsix
RUN code-server --install-extension /tmp/indent-rainbow.vsix && \
 code-server --install-extension /tmp/rainbow-brackets.vsix && \
 code-server --install-extension /tmp/dracula.vsix
RUN code-server --install-extension /tmp/python.vsix
RUN code-server --install-extension /tmp/pyright.vsix
RUN sudo rm -f /tmp/*.vsix
RUN sudo apt-get update && \
 sudo apt-get -y install python3-pip \
 python3-bs4 \
 python3-lxml \
 python3-requests \
 python3-flake8 \
 python3-mypy \
 python3-sqlalchemy

시작

$ docker-compose up -d

테스트

웹브라우저를 사용해서 https://도메인:포트/cdr/ 에 접속해본다.