Skip to content

Commit a61dff2

Browse files
rkorsakkeithrfung
authored andcommitted
✨ Improved Dockerfile (#59)
- Combines apt-get layers into one - Re-orders layers to move most-changed to the end - Splits out dependencies from source - Only the app folder needs to be copied - Only uses pipenv to generate a requirements file - it isn't necessary to create a virtual env on the container - Adds a .dockerignore with common python artifacts. I'm not seeing any __pycache__ or .pyc files being generated at this time, but it doesn't hurt to have this. - Parameterizes the host URL at the docker image level, and parameterizes the port + url at the makefile level. E.g. `make docker-run CONTAINER_ADDRESS="something else" CONTAINER_PORT=80` See [this documentation](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#minimize-the-number-of-layers) for best practices regarding layers and build cache.
1 parent e9dabb8 commit a61dff2

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__
2+
*.pyc
3+
*.pyo
4+
*.pyd

Dockerfile

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
FROM python:3.8
2-
COPY . .
3-
RUN apt update
4-
RUN apt-get install libgmp-dev
5-
RUN apt-get install libmpfr-dev
6-
RUN apt-get install libmpc-dev
2+
ENV host 0.0.0.0
3+
4+
RUN apt update && apt-get install -y \
5+
libgmp-dev \
6+
libmpfr-dev \
7+
libmpc-dev
78
RUN pip install pipenv
8-
RUN pipenv install
9+
10+
COPY ./Pipfile* /tmp/
11+
RUN cd /tmp && pipenv lock --requirements > requirements.txt
12+
RUN pip install -r /tmp/requirements.txt
13+
14+
COPY ./app /app
15+
916
EXPOSE 8000
10-
CMD pipenv run uvicorn app.main:app --host 0.0.0.0 --port 8000
17+
CMD uvicorn app.main:app --host "$host" --port 8000

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
OS ?= $(shell python -c 'import platform; print(platform.system())')
44
WINDOWS_ERROR = ⚠️ UNSUPPORTED WINDOWS INSTALL ⚠️
55
CONTAINER_NAME = electionguard_web_api
6+
CONTAINER_ADDRESS ?= 0.0.0.0
7+
CONTAINER_PORT ?= 8000
68
IMAGE_NAME = electionguard_web_api
79

810
all: environment lint start
@@ -52,7 +54,7 @@ docker-build:
5254
docker build -t $(IMAGE_NAME) .
5355

5456
docker-run:
55-
docker run -d --name $(CONTAINER_NAME) -p 8000:8000 $(IMAGE_NAME)
57+
docker run -d --name $(CONTAINER_NAME) -p $(CONTAINER_PORT):8000 -e host=$(CONTAINER_ADDRESS) $(IMAGE_NAME)
5658

5759
# Linting
5860
lint:

0 commit comments

Comments
 (0)