diff --git a/docs/en/docs/deployment/docker.md b/docs/en/docs/deployment/docker.md index 4806d779d..309423453 100644 --- a/docs/en/docs/deployment/docker.md +++ b/docs/en/docs/deployment/docker.md @@ -66,10 +66,10 @@ For example, there's an official PostgreSQL -* MySQL -* MongoDB -* Redis, etc. +- PostgreSQL +- MySQL +- MongoDB +- Redis, etc. By using a pre-made container image it's very easy to **combine** and use different tools. For example, to try out a new database. In most cases, you can use the **official images**, and just configure them with environment variables. @@ -99,9 +99,9 @@ I'll show you how to build a **Docker image** for FastAPI **from scratch**, base This is what you would want to do in **most cases**, for example: -* Using **Kubernetes** or similar tools -* When running on a **Raspberry Pi** -* Using a cloud service that would run a container image for you, etc. +- Using **Kubernetes** or similar tools +- When running on a **Raspberry Pi** +- Using a cloud service that would run a container image for you, etc. ### Package Requirements { #package-requirements } @@ -111,7 +111,7 @@ It would depend mainly on the tool you use to **install** those requirements. The most common way to do it is to have a file `requirements.txt` with the package names and their versions, one per line. -You would of course use the same ideas you read in [About FastAPI versions](versions.md){.internal-link target=_blank} to set the ranges of versions. +You would of course use the same ideas you read in [About FastAPI versions](versions.md){.internal-link target=\_blank} to set the ranges of versions. For example, your `requirements.txt` could look like: @@ -140,9 +140,9 @@ There are other formats and tools to define and install package dependencies. ### Create the **FastAPI** Code { #create-the-fastapi-code } -* Create an `app` directory and enter it. -* Create an empty file `__init__.py`. -* Create a `main.py` file with: +- Create an `app` directory and enter it. +- Create an empty file `__init__.py`. +- Create a `main.py` file with: ```Python from fastapi import FastAPI @@ -188,41 +188,47 @@ CMD ["fastapi", "run", "app/main.py", "--port", "80"] 2. Set the current working directory to `/code`. - This is where we'll put the `requirements.txt` file and the `app` directory. + This is where we'll put the `requirements.txt` file and the `app` directory. 3. Copy the file with the requirements to the `/code` directory. - Copy **only** the file with the requirements first, not the rest of the code. + Copy **only** the file with the requirements first, not the rest of the code. - As this file **doesn't change often**, Docker will detect it and use the **cache** for this step, enabling the cache for the next step too. + As this file **doesn't change often**, Docker will detect it and use the **cache** for this step, enabling the cache for the next step too. 4. Install the package dependencies in the requirements file. - The `--no-cache-dir` option tells `pip` to not save the downloaded packages locally, as that is only if `pip` was going to be run again to install the same packages, but that's not the case when working with containers. + The `--no-cache-dir` option tells `pip` to not save the downloaded packages locally, as that is only if `pip` was going to be run again to install the same packages, but that's not the case when working with containers. - /// note + /// note - The `--no-cache-dir` is only related to `pip`, it has nothing to do with Docker or containers. + The `--no-cache-dir` is only related to `pip`, it has nothing to do with Docker or containers. - /// + /// - The `--upgrade` option tells `pip` to upgrade the packages if they are already installed. + The `--upgrade` option tells `pip` to upgrade the packages if they are already installed. - Because the previous step copying the file could be detected by the **Docker cache**, this step will also **use the Docker cache** when available. + Because the previous step copying the file could be detected by the **Docker cache**, this step will also **use the Docker cache** when available. - Using the cache in this step will **save** you a lot of **time** when building the image again and again during development, instead of **downloading and installing** all the dependencies **every time**. + Using the cache in this step will **save** you a lot of **time** when building the image again and again during development, instead of **downloading and installing** all the dependencies **every time**. 5. Copy the `./app` directory inside the `/code` directory. - As this has all the code which is what **changes most frequently** the Docker **cache** won't be used for this or any **following steps** easily. + As this has all the code which is what **changes most frequently** the Docker **cache** won't be used for this or any **following steps** easily. - So, it's important to put this **near the end** of the `Dockerfile`, to optimize the container image build times. + So, it's important to put this **near the end** of the `Dockerfile`, to optimize the container image build times. 6. Set the **command** to use `fastapi run`, which uses Uvicorn underneath. - `CMD` takes a list of strings, each of these strings is what you would type in the command line separated by spaces. + `CMD` takes a list of strings, each of these strings is what you would type in the command line separated by spaces. - This command will be run from the **current working directory**, the same `/code` directory you set above with `WORKDIR /code`. + This command will be run from the **current working directory**, the same `/code` directory you set above with `WORKDIR /code`. + + /// tip + + The --reload option is useful during local development as it enables automatic code reloading. However, it is not recommended for production Docker deployments, as it adds file-watching overhead. When running inside a container, use a standard command without --reload. + + /// /// tip @@ -254,7 +260,7 @@ CMD ["fastapi", "run", "app/main.py", "--port", "80"] CMD fastapi run app/main.py --port 80 ``` -Make sure to always use the **exec** form to ensure that FastAPI can shutdown gracefully and [lifespan events](../advanced/events.md){.internal-link target=_blank} are triggered. +Make sure to always use the **exec** form to ensure that FastAPI can shutdown gracefully and [lifespan events](../advanced/events.md){.internal-link target=\_blank} are triggered. You can read more about it in the Docker docs for shell and exec form. @@ -317,8 +323,8 @@ COPY ./app /code/app Now that all the files are in place, let's build the container image. -* Go to the project directory (in where your `Dockerfile` is, containing your `app` directory). -* Build your FastAPI image: +- Go to the project directory (in where your `Dockerfile` is, containing your `app` directory). +- Build your FastAPI image: