Skip to content

Commit 1a22010

Browse files
authored
Docker updates (#102)
* Update Dockerfile * Update Requirements * Update Workflows * Update docker ci * Add user to Dockerfile * Update docker CI
1 parent 55e9a12 commit 1a22010

8 files changed

+143
-49
lines changed

.github/workflows/docker-build.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: docker/setup-buildx-action@v3
2121

2222
- name: Build Docker image
23-
run: docker build . -t onboardlite
23+
run: docker build . --target prod -t onboardlite
2424

2525
- name: Verify Docker image
2626
run: docker run --rm onboardlite echo "Docker image built successfully!"

.github/workflows/docker-publish.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI/CD Workflow
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
schedule:
9+
- cron: '21 18 * * 3'
10+
11+
jobs:
12+
build-and-push:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v2
18+
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v1
21+
22+
- name: Login to GitHub Container Registry
23+
uses: docker/login-action@v2
24+
with:
25+
registry: ghcr.io
26+
username: ${{ github.actor }}
27+
password: ${{ secrets.GHCR_PAT }}
28+
29+
- name: Extract branch name
30+
id: extract_branch
31+
run: echo "::set-output name=branch::$(echo ${GITHUB_REF#refs/heads/})"
32+
33+
- name: Build Docker image
34+
run: docker build --target prod -t onboardlite:${{ steps.extract_branch.outputs.branch }} .
35+
36+
- name: Tag Docker image
37+
run: docker tag myapp:${{ steps.extract_branch.outputs.branch }} ghcr.io/${{ github.repository }}/onboardlite:${{ steps.extract_branch.outputs.branch }}
38+
39+
- name: Push Docker image
40+
run: docker push ghcr.io/${{ github.repository }}/onboardlite:${{ steps.extract_branch.outputs.branch }}

Dockerfile

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Use the official Python base image
2-
FROM python:3.11-bookworm
2+
FROM python:3.11-bookworm AS base
3+
4+
USER onboard-user
35

46
# Set the working directory in the container
57
WORKDIR /src
68

7-
# Copy the requirements file to the container
8-
COPY requirements.txt .
9-
109
# Install build-essential
1110
RUN apt-get update && apt-get install -y build-essential
1211

@@ -21,15 +20,41 @@ RUN mv bws /usr/local/bin
2120

2221
RUN rm -r /tmp/
2322

24-
# Install the dependencies
25-
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
2623

27-
# Copy the application code to the container
24+
25+
FROM base AS dev
26+
2827
COPY . .
2928

30-
# Expose the port that the FastAPI application will run on
29+
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
30+
31+
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements-dev.txt
32+
33+
EXPOSE 8000
34+
35+
ENTRYPOINT ["python3", "app/entry.py" ]
36+
37+
CMD dev
38+
39+
40+
41+
FROM dev AS test
42+
43+
CMD ["pytest"]
44+
45+
46+
47+
FROM base as prod
48+
49+
COPY requirements.txt .
50+
51+
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
52+
53+
COPY ./app ./app
54+
3155
EXPOSE 8000
3256

3357
# Start the FastAPI application
34-
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
35-
#CMD ["sleep", "1h"]
58+
ENTRYPOINT ["/bin/python3", "app/entry.py"]
59+
60+
CMD []

app/entry.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@
77
def run_uvicorn():
88
host = os.getenv("ONBOARD_HOST", "0.0.0.0")
99
port = os.getenv("ONBOARD_PORT", "8000")
10-
command = ["uvicorn", "app.main:app", "--host", host, "--port", port]
10+
command = [
11+
"uvicorn",
12+
"app.main:app",
13+
"--host",
14+
host,
15+
"--port",
16+
port,
17+
"--workers",
18+
"2",
19+
]
20+
subprocess.run(command)
21+
22+
23+
def run_dev():
24+
host = os.getenv("ONBOARD_HOST", "0.0.0.0")
25+
port = os.getenv("ONBOARD_PORT", "8000")
26+
command = ["uvicorn", "app.main:app", "--host", host, "--port", port, "--reload"]
1127
subprocess.run(command)
1228

1329

1430
# Define the migrate command
1531
def run_migrate():
32+
os.chdir("./app")
1633
command = ["alembic", "upgrade", "head"]
1734
subprocess.run(command)
1835

@@ -21,5 +38,7 @@ def run_migrate():
2138
if __name__ == "__main__":
2239
if len(sys.argv) > 1 and sys.argv[1] == "migrate":
2340
run_migrate()
41+
elif len(sys.argv) > 1 and sys.argv[1] == "dev":
42+
run_dev()
2443
else:
2544
run_uvicorn()

docker-compose-dev.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
services:
2+
fastapi:
3+
build: .
4+
environment:
5+
- BWS_ACCESS_TOKEN={$BWS_ACCESS_TOKEN}
6+
ports:
7+
- 8000:8000
8+
volumes:
9+
- ./config/options.yml:/src/config/options.yml
10+
- ./database/:/data/
11+
develop:
12+
watch:
13+
# sync static content
14+
- path: ./app/static
15+
action: sync
16+
target: /src/app/static
17+
# sync templates
18+
- path: ./app/templates
19+
action: sync
20+
target: /src/app/templates
21+
- path: ./app/forms
22+
action: sync
23+
target: /src/app/templates
24+
# sync app
25+
- path: ./app/app/models/
26+
action: sync
27+
target: /src/app/models/
28+
- path: ./app/routes/
29+
action: sync
30+
target: /src/app/routes/
31+
- path: ./app/util/
32+
action: sync
33+
target: /src/app/util/
34+
- path: ./app/index.py
35+
action: sync
36+
target: /src/app/index.py
37+
- path: ./requirements.txt
38+
action: rebuild
39+
target: /src/app/requirements.txt
40+
redis:
41+
image: redis:7.2

docker-compose.yml

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
services:
22
fastapi:
3-
build: .
3+
image: ghcr.io/hackucf/onboardlite:latest
44
environment:
55
- BWS_ACCESS_TOKEN={$BWS_ACCESS_TOKEN}
66
ports:
77
- 8000:8000
88
volumes:
99
- ./config/options.yml:/src/config/options.yml
1010
- ./database/:/data/
11-
develop:
12-
watch:
13-
# sync static content
14-
- path: ./app/static
15-
action: sync
16-
target: /src/app/static
17-
# sync templates
18-
- path: ./app/templates
19-
action: sync
20-
target: /src/app/templates
21-
- path: ./app/forms
22-
action: sync
23-
target: /src/app/templates
24-
# sync app
25-
- path: ./app/app/models/
26-
action: sync+restart
27-
target: /src/app/models/
28-
- path: ./app/routes/
29-
action: sync+restart
30-
target: /src/app/routes/
31-
- path: ./app/util/
32-
action: sync+restart
33-
target: /src/app/util/
34-
- path: ./app/index.py
35-
action: sync+restart
36-
target: /src/app/index.py
37-
- path: ./requirements.txt
38-
action: rebuild
39-
target: /src/app/requirements.txt
4011
redis:
4112
image: redis:7.2

requirements-dev.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pre-commit==3.7.0
2+
semgrep==1.72.0
3+
virtualenv==20.26.1
4+
httpx
5+
pytest

requirements.txt

-7
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,15 @@ pyOpenSSL==23.2.0
1010
pydantic==2.7.1
1111
pydantic_settings==2.2.1
1212
python-dateutil==2.9.0.post0
13-
pre-commit==3.7.0
14-
python-dotenv==1.0.1
1513
python-jose==3.3.0
1614
python-terraform==0.10.1
1715
PyYAML==6.0.1
1816
redis==5.0.4
1917
requests==2.32.2
2018
requests-oauthlib==2.0.0
21-
semgrep==1.72.0
2219
stripe==9.6.0
2320
typing_extensions==4.11.0
2421
uvicorn==0.29.0
25-
virtualenv==20.26.1
26-
httpx
27-
pytest
2822
sqlmodel
2923
sentry_sdk
3024
alembic
31-
aiosqlite

0 commit comments

Comments
 (0)