Skip to content

Commit

Permalink
MF-655 Proper usage of docker volumes (absmach#657)
Browse files Browse the repository at this point in the history
* added filter and a switch for volume deletion

Signed-off-by: Jovan Kostovski <chombium@gmail.com>

* added persistent volume configuration

Signed-off-by: Jovan Kostovski <chombium@gmail.com>
  • Loading branch information
chombium authored and davide83 committed May 13, 2019
1 parent dfaf5e1 commit 771739e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 44 deletions.
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,25 @@ clean:
rm -rf ${BUILD_DIR}
rm -rf mqtt/node_modules

cleandocker: cleanghost
cleandocker:
# Stop all containers (if running)
docker-compose -f docker/docker-compose.yml stop
# Remove mainflux containers
docker ps -f name=mainflux -aq | xargs -r docker rm
# Remove old mainflux images
docker images -q mainflux\/* | xargs -r docker rmi

# Clean ghost docker images
cleanghost:
# Remove exited containers
docker ps -f status=dead -f status=exited -aq | xargs -r docker rm -v
docker ps -f name=mainflux -f status=dead -f status=exited -aq | xargs -r docker rm -v

# Remove unused images
docker images -f dangling=true -q | xargs -r docker rmi
# Remove unused volumes
docker volume ls -f dangling=true -q | xargs -r docker volume rm
docker images "mainflux\/*" -f dangling=true -q | xargs -r docker rmi

# Remove old mainflux images
docker images -q mainflux\/* | xargs -r docker rmi

ifndef spv
# Remove unused volumes
docker volume ls -f name=mainflux -f dangling=true -q | xargs -r docker volume rm
endif
install:
cp ${BUILD_DIR}/* $(GOBIN)

Expand Down
5 changes: 5 additions & 0 deletions docker/addons/bootstrap/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ networks:
docker_mainflux-base-net:
external: true

volumes:
mainflux-bootstrap-db-volume:

services:
bootstrap-db:
image: postgres:10.2-alpine
Expand All @@ -15,6 +18,8 @@ services:
POSTGRES_DB: bootstrap
networks:
- docker_mainflux-base-net
volumes:
- mainflux-bootstrap-db-volume:/var/lib/postgresql/data

bootstrap:
image: mainflux/bootstrap:latest
Expand Down
5 changes: 5 additions & 0 deletions docker/addons/cassandra-writer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ networks:
docker_mainflux-base-net:
external: true

volumes:
mainflux-cassandra-volume:

services:
cassandra:
image: cassandra:3.11.3
Expand All @@ -21,6 +24,8 @@ services:
- docker_mainflux-base-net
ports:
- 9042:9042
volumes:
- mainflux-cassandra-volume:/var/lib/cassandra

cassandra-writer:
image: mainflux/cassandra-writer:latest
Expand Down
8 changes: 8 additions & 0 deletions docker/addons/influxdb-writer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ networks:
docker_mainflux-base-net:
external: true

volumes:
mainflux-influxdb-volume:
mainflux-grafana-volume:

services:

influxdb:
Expand All @@ -25,6 +29,8 @@ services:
- docker_mainflux-base-net
ports:
- 8086:8086
volumes:
- mainflux-influxdb-volume:/var/lib/influxdb

influxdb-writer:
image: mainflux/influxdb-writer:latest
Expand Down Expand Up @@ -58,3 +64,5 @@ services:
- 3001:3000
networks:
- docker_mainflux-base-net
volumes:
- mainflux-grafana-volume:/var/lib/grafana
7 changes: 7 additions & 0 deletions docker/addons/mongodb-writer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ networks:
docker_mainflux-base-net:
external: true

volumes:
mainflux-mongodb-db-volume:
mainflux-mongodb-configdb-volume:

services:
mongodb:
image: mongo:3.6-jessie
Expand All @@ -24,6 +28,9 @@ services:
- 27017:27017
networks:
- docker_mainflux-base-net
volumes:
- mainflux-mongodb-db-volume:/data/db
- mainflux-mongodb-configdb-volume:/data/configdb

mongodb-writer:
image: mainflux/mongodb-writer:latest
Expand Down
17 changes: 17 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ networks:
mainflux-base-net:
driver: bridge

volumes:
mainflux-users-db-volume:
mainflux-things-db-volume:
mainflux-mqtt-redis-volume:
mainflux-things-redis-volume:
mainflux-es-redis-volume:

services:
nginx:
image: nginx:1.14.2
Expand Down Expand Up @@ -48,6 +55,8 @@ services:
POSTGRES_DB: users
networks:
- mainflux-base-net
volumes:
- mainflux-users-db-volume:/var/lib/postgresql/data

users:
image: mainflux/users:latest
Expand Down Expand Up @@ -82,13 +91,17 @@ services:
POSTGRES_DB: things
networks:
- mainflux-base-net
volumes:
- mainflux-things-db-volume:/var/lib/postgresql/data

things-redis:
image: redis:5.0-alpine
container_name: mainflux-things-redis
restart: on-failure
networks:
- mainflux-base-net
volumes:
- mainflux-things-redis-volume:/data

things:
image: mainflux/things:latest
Expand Down Expand Up @@ -184,13 +197,17 @@ services:
restart: on-failure
networks:
- mainflux-base-net
volumes:
- mainflux-es-redis-volume:/data

mqtt-redis:
image: redis:5.0-alpine
container_name: mainflux-mqtt-redis
restart: on-failure
networks:
- mainflux-base-net
volumes:
- mainflux-mqtt-redis-volume:/data

mqtt-adapter:
image: mainflux/mqtt:latest
Expand Down
48 changes: 13 additions & 35 deletions docs/dev-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ make docker_http

> N.B. Mainflux creates `FROM scratch` docker containers which are compact and small in size.
> N.B. The `things-db` and `users-db` containers are built from a vanilla PostgreSQL docker image downloaded from docker hub which does not persist the data when these containers are rebuilt. Thus, __rebuilding of all docker containers with `make dockers` or rebuilding the `things-db` and `users-db` containers separately with `make docker_things-db` and `make docker_users-db` respectively, will cause data loss. All your users, things, channels and connections between them will be lost!__ As we use this setup only for development, we don't guarantee any permanent data persistence. If you need to retain the data between the container rebuilds you can attach volume to the `things-db` and `users-db` containers. Check the official docs on how to use volumes [here](https://docs.docker.com/storage/volumes/) and [here](https://docs.docker.com/compose/compose-file/#volumes). For examples on how to add persistent volumes check the [Overriding the default docker-compose configuration](#overriding-the-default-docker-compose-configuration) section.
> N.B. The `things-db` and `users-db` containers are built from a vanilla PostgreSQL docker image downloaded from docker hub which does not persist the data when these containers are rebuilt. Thus, __rebuilding of all docker containers with `make dockers` or rebuilding the `things-db` and `users-db` containers separately with `make docker_things-db` and `make docker_users-db` respectively, will cause data loss. All your users, things, channels and connections between them will be lost!__ As we use this setup only for development, we don't guarantee any permanent data persistence. If you want to update your Mainflux dockerized installation and want to keep your data, use `make spv=true cleandocker` to clean the containers and images and keep the data and then `make run` to update the images and the containers. Check the [Cleaning up your dockerized Mainflux setup](#cleaning-up-your-dockerized-mainflux-setup) section for details. Please note that this kind of updating might not work if there are database changes.

#### Building Docker images for development

Expand All @@ -85,56 +85,34 @@ Commands `make dockers` and `make dockers_dev` are similar. The main difference

### Overriding the default docker-compose configuration
Sometimes, depending on the use case and the user's needs it might be useful to override or add some extra parameters to the docker-compose configuration. These configuration changes can be done by specifying multiple compose files with the [docker-compose command line option -f](https://docs.docker.com/compose/reference/overview/) as described [here](https://docs.docker.com/compose/extends/).
The following format of the `docker-compose` can be used to extend or override the configuration:
The following format of the `docker-compose` command can be used to extend or override the configuration:
```
docker-compose -f docker/docker-compose.yml -f docker/docker-compose.custom1.yml -f docker/docker-compose.custom2.yml up [-d]
```
In the command above each successive file overrides the previous parameters.

A practical example in our case would be to add persistent volumes to the users-db, things-db, influxdb and grafana containers so that we don't loose data between updates of Mainflux. The volumes are mapped to the default location on the host machine, something like `/var/lib/docker/volumes/project-name_volume-name`:
A practical example in our case would be to enable debugging and tracing in NATS so that we can see better how are the messages moving around.

`docker-compose.nats-debugging.yml`
```yaml
# docker/docker-compose.persistence.yml
version: "3"

volumes:
mainflux-users-db-volume:
mainflux-things-db-volume:

services:
users-db:
volumes:
- mainflux-users-db-volume:/var/lib/postgresql/data

things-db:
volumes:
- mainflux-things-db-volume:/var/lib/postgresql/data
nats:
command: --debug -DV
```
```yaml
# docker/addons/influxdb-writer/docker-compose.persistence.yml
version: "3"

volumes:
influxdb-volume:
grafana-volume:

services:
influxdb:
volumes:
- influxdb-volume:/var/lib/influxdb

grafana:
volumes:
- grafana-volume:/var/lib/grafana
```
When we have the override files in place, to compose the whole infrastructure including the persistent volumes we can execute:
```
docker-compose -f docker/docker-compose.yml -f docker/docker-compose.persistence.yml up -d
docker-compose -f docker/addons/influxdb-writer/docker-compose.yml -f docker/addons/influxdb-writer/docker-compose.persistence.yml up -d
docker-compose -f docker/docker-compose.yml -f docker/docker-compose.nats-debugging.yml up -d
```

__Note:__ Please store your customizations to some folder outside the Mainflux's source folder and maybe add them to some other git repository. You can always apply your customizations by pointing to the right file using `docker-compose -f ...`. Also be sure not to use the `make cleandocker` and `make cleanghost` as they might delete something which you don't want to delete (i.e. the newly created persistent volumes).
__Note:__ Please store your customizations to some folder outside the Mainflux's source folder and maybe add them to some other git repository. You can always apply your customizations by pointing to the right file using `docker-compose -f ...`.


### Cleaning up your dockerized Mainflux setup
If you want to clean your whole dockerized Mainflux installation you can use the `make cleandocker` command. Please note that __by default the `make cleandocker` command will stop and delete all of the containers, images and persistent volumes__. If you want to keep gathered data in the system (the persistent volumes) please use the following command `make spv=true cleandocker` (spv = skip persistent volumes). This form of the command will stop and delete the containers and images, but won't delete the persistent volumes.


### MQTT Microservice
The MQTT Microservice in Mainflux is special, as it is currently the only microservice written in NodeJS. It is not compiled, but node modules need to be downloaded in order to start the service:
Expand Down

0 comments on commit 771739e

Please sign in to comment.