diff --git a/Dockerfile b/Dockerfile index b9cab02a..25e21bc5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,14 @@ FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 -# ENVS RECOMENDATIONS -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 - -# PREPARE FOLDER -WORKDIR /api +ENV VARIABLE_NAME APP # COPY DEPENDENCIES COPY requirements.txt ./ +# COPY PROJECT +COPY ./app /app/app + # INSTALL DEPENDENCIES RUN pip install --no-cache-dir -r requirements.txt -# COPY PROJECT -COPY . /api/ - -CMD ["uvicorn", "--host", "0.0.0.0", "app.main:APP"] \ No newline at end of file +EXPOSE 80 diff --git a/README.md b/README.md index fb5ad988..4fdbd3a2 100644 --- a/README.md +++ b/README.md @@ -413,8 +413,17 @@ And don't despair if don't get the python setup working on the first try. No one ## Running / Development +For a live reloading on code changes. + * `pipenv run dev` -* Visit your app at [http://localhost:8000](http://localhost:8000). + +Without live reloading. + +* `pipenv run start` + +Visit your app at [http://localhost:8000](http://localhost:8000). + +Alternatively run our API with Docker. ### Running Tests > [pytest](https://docs.pytest.org/en/latest/) @@ -446,7 +455,22 @@ invoke generate-reqs [Pipfile.lock](./Pipfile.lock) will be automatically updated during `pipenv install`. -### Building +### Docker + +Our Docker image is based on [tiangolo/uvicorn-gunicorn-fastapi/](https://hub.docker.com/r/tiangolo/uvicorn-gunicorn-fastapi/). + +```bash +invoke docker --build +``` + +Run with `docker run` or `docker-compose` + +### Invoke + +Additional developer commands can be run by calling them with the [python `invoke` task runner](http://www.pyinvoke.org/). +```bash +invoke --list +``` ### Deploying diff --git a/docker-compose.yml b/docker-compose.yml index 6f599be8..aa870110 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3.7' +version: "3.7" services: api: @@ -8,6 +8,6 @@ services: volumes: - .:/api ports: - - "8000:8000" + - "80:80" stdin_open: true tty: true diff --git a/tasks.py b/tasks.py index bf2a60df..06a52486 100644 --- a/tasks.py +++ b/tasks.py @@ -9,6 +9,8 @@ invoke sort invoke check """ +import random + import invoke TARGETS_DESCRIPTION = "Paths/directories to format. [default: . ]" @@ -71,3 +73,15 @@ def generate_reqs(ctx): """Generate requirements.txt""" reqs = ["pipenv lock -r > requirements.txt", "pipenv lock -r --dev > requirements-dev.txt"] [ctx.run(req) for req in reqs] + + +@invoke.task +def docker(ctx, build=False, run=False, tag="covid-tracker-api:latest", name=f"covid-api-{random.randint(0,999)}"): + """Build and run docker container.""" + if not any([build, run]): + raise invoke.Exit(message="Specify either --build or --run", code=1) + if build: + docker_cmds = ["build", "."] + else: + docker_cmds = ["run", "--publish", "80", "--name", name] + ctx.run(" ".join(["docker", *docker_cmds, "-t", tag]))