Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
- Dockerfile
- Dockerimage
- Dockercontainer
In root folder, create a file 'Dockerfile'
Inside the docker file, create instruction to create DockerImage
- Basic dockerfile instruction
From python:3
ADD main.py .
RUN pip install requests beautifulsoup4
CMD [ "python", "./main.py" ]
Advance Dockerfile instruction
FROM python:3.8
WORKDIR /fastapi-app
COPY requirement.txt .
RUN pip install -r requirement.txt
COPY ./app ./app
CMD ["python", "./app/main.py"]
-
FROM python:3.8(pulls the image from docker hub)
-
WORKDIR /fastapi-app or WORKDIR /usr/src/app
-
COPY requirement.txt .
-
RUN pip install -r requirement.txt or RUN pip install --no-cache-dir -r requirements.txt
-
COPY ./app ./app or COPY . .
-
CMD ["python", "./app/main.py"]
-
docker build -t dockername
(-t: tags the image)Run the container
-
docker run python-fastapi
or -
docker run -t -i python-fastapi
(-t: Pseudo terminal, -i: interactive)` or -
docker run -d -p 8000:8000 python-fastapi
(-d, detached mode, -p: port mapping from host port to container port)
Make sure run specify port in your app as well, in last line uvicorn.run(app, port=8000, host="0.0.0.0")
To view the docker,
docker ps
(list all running container)docker exec -it dockerid /bin/sh
Now, you are inside your docker container structure.
-
Folder tutorial_youtube It has basic docker app. Used code for sample from these github, and https://www.youtube.com/watch?v=bi0cKgmRuiA
-
Folder tutorial_advance_youtube from https://www.youtube.com/watch?v=bi0cKgmRuiA
-
Folder freeCodeCamp_tutorials from freeCodeCampe youtube
Using FASTAPI.
Tu run we can use
uvicorn main:app --reload
Or another way, by importing uvicorn lib,
And test it on localport.
If everything working, then pip freeze > requirement.txt
Then start Dockerizing