Skip to content

ankusnayak/dockerize-fastapi-application-example

Repository files navigation

Run FastAPI Application

In Local:

uvicorn main:app --host 0.0.0.0 --port 8080 --reload

Host port where the server is running

http://127.0.0.1:8080/channels/codestackrs

http://localhost:8080/

localhost == 127.0.0.1

Docker

Docker allows us to run application on isolated environment. Container running on preferred linux distribution. More of create image where you can pre install software change anything you like. Treat it like a virtual machine. You can run these images on locally on your machine and also neatly integrated on cloud based technologies such as kubernetes.

How docker works:

Create Docker image based on the required settings then run that image in container locally or in production(server) that does not matter, same environment everywhere.

Install Docker -> then supply how our docker image look like (env settings and all) for that we need Dockerfile

How to create a Docker image

Start creating a image based on the Dockerfile

    docker build -t yt-channel-testapi .

-t <tag_name> build the image form the folder we currently in using . (dot)

Now create a local docker container to run the image locally on our local machine

specify the port number to published this docker container in our local machine (8080) then map 8080 local docker container port with internal (docker image) port

    docker run -d -p 8080:80 yt-channel-testapi

Why we use docker-compose ?

otherwise every time when there is a change in Dockerfile we need to stop the container, remove the container and then create a new container using that image. Thats where docker-compose helps to handles these scenarios.

Dependency Change:

when there is a dependency change then we need to rebuild the and it also the first time operation:

    docker-compose up --build

docker-compose up:

it will helps with development. when there is only code change then no need to rebuild the image again container. For that case we use this command(only code change)

    docker-compose up

docker-compose stop :

only stop the running containers

    docker-compose stop

docker-compose start :

resume the same container again and this is the same container, same data etc that we stops earlier.

    docker-compose start

what docker-compose down do ?

It will remove container, Stop container, keeps images, keeps volume as it is.

    docker-compose down

Mental Model: when to use what: for development

Scenario What you should do
First time docker-compose up --build
Code change (Python) NO rebuild needed
Dockerfile change Rebuild required
Dependency change Rebuild required

Build confusion solution:

This is common confusion while working with docker.

Scenario What it does
build: . tells how to build
--build tells when to build.

What does --build rebuild?

`--build` rebuilds the IMAGE — NOT the container

What actually happens with docker-compose up --build

Sequence: 1. Rebuild IMAGE bash docker build . 2. Check existing container 3. If image hash changed → recreate container 4. Start container

So:

Item Rebuilt?
Docker image ✅ YES
Container ❌ Not rebuilt — recreated

⚠️ Containers are never “rebuilt”. They are created or destroyed.

Important:

Image:

  • Immutable
  • Built from Dockerfile
  • Can be rebuilt

Container:

  • Runtime instance
  • Cannot be rebuilt
  • Must be removed & recreated

Why container gets recreated after --build

Docker sees: Old image hash ≠ New image hash

A container cannot change its image, so Docker must:

stop old container
remove old container
create new container from new image
start new container

Proof (real commands behind the scenes) docker-compose up --build ≈

    docker build .
    docker stop channel-api
    docker rm channel-api
    docker create channel-api
    docker start channel-api

Mental model: (--build)

Dockerfile ──build──▶ Image ──create──▶ Container

About

This is an example of how to dockerize a fastapi application

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published