Skip to content

Commit cc40a82

Browse files
authored
Dockerfile refactor (#157)
* Use multi-stage Dockerfile * fixup! Use multi-stage Dockerfile
1 parent 2f23298 commit cc40a82

File tree

8 files changed

+136
-103
lines changed

8 files changed

+136
-103
lines changed

.circleci/config.yml

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ jobs:
118118
steps:
119119
- test
120120

121+
build-dev-certs:
122+
executor:
123+
name: elixir-builder
124+
env: dev
125+
erlang_version: "22.0"
126+
elixir_version: "1.9"
127+
steps:
128+
- checkout
129+
- restore_cache:
130+
keys:
131+
- mix-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
132+
- restore_cache:
133+
keys:
134+
- build-${CIRCLE_JOB}-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
135+
- run:
136+
name: Generate dev certs
137+
command: mix certs.dev
138+
- save_cache:
139+
key: certs-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
140+
paths:
141+
- priv
142+
121143
test-erlang-22_elixir-1-8:
122144
executor:
123145
name: elixir-builder
@@ -173,7 +195,7 @@ jobs:
173195
- mix-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
174196
- run: mix format --check-formatted
175197

176-
build-tar-and-docker-image:
198+
build-docker-image:
177199
executor:
178200
name: elixir-builder
179201
env: prod
@@ -184,32 +206,40 @@ jobs:
184206
- restore_cache:
185207
keys:
186208
- mix-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
187-
- run:
188-
name: Mix build release
189-
command: mix do certs.dev, distillery.release
190-
- save_cache:
191-
key: certs-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
192-
paths:
193-
- "priv"
194-
- run:
195-
name : Build tar archive
196-
command: tar -czf mongoose_push.tar.gz -C _build/${MIX_ENV}/rel/mongoose_push .
197209
- run: *DOCKER_TAGS
198210
- run:
199211
name: Login to DockerHub
200212
command: docker login -u "${DOCKERHUB_USER}" -p "${DOCKERHUB_PASS}"
201213
- run:
202214
name: Build docker image
203-
command: docker build -f Dockerfile.release --build-arg MIX_ENV=prod -t $DOCKERHUB_REPOSITORY/$DOCKER_IMAGE:$DOCKER_TAG .
215+
command: docker build -f Dockerfile --build-arg MIX_ENV=prod -t mpush:latest .
216+
- run:
217+
name: Push docker image but do not tag it as the latest
218+
command: docker tag mpush:latest $DOCKERHUB_REPOSITORY/$DOCKER_IMAGE:$DOCKER_TAG
204219
- run:
205220
name: Push docker image but do not tag it as the latest
206221
command: docker push $DOCKERHUB_REPOSITORY/$DOCKER_IMAGE:$DOCKER_TAG
222+
- run:
223+
name: Save Docker image layer cache
224+
command: |
225+
mkdir -p /tmp/caches
226+
docker save -o /tmp/caches/mpush-image.tar $DOCKERHUB_REPOSITORY/$DOCKER_IMAGE:$DOCKER_TAG
227+
- save_cache:
228+
key: docker-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
229+
paths:
230+
- /tmp/caches/mpush-image.tar
207231

208232
integration-tests:
209233
machine:
210234
image: ubuntu-1604:201903-01
211235
steps:
212236
- checkout
237+
- restore_cache:
238+
keys:
239+
- docker-cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ .Revision }}
240+
- run:
241+
name: Load the MongoosePush Docker image
242+
command: docker load -i /tmp/caches/mpush-image.tar
213243
- run:
214244
name: Install elixir
215245
command: |
@@ -235,7 +265,7 @@ jobs:
235265
name: Setup mocks and MPush
236266
command: |
237267
export PRIV=$HOME/app/priv && \
238-
docker-compose -f /home/circleci/project/test/docker/integration.yml up -d
268+
docker-compose -f /home/circleci/project/test/docker/docker-compose.integration.yml up -d
239269
- run:
240270
name: Wait for FCM mock
241271
command: sudo dockerize -wait tcp://localhost:4001 -timeout 1m
@@ -302,14 +332,19 @@ workflows:
302332
requires:
303333
- pre-build
304334
filters: *all_tags
305-
- build-tar-and-docker-image:
335+
- build-dev-certs:
336+
requires:
337+
- pre-build
338+
filters: *all_tags
339+
- build-docker-image:
306340
requires:
307341
- pre-build
308342
filters: *all_tags
309343

310344
- integration-tests:
311345
requires:
312-
- build-tar-and-docker-image
346+
- build-docker-image
347+
- build-dev-certs
313348
filters: *all_tags
314349

315350
- push-to-dockerhub:

Dockerfile.release renamed to Dockerfile

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1+
FROM rslota/beam-builder:erlang-22.0_elixir-1.9 AS builder
2+
3+
USER root
4+
5+
WORKDIR /opt/app
6+
ENV HOME=/opt/app
7+
ENV MIX_ENV=prod
8+
9+
COPY mix.exs mix.lock ./
10+
COPY config config
11+
COPY asn.1 asn.1
12+
COPY rel rel
13+
COPY lib lib
14+
COPY priv priv
15+
16+
# Install Hex+Rebar
17+
RUN mix local.hex --force && \
18+
mix local.rebar --force
19+
20+
21+
RUN mix deps.get
22+
RUN mix do certs.dev, distillery.release
23+
RUN tar -czf mongoose_push.tar.gz -C _build/prod/rel/mongoose_push .
24+
25+
126
FROM debian:stretch-slim
227

