Skip to content

Commit 70a429d

Browse files
Merge pull request #102 from socraticDevBlog/20250428-dockercompose
Refactor Docker setup: streamline Dockerfile, update README for Docke…
2 parents f862fe9 + ea81a6b commit 70a429d

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

backend/Dockerfile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ FROM python:3.13.3-slim
33
WORKDIR /app
44

55
COPY Pipfile Pipfile.lock ./
6-
COPY app ./app
7-
8-
RUN pip install --no-cache-dir pipenv
96

10-
ENV PIPENV_VENV_IN_PROJECT=1
7+
RUN pip install --no-cache-dir pipenv && pipenv install --dev
118

12-
RUN pipenv install --dev
9+
COPY app ./app
1310

1411
EXPOSE 8000
1512

16-
CMD ["pipenv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
13+
CMD ["pipenv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

backend/README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,38 @@ export PIPENV_PIPFILE=$(pwd)/Pipfile
2828
pipenv run fastapi dev
2929
```
3030

31-
## Docker
31+
## Docker compose
3232

33-
build the image locally
33+
build the app image locally
3434

3535
```
3636
docker build -t pastebin-backend .
3737
```
3838

39-
run the container
39+
have a `.env` file at the root of the `/backend` directory and fill it out with
40+
these values
4041

42+
```ini
43+
DB_USER=myuser
44+
DB_PASSWORD=mypassword
45+
DB_NAME=mydeb
46+
DB_SCHEMA=myschema
47+
DB_HOST=db # must match docker-compose file service name
4148
```
42-
docker run -p 8000:8000 pastebin-backend
49+
50+
run the app alongside a Postgresql database on your local machine
51+
52+
```bash
53+
docker compose up --build
54+
```
55+
56+
to reset your docker environment
57+
58+
```bash
59+
docker compose down --volumes
4360
```
4461

45-
FastAPI app is available on: [http://localhost:8000](http://localhost:8000)
62+
FastAPI app swagger is available on: [http://localhost:8010/docs](http://localhost:8010/docs)
4663

4764
## API - locally
4865

backend/docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
services:
2+
app:
3+
build:
4+
context: ./
5+
dockerfile: ./Dockerfile
6+
container_name: fastapi_app
7+
ports:
8+
- "8010:8000"
9+
env_file:
10+
- ./.env
11+
depends_on:
12+
db:
13+
condition: service_healthy
14+
15+
db:
16+
image: postgres:15
17+
container_name: postgres_db
18+
restart: always
19+
env_file:
20+
- ./.env
21+
environment:
22+
POSTGRES_USER: ${DB_USER}
23+
POSTGRES_PASSWORD: ${DB_PASSWORD}
24+
POSTGRES_DB: ${DB_NAME}
25+
ports:
26+
- "5432:5432"
27+
volumes:
28+
- postgres_data:/var/lib/postgresql/data
29+
healthcheck:
30+
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
31+
interval: 10s
32+
timeout: 5s
33+
retries: 5
34+
35+
volumes:
36+
postgres_data:

0 commit comments

Comments
 (0)