By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Solving the FastAPI, Alembic, Docker Problem | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Solving the FastAPI, Alembic, Docker Problem | HackerNoon
Computing

Solving the FastAPI, Alembic, Docker Problem | HackerNoon

News Room
Last updated: 2025/12/05 at 5:36 PM
News Room Published 5 December 2025
Share
Solving the FastAPI, Alembic, Docker Problem | HackerNoon
SHARE

I was working on a FastAPI application and wanted to dockerize for development, you know the usual, Dockerfile, Docker compose and the lot.

It was meant to be a straightforward process until I ran my alembic migration command and I realized I didn’t see the version of the alembic migration in my local directory. The natural solution is to use a bind mount, which once more, should be an easy fix until it wasn’t.

I checked out different solutions and I found this one that proposed I use a bind mount but can’t simultaneously use the watch process of compose. I’m just too stubborn to accept that solution. I wanted to have my cake and eat it! I wanted my versions in my localhost after it’s generated in my container and I also want the “watch” feature from compose. The major problem was permission issues between the host and container.

This write up was my fix and should work for folks using Flask. The brilliant UV (I highly recommend) is used for package dependency management . Django users can check this out. Enough story, let’s setup.

Dockerfile

# 1. Use Python 3.13 bookworm as the base
FROM ghcr.io/astral-sh/uv:python3.13-bookworm

# 2. Set the working directory
WORKDIR /app

# 3. Set environment variables for UV
# UV_COMPILE_BYTECODE: Compiles python files to .pyc for faster startup
# UV_LINK_MODE: copy (safer for docker layers than hardlinks)
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy

# 4. create a non-root user for security
RUN useradd -u 1000 app

# 5. Set HOME explicitly to /app
# This prevents the "/nonexistent/.cache/uv" error by telling tools 
# to look for their config/cache in /app instead of /nonexistent
ENV HOME=/app

# 6. Ensures the /app directory (and everything inside) is owned by the app user
# We run this BEFORE switching users
RUN chown -R app:app /app

# 7. Copy pyproject.toml and uv.lock with correct ownership
# changing ownership during COPY is more efficient than running chown later
COPY --chown=app:app pyproject.toml uv.lock ./

# 8. Switch to the non-root user BEFORE installing dependencies
USER app

# 9. Install dependencies
# Since we are now the 'app' user, the .venv will be owned by 'app'
# and the cache will be written to /app/.cache/uv.
RUN uv sync --frozen --no-install-project --no-dev


# 10. Copy the rest of the application code
COPY --chown=app:app . .
RUN uv sync --frozen --no-dev

# 11. Expose the port
EXPOSE 8000


# 12. Command to run the application
# For production: CMD ["uv", "run", "fastapi", "run", "main.py" ...
# This assumes your main.py is in root, I usually have mine in src/main.py
# so change it accordingly
CMD ["uv", "run", "fastapi", "dev", "main.py", "--host", "0.0.0.0", "--port", "8000"]

I will explain some of my choices here, the comments should suffice for the rest.

In (4.) above, We create a user and assign a UID(user ID) value of 1000, when we create files, it is assigned a UID and GID(group ID), we specifically prefer to use 1000 because many Linux distributions assign that to the first regular user created on a system. Hence, matching 1000 can align container UID with host UID for easier file permission mapping. Feel free to switch things if this is not your use case since you now understand the rationale.

In (5.) above, I am using uv for my package and project management on my host and it just makes sense to continue that. When you install a dependency with uv, it creates a cache of that dependency globally ( i.e in your system directory) especially if you re-install a dependency or use in another project. It stores them usually in $HOME/.cache/uv, by setting this variable to /app, we are making sure it is stored in /app/.cache/uv. If we don’t do that, we get /nonexistent/.cache/uv which is either “unwritable” or absent and causes errors. You can also choose not to have a cache and it will reduce you final image size greatly especially for production, but for development, I choose to do that. You can use the UVNOCACHE flag.

Let us also look at (9.) above. Continuing from (5.) above, you could also include the –no-cache flag here.

--frozen:  uv.lock ensures consistency installs across environments.
--no-install-project: is used to avoid installing the project here for optimal layer caching. 
By separating this from the install, when the source changes, this does not get rebuilt,
the install layer(10) is done again, making your image rebuild even faster.
--no-dev: Avoids installing development dependencies.

Lovely! congratulations if you made it through that. It becomes easier from here!

Docker Compose

