Compare commits

...

10 commits

Author SHA1 Message Date
5c332ebadc
captain 2024-05-10 08:45:54 +10:00
nero120
2ee8777459 Added upgrade info to README.
Updated mongodb container in docker-compose.yml to include db env vars.
Added backup volume to mongodb container in docker-compose.yml.
Updated env variable names.
2021-04-03 01:52:00 +01:00
nero120
024b624d9e Updated target api version to 1.1.13.
Replaced traefik with Caddy as reverse proxy.
Added health check for api in docker compose (resolves #4).
Updated node to v14 and other dependencies.
2021-04-02 01:27:43 +01:00
nero120
76d2d1e799 Updated target api version to 1.1.12 and latest dependencies. 2020-04-29 14:53:47 +01:00
nero120
da3bed3630 Updated target api version to 1.1.11 and latest dependencies. 2020-01-21 05:25:48 +00:00
nero120
821f82a2b3 Updated target api version to 1.1.10 and latest dependencies. 2019-10-08 22:00:31 +01:00
Rich
eccab5084f
Update README.md 2019-06-28 11:27:10 +01:00
nero120
fac91dced7 Updated docker files to use API v1.1.9 and latest dependencies. 2019-06-24 10:55:40 +01:00
nero120
6554db9377 Updated target api version to 1.1.8. 2019-01-07 11:55:50 +00:00
nero120
fe848488f0 Added dockerfile and updated target api version to 1.1.7. 2019-01-01 14:51:45 +00:00
9 changed files with 169 additions and 79 deletions

7
.env
View file

@ -1,4 +1,5 @@
API_HOSTNAME=xbsapi.yourdomain.org
DB_NAME=xbrowsersync
DB_PASSWORD=xbsdbpass
DB_USERNAME=xbsdb
COMPOSE_CONVERT_WINDOWS_PATHS=1
XBS_API_HOSTNAME=xbsapi.yourdomain.org
XBS_DB_PASSWORD=xbsdbpass
XBS_DB_USERNAME=xbsdb

View file

@ -10,7 +10,7 @@ This repository contains the Docker files required to get your own [xBrowserSync
## Running the API image
Running the API image alone requires that you have a MongoDB instance and, ideally, a web server to reverse proxy the API behind.
Running the API image alone requires that you have a MongoDB instance and (ideally) a web server to reverse proxy the API behind.
1. Create a file named `settings.json` and include any required custom [settings](https://github.com/xbrowsersync/api#3-modify-configuration-settings) values, such as those for connecting to your MongoDB instance. For example, if you are running Docker and MongoDB on a windows server, you would point the service at the MongoDB instance running on the host using the following settings:
@ -30,40 +30,87 @@ 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.
## Running a production-ready service
If you do not already have a MongoDB instance or are intending to expose your xBrowserSync service over the internet then it is recommended to use the provided [`docker-compose.yml`](https://github.com/xbrowsersync/api-docker/blob/master/docker-compose.yml) which will create fully configured containers for a MongoDB database, the xBrowserSync API and a [traefik](https://traefik.io/) reverse proxy web server to run in front of the API. Traefik automatically acquires and updates SSL certificates from [Let's Encrypt](https://letsencrypt.org/) so that your xBrowserSync API service will run securely over HTTPS.
If you do not already have a MongoDB instance or are intending to expose your xBrowserSync service over the internet then it is recommended to use the provided [`docker-compose.yml`](https://github.com/xbrowsersync/api-docker/blob/master/docker-compose.yml) which will create fully configured containers for a MongoDB database, the xBrowserSync API and a [Caddy](https://caddyserver.com/) web server as a reverse proxy in front of the API. Caddy automatically acquires and updates SSL certificates so that your xBrowserSync API service will run securely over HTTPS.
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. Secure the [`acme.json`](https://github.com/xbrowsersync/api-docker/blob/master/acme.json) file as per traefik's requirements:
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:
```
$ sudo chmod 600 acme.json
docker-compose up -d
```
3. 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.
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.
4. Open the [`traefik.toml`](https://github.com/xbrowsersync/api-docker/blob/master/traefik.toml) file in a text editor and update the `email` value on [line 18](https://github.com/xbrowsersync/api-docker/blob/master/traefik.toml#L18) to your own email address in order to successfully acquire an SSL certificate from [Let's Encrypt](https://letsencrypt.org/).
## Upgrade process
5. (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.
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.
6. Run the following command to start the containers:
The following steps detail the process in full:
1. Connect to the MongoDB container:
```
$ sudo docker-compose up -d
docker exec -it xbs-db bash
```
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.
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

View file

4
captain-definition Normal file
View file

@ -0,0 +1,4 @@
{
"schemaVersion": 2,
"dockerfilePath": "./dockerfile"
}

View file

@ -1,58 +1,55 @@
version: "3.7"
volumes:
xbs-db-data:
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"
image: "mongo:4.0.4"
labels:
- "traefik.enable=false"
networks:
- "xbs-net"
restart: "always"
- "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"
image: "xbrowsersync/api:1.1.6"
labels:
- "traefik.frontend.rule=Host:$XBS_API_HOSTNAME"
- "traefik.port=8080"
networks:
- "xbs-net"
restart: "always"
- "XBROWSERSYNC_DB_PWD=$DB_PASSWORD"
- "XBROWSERSYNC_DB_USER=$DB_USERNAME"
healthcheck:
test: [ "CMD", "node", "/usr/src/api/healthcheck.js" ]
interval: "1m"
timeout: "10s"
retries: "5"
start_period: "30s"
image: "xbrowsersync/api:1.1.13"
restart: "unless-stopped"
volumes:
- "./settings.json:/usr/src/api/config/settings.json"
- "./healthcheck.js:/usr/src/api/healthcheck.js"
reverse-proxy:
command: "--api --docker"
command: "caddy reverse-proxy --from $API_HOSTNAME --to api:8080"
container_name: "xbs-reverse-proxy"
depends_on:
- "api"
image: traefik:1.7.5-alpine
labels:
- "traefik.enable=false"
networks:
- "xbs-net"
image: "caddy:2.3.0-alpine"
ports:
- "443:443"
- "80:80"
restart: "always"
restart: "unless-stopped"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "./acme.json:/acme.json"
- "./traefik.toml:/traefik.toml"
- "xbs-caddy-config:/config"
- "xbs-caddy-data:/data"
networks:
xbs-net:
volumes:
xbs-caddy-config:
xbs-caddy-data:
xbs-db-backups:
xbs-db-data:

20
dockerfile Normal file
View file

@ -0,0 +1,20 @@
FROM node:14.16.0-alpine
# Set environment variables
ENV XBROWSERSYNC_API_VERSION 1.1.13
WORKDIR /usr/src/api
# Download release and unpack
RUN wget -q -O release.tar.gz https://github.com/xBrowserSync/api/archive/v$XBROWSERSYNC_API_VERSION.tar.gz \
&& tar -C . -xzf release.tar.gz \
&& rm release.tar.gz \
&& mv api-$XBROWSERSYNC_API_VERSION/* . \
&& rm -rf api-$XBROWSERSYNC_API_VERSION/
# Install dependencies
RUN npm install --only=production
# Expose port and start api
EXPOSE 8080
CMD [ "node", "dist/api.js"]

44
healthcheck.js Normal file
View file

@ -0,0 +1,44 @@
const http = require('http');
const response = http.request(
{
host: '0.0.0.0',
method: 'GET',
path: '/info',
port: 8080,
timeout: 2000,
},
(res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
const payload = JSON.parse(body);
switch (payload.status) {
case 1:
case 3:
console.log('HEALTHCHECK: online');
process.exit(0);
case 2:
default:
console.log('HEALTHCHECK: offline');
}
} else {
console.log('HEALTHCHECK: offline');
}
process.exit(1);
});
}
);
response.on('error', function (err) {
console.log('HEALTHCHECK: offline');
process.exit(1);
});
response.end();

View file

@ -1,5 +1,5 @@
{
"db": {
"host": "db"
"host": "srv-captain--mongodb"
}
}

View file

@ -1,23 +0,0 @@
defaultEntryPoints = ["http", "https"]
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[retry]
[api]
dashboard = false
[acme]
email = "you@yourdomain.org"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"