Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Docker support for Folksonomy API #254

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

ekas-7
Copy link

@ekas-7 ekas-7 commented Mar 12, 2025

add Docker support for Folksonomy API

What

  • Add Docker support to simplify development and deployment of Folksonomy API
  • Create multi-stage Dockerfile to optimize container size and build efficiency
  • Configure docker-compose.yml with properly networked services for API and PostgreSQL
  • Include non-root user configuration and security best practices
  • Add comprehensive .dockerignore to prevent unnecessary files in image builds

Screenshot

image image

Fixes bug(s)

  • Resolves environment inconsistency issues between developers
  • Fixes setup problems on modern Linux distributions like Debian 12

Part of

- Create multi-stage Dockerfile for efficient image building
- Configure docker-compose.yml with PostgreSQL service
- Set up PostgreSQL on alternative port (5433) to avoid conflicts
- Add .dockerignore to optimize Docker builds
@ekas-7 ekas-7 requested a review from a team as a code owner March 12, 2025 19:58
@ekas-7 ekas-7 changed the title Add Docker support for Folksonomy API feat: add Docker support for Folksonomy API Mar 12, 2025
@areebahmeddd
Copy link
Member

areebahmeddd commented Mar 13, 2025

Hey @ekas-7,

It's best to create an issue first for this pull request and then reference it. Also noticed you're pushing from the main branch, please create your own branch and push from there instead.

@codecov-commenter
Copy link

codecov-commenter commented Mar 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.57%. Comparing base (b9cda65) to head (f7639a6).
Report is 82 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #254      +/-   ##
==========================================
- Coverage   95.06%   94.57%   -0.49%     
==========================================
  Files           5        5              
  Lines         324      387      +63     
==========================================
+ Hits          308      366      +58     
- Misses         16       21       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekas-7 that's a great PR.

I have some comments though, I let you fix them.

Dockerfile Outdated
Comment on lines 47 to 48
# Copy the local_settings example to local_settings.py (if not exists)
RUN if [ ! -f local_settings.py ]; then cp local_settings_example.py local_settings.py; fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to do this.

If someone wants to use a local_settings it's better to mount it in the container. It's a bad idea to have it in container image IMO. (and it's not a mandatory file)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

Dockerfile Outdated
Comment on lines 61 to 62
CMD python db-migration.py && \
uvicorn folksonomy.api:app --host 0.0.0.0 --port 8000 --proxy-headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not to automatically run migrations. My long experience tells me it's better that migration are run by the user manually as needed (eg., in production, after a release).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docker compose exec api python db-migration.py
creted a command for mannual migration

README.md Outdated
Comment on lines 37 to 39
## Requirements
- Docker
- Docker Compose
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give a minimal version number, as we sometimes have problem with that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version Updated in Readme.md

README.md Outdated
Comment on lines 49 to 52
2. Start the services
```bash
docker-compose up -d
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use docker compose instead of docker-compose (deprecated)

consistently which my comment above, add the migration run (docker compose run --rm folksonomy_api python db-migration.py, if we set postgres as a dependency of folksonomy_api)

README.md Outdated
```bash
./generate_openapi_json.py
psql -h localhost -p 5433 -U folksonomy -d folksonomy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why rely on having psql localy instead of using the container ?

docker compose exec -u postgres folksonomy_db psql

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected with :
docker compose exec db psql -U folksonomy -d folksonomy

Comment on lines 12 to 16
environment:
- POSTGRES_USER=folksonomy
- POSTGRES_PASSWORD=folksonomy
- POSTGRES_DATABASE=folksonomy
- POSTGRES_HOST=db
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to put variable in a .env and reuse them here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

volumes:
# Mount .env file for environment variables
- ./.env:/app/.env
# Mount a volume for logs
- api_logs:/app/logs
env_file:
- .env
depends_on:

context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't publish on all ports, this can be a security risk in public wifi.

So by default, publish to 127.0.0.1:8000

the best would be to use a variable and set it in .env (I would call it API_EXPOSE):

     - "${API_EXPOSE}:8000"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ports:
- "${API_EXPOSE:-8000}:8000"

- POSTGRES_PASSWORD=folksonomy
- POSTGRES_DB=folksonomy
ports:
- "5433:5432"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't expose the database and surely not on all address (see above)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to
ports:
- "${DB_EXPOSE:-5433}:5432"

@ekas-7
Copy link
Author

ekas-7 commented Mar 13, 2025

@alexgarel sir right onto improvements that u have mentioned in your comments

@ekas-7
Copy link
Author

ekas-7 commented Mar 13, 2025

@alexgarel i have taken all the comments into account and created a new version of codebase.
Kindly let me know if any more changes are required .
Thank u for ur guidance got a lot of exposure from ur comments how to dockerise a codebase for prod level.

@alexgarel
Copy link
Member

@ekas-7 #247 is finally merged ! You can adapt your PR according to it.

@ekas-7 also look at the documentation bearing in mind that docker is an option, but not the only option for developers.

Thanks.

@ekas-7
Copy link
Author

ekas-7 commented Mar 18, 2025

@alexgarel I will make changes by EOD. Thank u for ur insights on docker .

@ekas-7
Copy link
Author

ekas-7 commented Mar 18, 2025

Screen.Recording.2025-03-18.at.7.1.mp4

@alexgarel i think we are good to go
Thank u for ur insights on docker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants