This project contains a simple and practical example of a Dockerfile to help beginners learn how to use Docker.
- Docker installed on your OS. Docker Installation
- Basic knowledge of the Linux command line.
-
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim: Use the official fastapi image as our base image. We're using the slim version to keep the image small. -
WORKDIR /app: Set the working directory in the container to/app. -
COPY requirements.txt .: Copy therequirements.txtfile from the current directory to the container which is/appdirectory, because in the previous step we have usedWORKDIRthis directive makes/appdirectory and goes in it. -
RUN pip install --no-cache-dir -r requirements.txt: Install the project dependencies using pip. -
COPY main.py .: Copy the rest of the application code to the container which is/app -
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]: Set the command to start the application.
-
Clone the repository:
git clone https://github.com/devopshobbies/docker-templates.git
-
Build the Docker image:
cd docker-templates/13-FastApi docker build -t hello-fastapi .
-
Run the Docker container:
docker container run --detach --publish 80:80 hello-fastapi
-d,--detach Run container in background and print container ID
-p, --publish list Publish a container's port(s) to the host