28+
329
# set locales
430
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y locales
531
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
@@ -28,7 +54,8 @@ ENV PUSH_HTTPS_BIND_ADDR=0.0.0.0 PUSH_HTTPS_PORT=8443 MIX_ENV=prod \
2854

2955
WORKDIR /opt/app
3056

31-
ADD mongoose_push.tar.gz ./
57+
COPY --from=builder /opt/app/mongoose_push.tar.gz mongoose_push.tar.gz
58+
RUN tar -xf mongoose_push.tar.gz ./
3259

3360
# Move priv dir
3461
RUN mv $(find lib -name mongoose_push-*)/priv .

Dockerfile.build

Lines changed: 0 additions & 33 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ docker run -v `pwd`/priv:/opt/app/priv \
4545
Building docker is really easy, just type:
4646

4747
```bash
48-
MIX_ENV=prod mix do deps.get, certs.dev, docker.build, docker.release
48+
docker build . -t mpush:latest
4949
```
5050

51-
As a result of this command you get access to `mongoose_push:release` docker image. You may run it by typing:
51+
As a result of this command you get access to `mpush:latest` docker image. You may run it by typing:
5252

5353
```bash
54-
docker run -it --rm mongoose_push:release foreground
54+
docker run -it --rm mpush:latest foreground
5555
```
5656

5757
The docker image that you have just built, exposes the port `8443` for the HTTP API of MongoosePush. It contains a `VOLUME` for path */opt/app* - it is handy for injecting `APNS` and `HTTP API` certificates since by default the docker image comes with test, self-signed certificates.
@@ -138,7 +138,7 @@ Development release is by default configured to connect to local APNS / FCM mock
138138
in `config/dev.exs` file.
139139
For now, let's just start those mocks so that we can use default dev configuration:
140140
```bash
141-
docker-compose -f test/docker/docker-compose.yml up -d
141+
docker-compose -f test/docker/docker-compose.unit.yml up -d
142142
```
143143

144144
After this step you may try to run the service via:
@@ -151,7 +151,7 @@ _build/dev/rel/mongoose_push/bin/mongoose_push console
151151

152152
Setup FCM and APNS mocks first:
153153
```bash
154-
$ docker-compose -f test/docker/docker-compose.yml up -d
154+
$ docker-compose -f test/docker/docker-compose.unit.yml up -d
155155
```
156156

157157
Generate certificates. This step is needed to be run only once:
@@ -166,7 +166,7 @@ $ mix test
166166

167167
You can cleanup docker after tests by calling:
168168
```bash
169-
$ docker-compose -f test/docker/docker-compose.yml down
169+
$ docker-compose -f test/docker/docker-compose.unit.yml down
170170
```
171171

172172
## Configuration
@@ -329,7 +329,7 @@ If you specify both **alert** and **data**, target device will receive both noti
329329
* **429** `{"reason" : "too_many_requests"}` - there were too many requests to the server.
330330
* **503** `{"reason" : "service_internal"|"internal_config"|"unspecified"}` - the internal service or configuration error occured.
331331
* **520** `{"reason" : "unspecified"}` - the unknown error occured.
332-
* **500** `{"reason" : reason}` - the server internal error occured,
332+
* **500** `{"reason" : reason}` - the server internal error occured,
333333
specified by **reason**.
334334

335335
## Metrics
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: '3'
2+
3+
services:
4+
fcm-mock:
5+
image: mongooseim/fcm-mock-server
6+
container_name: fcm-mock
7+
ports:
8+
- "4001:4001"
9+
apns-mock:
10+
image: mobify/apns-http2-mock-server
11+
container_name: apns-mock
12+
ports:
13+
- "2197:2197"
14+
15+
mongoose-push:
16+
image: mpush:latest
17+
build:
18+
context: ../../
19+
dockerfile: Dockerfile
20+
21+
container_name: mongoose-push
22+
environment:
23+
- PUSH_APNS_ENABLED=true
24+
- PUSH_APNS_PROD_ENDPOINT=apns-mock
25+
- PUSH_APNS_PROD_USE_2197=true
26+
- PUSH_APNS_AUTH_TYPE=certificate
27+
- PUSH_APNS_DEV_ENDPOINT=apns-mock
28+
- PUSH_APNS_DEV_USE_2197=true
29+
- PUSH_FCM_ENABLED=true
30+
- PUSH_FCM_PORT=4000
31+
- PUSH_FCM_ENDPOINT=fcm-mock
32+
- TLS_SERVER_CERT_VALIDATION=false
33+
- FCM_AUTH_ENDPOINT=http://fcm-mock:4001
34+
ports:
35+
- "8443:8443"
36+
volumes:
37+
- ${PRIV}:/opt/app/priv
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '3'
2+
3+
services:
4+
fcm-mock:
5+
image: mongooseim/fcm-mock-server
6+
container_name: fcm-mock
7+
ports:
8+
- "4001:4001"
9+
- "4000:4000"
10+
apns-mock:
11+
image: mobify/apns-http2-mock-server
12+
container_name: apns-mock
13+
ports:
14+
- "2197:2197"

test/docker/docker-compose.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/docker/integration.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)