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

main.py

Before Dockerizing

Before dockerizing an app we need to follow steps below:

requirements.txt

  • Need to specify .dockerignore file.

.dockerignore

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:

2. Then we need to specify work directory that is container’s root directory.

3. Copy requirements.txt (to install dependencies) to inside of container.

4. Install Dependencies

5. Copy files of project that remains to inside of docker container

6. Expose Specified port: (Port that opened inside of code block)

7. RUN command to run FastApi Application in docker (specified host=localhost and port=10000 for test environment)

As follow as steps above, Dockerfile should be:

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:

(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:

(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
ibrahim tunc
ibrahim tunc
Articles: 13

Leave a Reply

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