Skip to content

Commit bad5b8b

Browse files
committed
Staged builds and caches for docker containers
- Add 'dev' and 'prod' profiles for use with docker-compose - Use package manager caches for pip and yarn within layers for faster local builds - Only install minimal deps for production builds
1 parent c168942 commit bad5b8b

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

.docker/app_dockerfile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
FROM node:15.14.0-stretch
1+
# syntax=docker/dockerfile:experimental
2+
FROM node:15.14.0-stretch as base
23
SHELL ["/bin/bash", "--login", "-c"]
34

45
WORKDIR /app
56
RUN npm install -g serve
67

7-
# copy repo contents and install deps
8-
#
9-
COPY webapp/package.json /app/
8+
EXPOSE 8081
109

11-
RUN yarn install
10+
COPY webapp/package.json webapp/yarn.lock ./
1211

13-
COPY webapp /app
12+
FROM base as development
13+
RUN --mount=type=cache,target=/root/.cache/yarn yarn install
14+
CMD [ "yarn", "serve", "--port", "8081"]
1415

16+
FROM base as production
17+
18+
ENV NODE_ENV production
19+
RUN --mount=type=cache,target=/root/.cache/yarn yarn install --prod
20+
COPY webapp ./
1521
RUN yarn build
1622
CMD [ "serve", "-s", "dist", "-p", "8081" ]

.docker/mongo_dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
FROM mongo:3.6-xenial
2-
32
COPY .docker/mongod.conf /etc/mongod.conf
3+
EXPOSE 27017
4+
5+
CMD ["mongod", "-f", "/etc/mongod.conf"]

.docker/server_dockerfile

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
FROM python:3.8
1+
# syntax=docker/dockerfile:experimental
2+
FROM python:3.8 as base
23
SHELL ["/bin/bash", "--login", "-c"]
4+
EXPOSE 5001
35
RUN apt update && apt install -y tree && apt clean
46

57
# Copy all server code into workdir
68
WORKDIR /app
7-
COPY ./pydatalab/ /app
8-
9-
# Install Pipenv and use it to grab dependencies
109
RUN pip install pipenv
11-
RUN pipenv install --deploy --dev
1210

13-
# Launch the server
11+
COPY Pipfile Pipfile.lock ./
12+
13+
# Create development image using flask's dev server with hot-reload
14+
FROM base as development
15+
RUN --mount=type=cache,target=/root/.cache/pip pipenv install --dev
16+
ENV FLASK_APP "pydatalab.main:create_app()"
17+
CMD ["pipenv", "run", "flask", "run", "--port", "5001", "--host", "0.0.0.0"]
18+
19+
# Create production image using gunicorn and minimal dependencies
20+
FROM base as production
21+
RUN --mount=type=cache,target=/root/.cache/pip pipenv install
22+
RUN [ "pipenv", "run", "pip", "install", "gunicorn" ]
23+
COPY ./pydatalab/ ./
1424
CMD ["pipenv", "run", \
1525
"gunicorn", \
1626
"-w", "2", \
17-
"--error-logfile", "logs/pydatalab_error.log", \
18-
"--access-logfile", "logs/pydatalab_access.log", \
27+
"--error-logfile", "/logs/pydatalab_error.log", \
28+
"--access-logfile", "/logs/pydatalab_access.log", \
1929
"-b", "0.0.0.0:5001", \
2030
"pydatalab.main:create_app()" \
2131
]

docker-compose.yml

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
1-
version: "3"
1+
version: "3.9"
22

33
services:
44
app:
55
restart: always
6+
profiles: ["prod"]
67
build:
78
context: .
89
dockerfile: .docker/app_dockerfile
9-
depends_on:
10-
- api
10+
target: production
1111
volumes:
12-
- ./logs:/app/logs/
12+
- ./logs:/logs
13+
ports:
14+
- "8081:8081"
15+
16+
app_dev:
17+
restart: always
18+
profiles: ["dev"]
19+
build:
20+
context: .
21+
dockerfile: .docker/app_dockerfile
22+
target: development
23+
volumes:
24+
- ./logs:/logs
25+
- ./webapp:/app
1326
ports:
1427
- "8081:8081"
1528

1629
api:
1730
restart: always
31+
profiles: ["prod"]
1832
build:
1933
context: .
2034
dockerfile: .docker/server_dockerfile
35+
target: production
2136
depends_on:
2237
- mongo
2338
volumes:
24-
- ./logs:/app/logs/
39+
- ./logs:/logs
40+
environment:
41+
- PYDATALAB_MONGO_URI=mongodb://mongo:27017/datalabvue
2542
ports:
2643
- "5001:5001"
44+
45+
api_dev:
46+
restart: always
47+
profiles: ["dev"]
48+
build:
49+
context: .
50+
dockerfile: .docker/server_dockerfile
51+
target: development
52+
depends_on:
53+
- mongo
54+
volumes:
55+
- ./logs:/logs
56+
- ./pydatalab:/app
2757
environment:
2858
- PYDATALAB_MONGO_URI=mongodb://mongo:27017/datalabvue
59+
ports:
60+
- "5001:5001"
2961

3062
mongo:
3163
restart: always
3264
build:
3365
context: .
3466
dockerfile: .docker/mongo_dockerfile
35-
command: ["mongod", "-f", "/etc/mongod.conf"]
3667
volumes:
37-
- ./logs:/var/logs/mongod/
68+
- ./logs:/var/logs/mongod
3869
ports:
3970
- "27017:27017"
4071

0 commit comments

Comments
 (0)