diff --git a/.env b/.env index 39f3876..89b8f5b 100644 --- a/.env +++ b/.env @@ -1,4 +1,5 @@ -COMPOSE_CONVERT_WINDOWS_PATHS=1 -XBS_API_HOSTNAME=xbsapi.yourdomain.org -XBS_DB_PASSWORD=xbsdbpass -XBS_DB_USERNAME=xbsdb \ No newline at end of file +API_HOSTNAME=xbsapi.yourdomain.org +DB_NAME=xbrowsersync +DB_PASSWORD=xbsdbpass +DB_USERNAME=xbsdb +COMPOSE_CONVERT_WINDOWS_PATHS=1 \ No newline at end of file diff --git a/README.md b/README.md index 14b4a79..3c3531a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Running the API image alone requires that you have a MongoDB instance and (ideal 3. Run the following command to start an API container, providing the actual path to the `settings.json` file created in step 1. The service will be exposed via port 8080. If you did not create the environment variables in step 2, you will need to provide the actual username and password values in line, i.e. `-e XBROWSERSYNC_DB_USER=YourDatabaseUsername -e XBROWSERSYNC_DB_PWD=YourDatabasePassword`: ``` - $ sudo docker run --name xbs-api -p 8080:8080 -e XBROWSERSYNC_DB_USER -e XBROWSERSYNC_DB_PWD -v /path/to/settings.json:/usr/src/api/config/settings.json -d xbrowsersync/api + docker run --name xbs-api -p 8080:8080 -e XBROWSERSYNC_DB_USER -e XBROWSERSYNC_DB_PWD -v /path/to/settings.json:/usr/src/api/config/settings.json -d xbrowsersync/api ``` You can now access your xBrowserSync API service at http://127.0.0.1:8080. @@ -42,20 +42,75 @@ If you do not already have a MongoDB instance or are intending to expose your xB 1. Clone the [api-docker](https://github.com/xbrowsersync/api-docker/) GitHub repo: ``` - $ git clone https://github.com/xbrowsersync/api-docker.git + git clone https://github.com/xbrowsersync/api-docker.git ``` - 2. Open the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file in a text editor and update the `XBS_API_HOSTNAME` value to correspond to the host name that the API service will be exposed over (ensure you have configured your DNS provider to point the desired host name to your host's IP address). Also, change the `XBS_DB_USERNAME` and `XBS_DB_PASSWORD` values to any of your choosing. + 2. Open the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file in a text editor and update the `API_HOSTNAME` value to correspond to the host name that the API service will be exposed over (ensure you have configured your DNS provider to point the desired host name to your host's IP address). Also, change the `DB_USERNAME` and `DB_PASSWORD` values to any of your choosing. 3. (Optionally) open the [`settings.json`](https://github.com/xbrowsersync/api-docker/blob/master/settings.json) file and include any custom [settings](https://github.com/xbrowsersync/api#3-modify-configuration-settings) values you wish to run on your service. Important: do not change the `db.host` value. 4. Run the following command to start the containers: ``` - $ docker-compose up -d + docker-compose up -d ``` - You can now access your xBrowserSync API service over HTTPS at the value of `XBS_API_HOSTNAME` defined in the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file. + You can now access your xBrowserSync API service over HTTPS at the value of `API_HOSTNAME` defined in the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file. + +## Upgrade process + +If you wish to upgrade your existing production-ready service created using the [`docker-compose.yml`](https://github.com/xbrowsersync/api-docker/blob/master/docker-compose.yml) file, you may encounter database errors when upgrading the MongoDB version. To avoid these issues, it is recommended that you export the db data from the old version and restore to the new version once up and running. + +The following steps detail the process in full: + + + 1. Connect to the MongoDB container: + + ``` + docker exec -it xbs-db bash + ``` + + 2. Export db data to the `xbs-db-backups` Docker volume using [mongodump](https://docs.mongodb.com/database-tools/mongodump/): + + ``` + mongodump --db $XBS_DB_NAME --host localhost --port 27017 --username $XBS_DB_USERNAME --password $XBS_DB_PASSWORD --authenticationDatabase admin --archive=/data/backups/xbs-db-`date +"%Y-%m-%d"`.gz --gzip + ``` + + 3. Exit the MongoDB container and navigate to the api-docker GitHub repo folder. Stop and remove all containers: + + ``` + docker-compose down + ``` + + 4. Delete the `xbs-db-data` Docker volume before upgrading MongoDB. You need to find the volume name first using: + + ``` + docker volume ls + ``` + + It will be something like `api-docker_xbs-db-data`. Once located, delete it with: + + ``` + docker volume rm api-docker_xbs-db-data + ``` + + 5. Get latest changes and start the containers: + + ``` + git pull + docker-compose up -d + ``` + 6. Connect to the MongoDB container: + + ``` + docker exec -it xbs-db bash + ``` + + 7. Restore db data from the `xbs-db-backups` Docker volume using [mongorestore](https://docs.mongodb.com/database-tools/mongorestore/) (replace `{BackupFileName}` with the actual backup file name): + + ``` + mongorestore --username $XBS_DB_USERNAME --password $XBS_DB_PASSWORD --authenticationDatabase admin --verbose=5 --gzip --drop --archive=/data/backups/{BackupFileName} + ``` ## Issues and feature requests diff --git a/docker-compose.yml b/docker-compose.yml index 8a8e45c..21d837e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,21 +4,25 @@ services: db: container_name: "xbs-db" environment: - - "MONGO_INITDB_DATABASE=xbrowsersync" - - "MONGO_INITDB_ROOT_PASSWORD=$XBS_DB_PASSWORD" - - "MONGO_INITDB_ROOT_USERNAME=$XBS_DB_USERNAME" + - "MONGO_INITDB_DATABASE=$DB_NAME" + - "MONGO_INITDB_ROOT_PASSWORD=$DB_PASSWORD" + - "MONGO_INITDB_ROOT_USERNAME=$DB_USERNAME" + - "XBS_DB_NAME=$DB_NAME" + - "XBS_DB_PASSWORD=$DB_PASSWORD" + - "XBS_DB_USERNAME=$DB_USERNAME" image: "mongo:4.4.4" restart: "unless-stopped" volumes: - "xbs-db-data:/data/db" + - "xbs-db-backups:/data/backups" - "./mongoconfig.js:/docker-entrypoint-initdb.d/mongoconfig.js" api: container_name: "xbs-api" depends_on: - "db" environment: - - "XBROWSERSYNC_DB_PWD=$XBS_DB_PASSWORD" - - "XBROWSERSYNC_DB_USER=$XBS_DB_USERNAME" + - "XBROWSERSYNC_DB_PWD=$DB_PASSWORD" + - "XBROWSERSYNC_DB_USER=$DB_USERNAME" healthcheck: test: [ "CMD", "node", "/usr/src/api/healthcheck.js" ] interval: "1m" @@ -31,7 +35,7 @@ services: - "./settings.json:/usr/src/api/config/settings.json" - "./healthcheck.js:/usr/src/api/healthcheck.js" reverse-proxy: - command: "caddy reverse-proxy --from $XBS_API_HOSTNAME --to api:8080" + command: "caddy reverse-proxy --from $API_HOSTNAME --to api:8080" container_name: "xbs-reverse-proxy" depends_on: - "api" @@ -47,4 +51,5 @@ services: volumes: xbs-caddy-config: xbs-caddy-data: + xbs-db-backups: xbs-db-data: