Skip to content

Commit 22e3e04

Browse files
authored
Merge pull request #6 from dele-work/feat/automatically-use-an-unspecified-port
feat: Automatically use an unspecified port
2 parents 75a6ec6 + 9c7d2fd commit 22e3e04

File tree

5 files changed

+27
-51
lines changed

5 files changed

+27
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,4 @@ sonarqube/logs
138138
.idea
139139
.DS_Store
140140

141+
.port

Makefile

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,37 @@ help:
1717
@echo " run-prod"
1818
@echo " Run production docker compose."
1919
@echo " stop-prod"
20-
@echo " Run production docker compose."
20+
@echo " Run production docker compose."
2121
@echo " formatter"
2222
@echo " Apply black formatting to code."
2323
@echo " mypy"
24-
@echo " Check typing."
24+
@echo " Check typing."
2525
@echo " lint"
2626
@echo " Lint code with ruff, and check if black formatter should be applied."
2727
@echo " lint-watch"
2828
@echo " Lint code with ruff in watch mode."
2929
@echo " lint-fix"
30-
@echo " Lint code with ruff and try to fix."
31-
30+
@echo " Lint code with ruff and try to fix."
31+
3232
install:
3333
cd src && poetry install && cd ../..
3434

3535
run-app:
3636
cd src && poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 && cd ..
3737

38-
3938
run-dev-build:
40-
docker compose -f docker-compose-dev.yml up --build
39+
$(MAKE) find-port
40+
docker-compose up --build
4141

4242
run-dev:
43-
docker compose -f docker-compose-dev.yml up
43+
$(MAKE) find-port
44+
docker-compose up
4445

4546
stop-dev:
46-
docker compose -f docker-compose-dev.yml down
47-
48-
run-prod:
49-
docker compose up --build
47+
docker-compose down
5048

5149
stop-prod:
52-
docker compose down
50+
docker-compose down
5351

5452
formatter:
5553
cd src && \
@@ -70,3 +68,12 @@ lint-watch:
7068
lint-fix:
7169
cd src && \
7270
poetry run ruff app --fix
71+
72+
find-port:
73+
@echo "Finding available port..."
74+
@PORT=8000; \
75+
while lsof -Pi :$$PORT -sTCP:LISTEN -t >/dev/null ; do \
76+
PORT=$$((PORT+1)); \
77+
done; \
78+
echo "\033[0;32mUsing available port: $$PORT\033[0m"; \
79+
sed -i.bak "s/8000:8000/$$PORT:8000/g" docker-compose.yml

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ make run-app
3636

3737
Open [http://localhost:8000/docs](http://localhost:8000/docs) with your browser to see the result.
3838

39-
You can start editing the server by modifying `app/main.py`.
39+
By default, the server uses port 8000. However, if this port is already in use, the server will automatically increment and select the next available port, ensuring the application remains accessible.
40+
41+
You can start editing the server by modifying `src/app/main.py`.
4042

4143
- [fastapi-alembic-sqlmodel-async](https://github.com/jonra1993/fastapi-alembic-sqlmodel-async).
4244
- [full-stack-fastapi-postgresql](https://github.com/tiangolo/full-stack-fastapi-postgresql).

docker-compose-dev.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

docker-compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ services:
55
container_name: ${PROJECT_NAME}_fastapi_server
66
build: ./
77
restart: always
8-
command: "sh -c 'gunicorn -w 3 -k uvicorn.workers.UvicornWorker app.main:app --bind 0.0.0.0:8000 --preload --log-level=debug --timeout 120'"
8+
command: "sh -c 'uvicorn app.main:app --reload --workers 3 --host 0.0.0.0 --port 8000'"
99
volumes:
1010
- ./src:/code
11-
expose:
12-
- 8000
11+
ports:
12+
- "8001:8000"
1313
env_file: ".env"
1414

1515
caddy_reverse_proxy:
@@ -30,4 +30,4 @@ services:
3030

3131
volumes:
3232
caddy_data:
33-
caddy_config:
33+
caddy_config:

0 commit comments

Comments
 (0)