What is Docker: Docker is a platform and tool that simplifies the process of building, deploying, and managing applications using containers.
What is Dockerization/Containerization: Dockerizartion or Containerzation is a method of packaging and deploying software applications along with their dependencies into standardized units called containers.
Sample FastApi Test App
Sample FastApi app structure:
To clone test repository: https://github.com/ibobey/Dockerize-FastApi
Project Contains:
- api: package that contains FastApi
- venv: viritual environrment that contains python environment
- .dockerignore: A file that ignore-instructions from docker contianer while dockerizing
- .gitignore: Files and folders that ignored from VCS (git)
- Dockerfile: That includes Containerzation instructions.
- main.py: Main Python file that includes python scripts
- requirements.txt: Text file that includes python dependencies.
api.api.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from fastapi import FastAPI app = FastAPI() @app.get('/') async def root(): return {"directory": "root"} @app.get('/api') async def api(): return {"directory": "api", "version": "1.0.0", "description": "Test Api"} |
main.py
1 | from api.api import app |
Before Dockerizing
Before dockerizing an app we need to follow steps below:
- Need to install docker desktop (docker official https://www.docker.com/products/docker-desktop/)
- Need to specify requirements.txt that includes non built-in modules. ( For more about venv and requirements.txt https://hawx-team.com/virtual-environment-python-venv/)
requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 | annotated-types==0.6.0 anyio==4.3.0 click==8.1.7 colorama==0.4.6 fastapi==0.109.2 h11==0.14.0 idna==3.6 pydantic==2.6.1 pydantic_core==2.16.2 sniffio==1.3.0 starlette==0.36.3 typing_extensions==4.9.0 uvicorn==0.27.1 |
- Need to specify .dockerignore file.
.dockerignore
1 | venv |
venv files should be ignored from docker, because docker container downloads own specified python environment and dependencies automaticly.
Dockerize FastApi Application
After we followed steps below:
- Docker Desktop installed
- venv ignored from docker
- specified dependencies (requirements.txt file)
We are ready to dockerize our application.
1. First we need to specify python version:
1 | FROM python:3.12-alpine |
2. Then we need to specify work directory that is container’s root directory.
1 | WORKDIR /app |
3. Copy requirements.txt (to install dependencies) to inside of container.
1 | COPY requirements.txt . |
4. Install Dependencies
1 | RUN pip install --no-cache-dir --upgrade -r requirements.txt |
5. Copy files of project that remains to inside of docker container
1 | COPY . . |
6. Expose Specified port: (Port that opened inside of code block)
1 | EXPOSE 10000 |
7. RUN command to run FastApi Application in docker (specified host=localhost and port=10000 for test environment)
1 | CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--port=10000"] |
As follow as steps above, Dockerfile should be:
1 2 3 4 5 6 7 8 9 10 11 12 13 | FROM python:3.12-alpine WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir --upgrade -r requirements.txt COPY . . EXPOSE 10000 CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--port=10000"] |
Build an Docker Image
Docker Image: : Docker images are read-only templates that contain everything needed to run an application, including the code, runtime environment, libraries, and dependencies. Images are built using a Dockerfile, which specifies the instructions for creating the image.
To build an image we need to open terminal that includes Dockerfile on root directory.
We need to run:
1 | docker build -t fastapi-test-app . |
(Docker desktop need to installed and active)
- -t: Specified tag(name) of docker image that contains or project
- “.”: Dockerfile location (“.” means Dockerfile in root folder)
- docker build: Build an image that specified dockerfile and named by -t flag
Run Docker Container
After we build an docker image, we can run conatiner:
1 | docker run -p 10000:10000 fastapi-test-app |
(Docker desktop need to installed and active)
- -p: Port flag, that specifies port inside of docker-container and publishes port to our local computer. (port 10000: runs fastapi application inside of docker-container and port :10000 we can access from our local computer to container)
- fastapi-test-app: Name of docker image that gonna be runs an contianer.
- docker run: Runs docker-container that maden from specified docker image and published from specified port
Access Application
To access application that run inside of contianer and publishes a port to access container.
We need to visit our local connection: http://localhost:10000 (:10000 is a port number that we specified port above)
Summary
- Docker is a fast and compatible tool for development and deployment.
- Before we dockerize an app we need to: install docker, specify dependendices and files that ignored from docker
- We need to write a Dockerfile that setups code environment and dependencies, ports and run commands.
- We need to build an image by using dockerfile that includes all environment and dependencies for project.
- Than we need to run container and publish ports to connect container