Skip to content

Commit

Permalink
Update Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
rexruan committed Feb 22, 2024
1 parent 878595d commit ff25287
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
20 changes: 14 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ FROM python:3.10

USER 0:0

ENV PYTHONPATH="$PYTHONPATH:/root/server:/tools/efselab"
ENV PYTHONPATH="$PYTHONPATH:/root:/root/server:/root/tools/efselab"
ENV PATH="$PATH:/root:/root/server:/root/tools:/root/tools/efselab"
ENV SWEGRAM_WORKSPACE=/root
ENV PRODUCTION=1

# Python packages
RUN ln -s /usr/bin/python3.10 /usr/bin/python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY tools/ /tools
COPY build_dependencies /root/build_dependencies
COPY server/ /root/server
COPY run_server.py /root
COPY app_run.py /root

COPY requirements.server.txt /requirements.txt

RUN pip install -r /requirements.txt --index-url https://pypi.org/simple
# Entry python script to run app
COPY main.py /root

WORKDIR /root
RUN python -m venv venv && \
. venv/bin/activate && \
pip install -r /requirements.txt --index-url https://pypi.org/simple && \
deactivate

ENTRYPOINT [ "uvicorn", "run_server:app", "--reload" ]
RUN . venv/bin/activate && swegram-build && deactivate
RUN rm -rf build_dependencies requirements.txt

ENTRYPOINT [ "bash", "-c", "source venv/bin/activate && python main.py" ]
6 changes: 6 additions & 0 deletions run_server.py → app_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
app.include_router(states_router, prefix=f"{PROD_PREFIX}/states", tags=["states"], dependencies=[Depends(get_db)])
app.include_router(text_router, prefix=f"{PROD_PREFIX}/text", tags=["text"], dependencies=[Depends(get_db)])
app.include_router(texts_router, prefix=f"{PROD_PREFIX}/texts", tags=["texts"], dependencies=[Depends(get_db)])


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="0.0.0.0", port=8000)
27 changes: 10 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ services:
- client


backend:
image: swegram-backend:latest # Image needs to be built with network
container_name: fastapi-backend
restart: always
privileged: true
network_mode: host
volumes:
- /var/run/docker.sock:/var/run/docker.sock
profiles:
- client
# backend:
# image: swegram-backend:latest # Image needs to be built with network
# container_name: swegram-backend-fastapi
# restart: always
# privileged: true
# network_mode: host
# volumes:
# - /var/run/docker.sock:/var/run/docker.sock
# profiles:
# - client


vue-nginx-proxy:
Expand All @@ -45,10 +45,3 @@ services:
- /var/run/docker.sock:/var/run/docker.sock
profiles:
- client







4 changes: 4 additions & 0 deletions frontend/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ server {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

# Disable keepalive connections
keepalive_timeout 0;

}
15 changes: 15 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
import os
import subprocess
from time import sleep

if __name__ == "__main__":

os.environ["PRODUCTION"] = "1"
sleep(20)
while True:
try:
subprocess.run(["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "app_run:app"])
except Exception as error:
logging.error(error)
sleep(5)
1 change: 1 addition & 0 deletions requirements.server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ swegram
cryptography
fastapi
uvicorn
gunicorn
databases[mysql]
mysql-connector-python
mysqlclient

0 comments on commit ff25287

Please sign in to comment.