Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dockerfile and compose #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions DOCKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Docker readme

## Get the source code

Get the code and checkout the branch

```sh
git clone https://github.com/rastaman/bon-appetit-server.git
git checkout docker_integration
```

## Build and run

```sh
$ docker-compose up
...
```

In another shell:

```sh
$ curl -s -H 'Content-Type: application/json' http://127.0.0.1:3001/bon-appetit/api/v1/restaurant | jq .
{
"restaurants": []
}
```

### Using the real ip

Either find it or you can try this:

```sh
$ export IPV4=$(ifconfig | grep "inet " | grep -v 127 | cut -d " " -f 2 | head -1)
...
$ curl -s -H 'Content-Type: application/json' http://${IPV4}:3001/bon-appetit/api/v1/restaurant | jq .
{
"restaurants": []
}
```

## Feeding datas

```sh
$ for i in restaurant review event; do
curl -i -H 'Content-Type: application/json' -X POST -d @src/json-models/${i}s.json http://${IPV4}:3001/bon-appetit/api/v1/${i}/batch
done
HTTP/1.1 100 Continue

HTTP/1.1 201 Created
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Content-Type: application/json; charset=utf-8
Content-Length: 46
ETag: W/"2e-+BC4cVbW34Arrk0xXWN0mHD7ugM"
Date: Wed, 20 Nov 2019 19:22:51 GMT
Connection: keep-alive

{"message":"Restaurant created with Success!"}HTTP/1.1 100 Continue

HTTP/1.1 201 Created
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Content-Type: application/json; charset=utf-8
Content-Length: 42
ETag: W/"2a-xDE1zrIrgK8aQf9y+iu4dwQByb4"
Date: Wed, 20 Nov 2019 19:22:52 GMT
Connection: keep-alive

{"message":"Review created with Success!"}HTTP/1.1 100 Continue

HTTP/1.1 201 Created
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Content-Type: application/json; charset=utf-8
Content-Length: 42
ETag: W/"2a-Mfe60Vp8r62DX2PlSD3YLDPmzQM"
Date: Wed, 20 Nov 2019 19:22:52 GMT
Connection: keep-alive

{"message":"Events created with Success!"}
$ export DISHES=$(find src/json-models/dishes -name "*.json" | gpaste -d" " -s)
...
for d in $DISHES; do
curl -i -H 'Content-Type: application/json' -X POST -d @${d} http://${IPV4}:3001/bon-appetit/api/v1/dish/batch
done
```

### Checking that dishes exists

```sh
$ curl -s -H 'Content-Type: application/json' http://${IPV4}:3001/bon-appetit/api/v1/dish | jq . | head -20
{
"dishes": [
{
"ingredients": [
"200g risotto rice",
"1 large garlic clove",
"2 spring onions",
"900ml low-salt chicken stock",
"120g frozen peas",
"1 large courgette",
"50g grated medium",
"140g cooked prawns"
],
"imageURL": "https://s3-sa-east-1.amazonaws.com/bon-appetit-resources/dishes/homemade/large/toddler-recipe-microwave-courgette-and-pea-risotto-prawns.jpeg",
"mediumImageURL": "https://s3-sa-east-1.amazonaws.com/bon-appetit-resources/dishes/homemade/medium/toddler-recipe-microwave-courgette-and-pea-risotto-prawns.jpeg",
"thumbnailImageURL": "https://s3-sa-east-1.amazonaws.com/bon-appetit-resources/dishes/homemade/thumbnail/toddler-recipe-microwave-courgette-and-pea-risotto-prawns.jpeg",
"title": "Microwave courgette and pea risotto with prawns",
"description": "If you're after a family-friendly meal that takes under 30 minutes, try this courgette and pea risotto.",
"type": "Homemade",
"stars": 4,
...
```

### Rebuilding the backend

```sh
docker-compose build backend
docker-compose up
```

## References

- [Managing MongoDB on docker with docker-compose - Faun - Medium](https://medium.com/faun/managing-mongodb-on-docker-with-docker-compose-26bf8a0bbae3)
- [Use multi-stage builds | Docker Documentation](https://docs.docker.com/develop/develop-images/multistage-build/)
- [Dockerizing a Node.js web app | Node.js](https://nodejs.org/de/docs/guides/nodejs-docker-webapp/)
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM node:10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3001

CMD [ "node", "./node_modules/.bin/pm2-runtime", "./src/bin" ]

# FROM go
# WORKDIR /go/src/github.com/alexellis/href-counter/
# RUN go get -d -v golang.org/x/net/html
# COPY app.go .
# RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

# FROM alpine:latest
# RUN apk --no-cache add ca-certificates
# WORKDIR /root/
# COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
# CMD ["./app"]
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'
services:
datastore:
image: "mongo:latest"
backend:
build: .
environment:
DATABASE_URL: mongodb://datastore:27017/bon-appetit
PORT: "3001"
NODE_ENV: production
ports:
- "3001:3001"
11 changes: 7 additions & 4 deletions src/controllers/dish-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ exports.createInBatch = async (req, res, next) => {
try {
await DishDAO.createInBatch(req.body);

return res.status(201);
return res.status(201).json({
message: "Dish created with Success!"
});
} catch (error) {
return res.status(500).send({
error
debug(error);

return res.status(500).json({
message: "Error when trying to Create Dishes."
});
}
};

exports.readAll = async (req, res, next) => {
Expand Down
11 changes: 8 additions & 3 deletions src/controllers/review-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ exports.createInBatch = async (req, res, next) => {
try {
await ReviewDAO.createInBatch(req.body);

return res.status(201);
return res.status(201).json({
message: "Review created with Success!"
});
} catch (error) {
return res.status(500).send({
error
debug(error);

return res.status(500).json({
message: "Error when trying to Create Reviews."
});

}
};

Expand Down