Let’s write the docker-compose.yaml (yes, the docker docs prefer yaml over yml). The aim is to build our fastapi application and have it connected to a postgres database. Let us write it and explain a few things:

services:

  db:
    image: postgres
    env_file:
      - .env
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql
    restart: always
  api:
    build: .
    env_file:
      - .env

    depends_on:
      - db

    ports: 
     - "8000:8000"

    develop:
      watch:
        - path: ./src
          action: sync
          target: /app/src

        - path: ./pyproject.toml
          action: rebuild
    working_dir: /app
    volumes:
      - ./migrations:/app/migrations:z
      - ./migrations/versions:/app/migrations/versions:z



volumes:
  postgres_data:

I love my docker compose file looking neat and simple and typically avoid unnecessary variable. For example, I simply usually can not wrap my head around why people choose to use environment and create a long list of environment variables in the yaml file thus excessively lengthening the file. Having a .env file and just specifying it, works a treat. You should follow these best practices for a smooth output.

Pro tip: Just use os.environ to load them in your python file.

Pro tip #2: You can view those variables with docker compose config n The key part to explain which is largely the reason why I wrote this is the “api.volumes“ section. We create a bind mount separately for the migrations and for versions under migrations. The important bit is in fact the “z” that follows. Without it, we keep getting permission errors. What does it do? In our context, it tells docker to relabel the SELinux security context of the host path so container processes can access it. Thus, I am able to use the bind mount for my migrations and hence, see my versions in my host each time I run a migration command in the container as well as use the watch feature of compose. Finally, we eat out cake and still have it!

PS: For users on macOS or Windows (which are common Docker development environments), this flag may be unnecessary and subsequently be ignored. You can test that out!

.dockerignore

__pycache__/
.venv
.ruff_cache
./src/email_templates/
.env

Finally…

I suggest creating a directory for your alembic migration say “migrations” and sub-directory “versions”, run your alembic init. Do not forget to change the script_location variable in your alembic.ini as well as edit the appropriate part of your env.py.

docker compose up --watch

In a second terminal,

docker compose exec api uv run alembic revision --autogenerate -m "Initial"
docker compose exec api uv run alembic upgrade head

Conclusion

If you completed this, I hope you have learnt a thing or two. A lot of these are my personal opinion and solution so you may find yourself not agreeing with some, that’s okay. Feel free to send me a message on LinkedIn. Thanks!!

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article WIRED Roundup: DOGE Isn’t Dead, Facebook Dating Is Real, and Amazon’s AI Ambitions WIRED Roundup: DOGE Isn’t Dead, Facebook Dating Is Real, and Amazon’s AI Ambitions
Next Article What “American Dream 2.0” Really Means (and Who Stands to Benefit) What “American Dream 2.0” Really Means (and Who Stands to Benefit)
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Google’s Most Powerful Productivity Tool Can Save You So Much Time – BGR
Google’s Most Powerful Productivity Tool Can Save You So Much Time – BGR
News
Interview: Paul Neville, director of digital, data and technology, The Pensions Regulator | Computer Weekly
Interview: Paul Neville, director of digital, data and technology, The Pensions Regulator | Computer Weekly
News
TSA Warns Travelers to Avoid Free Airport Wi-Fi
TSA Warns Travelers to Avoid Free Airport Wi-Fi
News
Secure Legion Launches First Metadata-Free Messenger with Zero Servers | HackerNoon
Secure Legion Launches First Metadata-Free Messenger with Zero Servers | HackerNoon
Computing

You Might also Like

Secure Legion Launches First Metadata-Free Messenger with Zero Servers | HackerNoon
Computing

Secure Legion Launches First Metadata-Free Messenger with Zero Servers | HackerNoon

11 Min Read
Why I Stopped Letting aI Agents Write Directly to my Database (and Built MemState) | HackerNoon
Computing

Why I Stopped Letting aI Agents Write Directly to my Database (and Built MemState) | HackerNoon

6 Min Read
When Bots Replace People: Why Your AI Strategy Needs More Humanity | HackerNoon
Computing

When Bots Replace People: Why Your AI Strategy Needs More Humanity | HackerNoon

10 Min Read
The Shift from Ad-Hoc Competitive Research to Always-On Competitive Intelligence in B2B SaaS | HackerNoon
Computing

The Shift from Ad-Hoc Competitive Research to Always-On Competitive Intelligence in B2B SaaS | HackerNoon

10 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?