# TTYD 網頁終端機部屬程

# 建立 Dockerfile

FROM alpine:3.13.5
RUN apk add --no-cache tini ttyd socat nginx unzip openssl openssh ca-certificates  && \
    echo "daemon off;" >> /etc/nginx/nginx.conf && \
    mkdir /run/nginx && \
    echo "server { listen 80 default_server; root /var/www/localhost/htdocs; location / { try_files \$uri /index.html; } }" > /etc/nginx/conf.d/default.conf && \
    echo "<html><head><title>Welcome to Nginx</title></head><body><h1>Nginx works!</h1></body></html>" > /var/www/localhost/htdocs/index.html && \
    chown -R 0:82 /var/www/localhost/htdocs && \
    echo "export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '" >> /etc/profile && \
    echo "alias poweroff='kill 1'" >> /etc/profile
ENV TINI_KILL_PROCESS_GROUP=1
EXPOSE 7681 80
ENTRYPOINT ["/sbin/tini", "--"]
CMD [ "ttyd", "/usr/bin/ssh", "root@172.20.0.1"]

# 建立 docker-compose.yml 檔案

version: "3.9"
services:
    ttyd:
        build: .
        stdin_open: true
        tty: true
        image: ttyd
        container_name: ttyd
        restart: always
        networks:
            default:
                ipv4_address: "172.20.0.X"
networks:
  default:
    name: dockercompose_static
    external: true

# 編譯及啟動容器

docker-compose up -d --build

# Nginx 反代設定

location /ttyd {
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 1d; # dont kill connection after 60s of inactivity
        proxy_pass http://172.20.0.X:7681/;
    }