Skip to content

Commit 17eacea

Browse files
authored
Fix Docker ports & +Gunicorn (#273)
* Docker fix & invoke tasks * Use gunicorn * add docker build + run invoke tasks * Update docker-compose.yml * hardcode published port * Add docker and project task runner to readme
1 parent 9eba6a7 commit 17eacea

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Dockerfile

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
22

3-
# ENVS RECOMENDATIONS
4-
ENV PYTHONDONTWRITEBYTECODE 1
5-
ENV PYTHONUNBUFFERED 1
6-
7-
# PREPARE FOLDER
8-
WORKDIR /api
3+
ENV VARIABLE_NAME APP
94

105
# COPY DEPENDENCIES
116
COPY requirements.txt ./
127

8+
# COPY PROJECT
9+
COPY ./app /app/app
10+
1311
# INSTALL DEPENDENCIES
1412
RUN pip install --no-cache-dir -r requirements.txt
1513

16-
# COPY PROJECT
17-
COPY . /api/
18-
19-
CMD ["uvicorn", "--host", "0.0.0.0", "app.main:APP"]
14+
EXPOSE 80

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,17 @@ And don't despair if don't get the python setup working on the first try. No one
413413

414414
## Running / Development
415415

416+
For a live reloading on code changes.
417+
416418
* `pipenv run dev`
417-
* Visit your app at [http://localhost:8000](http://localhost:8000).
419+
420+
Without live reloading.
421+
422+
* `pipenv run start`
423+
424+
Visit your app at [http://localhost:8000](http://localhost:8000).
425+
426+
Alternatively run our API with Docker.
418427

419428
### Running Tests
420429
> [pytest](https://docs.pytest.org/en/latest/)
@@ -446,7 +455,22 @@ invoke generate-reqs
446455

447456
[Pipfile.lock](./Pipfile.lock) will be automatically updated during `pipenv install`.
448457

449-
### Building
458+
### Docker
459+
460+
Our Docker image is based on [tiangolo/uvicorn-gunicorn-fastapi/](https://hub.docker.com/r/tiangolo/uvicorn-gunicorn-fastapi/).
461+
462+
```bash
463+
invoke docker --build
464+
```
465+
466+
Run with `docker run` or `docker-compose`
467+
468+
### Invoke
469+
470+
Additional developer commands can be run by calling them with the [python `invoke` task runner](http://www.pyinvoke.org/).
471+
```bash
472+
invoke --list
473+
```
450474

451475
### Deploying
452476

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3.7'
1+
version: "3.7"
22

33
services:
44
api:
@@ -8,6 +8,6 @@ services:
88
volumes:
99
- .:/api
1010
ports:
11-
- "8000:8000"
11+
- "80:80"
1212
stdin_open: true
1313
tty: true

tasks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
invoke sort
1010
invoke check
1111
"""
12+
import random
13+
1214
import invoke
1315

1416
TARGETS_DESCRIPTION = "Paths/directories to format. [default: . ]"
@@ -71,3 +73,15 @@ def generate_reqs(ctx):
7173
"""Generate requirements.txt"""
7274
reqs = ["pipenv lock -r > requirements.txt", "pipenv lock -r --dev > requirements-dev.txt"]
7375
[ctx.run(req) for req in reqs]
76+
77+
78+
@invoke.task
79+
def docker(ctx, build=False, run=False, tag="covid-tracker-api:latest", name=f"covid-api-{random.randint(0,999)}"):
80+
"""Build and run docker container."""
81+
if not any([build, run]):
82+
raise invoke.Exit(message="Specify either --build or --run", code=1)
83+
if build:
84+
docker_cmds = ["build", "."]
85+
else:
86+
docker_cmds = ["run", "--publish", "80", "--name", name]
87+
ctx.run(" ".join(["docker", *docker_cmds, "-t", tag]))

0 commit comments

Comments
 (0)