In Local:
uvicorn main:app --host 0.0.0.0 --port 8080 --reloadhttp://127.0.0.1:8080/channels/codestackrs
localhost == 127.0.0.1
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.
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
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)
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-testapiotherwise 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.
when there is a dependency change then we need to rebuild the and it also the first time operation:
docker-compose up --buildit 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 uponly stop the running containers
docker-compose stopresume the same container again and this is the same container, same data etc that we stops earlier.
docker-compose startIt will remove container, Stop container, keeps images, keeps volume as it is.
docker-compose down| 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 |
This is common confusion while working with docker.
| Scenario | What it does |
|---|---|
| build: . | tells how to build |
| --build | tells when to build. |
`--build` rebuilds the IMAGE — NOT the container
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 |
Image:
- Immutable
- Built from Dockerfile
- Can be rebuilt
Container:
- Runtime instance
- Cannot be rebuilt
- Must be removed & recreated
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-apiDockerfile ──build──▶ Image ──create──▶ Container