Skip to content

Commit 09b924f

Browse files
author
Paolo A. Spada
committed
Initial commit
0 parents  commit 09b924f

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/deps/

Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM clojure
2+
3+
ARG SRC=./deps/github.com/Commonfare-net/social-wallet-api
4+
5+
RUN mkdir -p /usr/src/app
6+
WORKDIR /usr/src/app
7+
COPY ${SRC} /usr/src/app
8+
RUN lein deps
9+
COPY . /usr/src/app
10+
# CMD ["lein", "ring", "server-headless"]
11+
ENTRYPOINT [ "lein", "ring", "server-headless" ]

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.PHONY: clean fetch docker/build docker/push
2+
3+
REPO=github.com/Commonfare-net/social-wallet-api
4+
DEPS=./deps
5+
SWAPISRC=${DEPS}/${REPO}
6+
IMAGE="commonfare/social-wallet-api"
7+
8+
$(shell [ -d ${SWAPISRC} ] && true || git clone https://${REPO} ${SWAPISRC})
9+
10+
TAG ?= $(shell cd ${SWAPISRC} && git describe --tag)
11+
VERSION ?= $(shell echo ${TAG} | cut -d'-' -f 1)
12+
13+
clean:
14+
rm -rf ${DEPS}/*
15+
16+
fetch:
17+
cd ${SWAPISRC} && git checkout master && git reset --hard && git pull && git checkout master && git checkout ${VERSION}
18+
19+
# comment the line of code ':host "localhost"' in order to allow the service
20+
# to listen on different names from localhost only
21+
fix:
22+
mv ${SWAPISRC}/project.clj ${SWAPISRC}/project.clj.ORIGINAL
23+
cat ${SWAPISRC}/project.clj.ORIGINAL | sed 's/\(:host "localhost"\)/;;\1/' > ${SWAPISRC}/project.clj
24+
25+
docker/build: fetch fix
26+
docker build . -t ${IMAGE}:${VERSION}
27+
docker tag ${IMAGE}:${VERSION} ${IMAGE}:latest
28+
29+
docker/push: docker/build
30+
docker push ${IMAGE}:${VERSION}
31+
docker push ${IMAGE}:latest
32+
33+
34+
all: fetch docker/build

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Dockerize Commonfare Social Wallet API
2+
The purpose of this project is to create a docker image of the
3+
Commonfare Social Wallet API service: https://github.com/Commonfare-net/social-wallet-api
4+
5+
## How it works
6+
At a glance this is very simple and consists of the follwing steps:
7+
1. Clone the source repository of Commonfare Social Wallet API (a.k.a SWAPI)
8+
2. Build a dedicated image through the Dockerfile for running SWAPI service which includes all the required Clojure/Lein/Java dependencies
9+
3. Push such image to Docker Hub
10+
11+
## How to use it
12+
There is a `Makefile` that should help out when building a new image.
13+
14+
### Build target
15+
Command `make docker/build` should build the image according to the provided `Dockerfile` and
16+
generate the final docker image named _commonfare/social-wallet-api_.
17+
The image is also tagged with the git tag of the original repository:
18+
```
19+
$ docker images
20+
REPOSITORY TAG IMAGE ID CREATED SIZE
21+
commonfare/social-wallet-api latest c179ea2bd09c 8 minutes ago 767MB
22+
commonfare/social-wallet-api v0.9.3 c179ea2bd09c 8 minutes ago 767MB
23+
```
24+
### Push target
25+
Command `make docker/push` will push the image to Docker Hub and make it publicly available
26+
27+
### Fix target
28+
The `fix` target is a temporary solution that is in charge to comment out a line in the source
29+
code of the Social Wallet API before building the image.
30+
Such line is in the project file `project.clj` and instructs the ring web server to listen only
31+
on the `localhost` hostname.
32+
33+
```
34+
:ring {:init social-wallet-api.handler/init
35+
:handler social-wallet-api.handler/app
36+
;; Accessible only from localhost
37+
;; https://stackoverflow.com/questions/24467539/lein-ring-server-headless-only-listen-to-localhost
38+
:host "localhost"
39+
:destroy social-wallet-api.handler/destroy
40+
:reload-paths ["src"]}
41+
```
42+
43+
If we leave such line there we will need to use docker `host` network in order to reach the
44+
service through (for example) `localhost:3000`.
45+
As we prefer not to get container services directly using the host netwroking rather relying on
46+
their private container network we'd like to avoid it. But to do so and still be able to reach
47+
the container after some port mapping such as `<host port>:<container port>`, we need to allow
48+
the services to listen on other hostnames too.
49+
50+
So after running the `fix` target we will get the following ad are good to go with buidling the image.
51+
52+
```
53+
:ring {:init social-wallet-api.handler/init
54+
:handler social-wallet-api.handler/app
55+
;; Accessible only from localhost
56+
;; https://stackoverflow.com/questions/24467539/lein-ring-server-headless-only-listen-to-localhost
57+
;;:host "localhost"
58+
:destroy social-wallet-api.handler/destroy
59+
:reload-paths ["src"]}
60+
```

0 commit comments

Comments
 (0)