- Add
test
stage intoDockerfile
$ cd minimal-fastapi
$ poetry install
poetry run black minimal_fastapi/
$ poetry run pytest tests
$ poetry run uvicorn minimal_fastapi.main:app --reload --host localhost --port 8000
$ pyenv install 3.12.4
$ pyenv local 3.12.4
$ pyenv virtualenv minimal-fastapi
$ pyenv activate minimal-fastapi
PyEnv docs PyEnv VirtualEnv docs
$ poetry config --list
cache-dir = "$HOME/.cache/pypoetry"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
keyring.enabled = true
solver.lazy-wheel = true
virtualenvs.create = true
virtualenvs.in-project = null # We want this to TRUE so VSCODE loads python version locally
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs" # $HOME.cache/pypoetry/virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
warnings.export = true
$ poetry config virtualenvs.in-project true
Check containers IPs:
$ docker network inspect bridge -f '{{json .Containers}}'
Build images with:
docker build -t minimal-fastapi .
The Dockerfile uses multi-stage builds to run lint and test stages before building the production stage. If linting or testing fails the build will fail.
You can stop the build at specific stages with the --target
option:
# STAGE options are: development, production
docker build -t minimal-fastapi:<STAGE> --target <STAGE> .
We could then get a shell inside the container with:
docker run -it minimal-fastapi:dev bash
If you do not specify a target the resulting image will be the last image defined which in our case is the 'production' image.
Run the 'production' image:
docker run -it -p 8000:8000 minimal-fastapi:dev
Open your browser and go to http://localhost:8000/redoc to see the API spec in ReDoc.
You can build and run the container with Docker Compose
docker compose up
Or, run in detached mode if you prefer.
NOTE - If you use an older version of Docker Compose, you may need to uncomment the version in the docker-compose,yml file!