Docker Maintainaince
Prerequisite Knowledge
If you are familier with using linux terminal commands that would be sufficient.
Why this topic ?
In this tutorial we will study the common maintainaince procedures you need to follow in order to maintain your server installed using Docker.
Topics Covered
Upgrading / Updating your Backend Instance Enable Remote access to your Database Run multiple Instances on One Server Backup and Restore your Instance Data
Upgrading / Updating your Backend Instance
Every Software needs to be updated. And you will need to update your server too. In this tutorial we will see how to update your server instance installed using docker.
Please note when you upgrade / update your docker container all your data will be deleted except for your data stored in ~/nearbyshops/data folder and data stored in PostgreSQL database. So before updating your server please ensure that you dont have any important data stored outside of these folders.
To update your docker container use the following commands.
Commands
docker-compose down
(please ensure that you are inside the directory where docker-compose.yml resides)
docker rmi nearbyshops/nearbyshops:latest --force
docker-compose up
After you do docker-compose up it will download the new docker image and create a container out of that new image. Your data stored in ~/nearbyshops/data folder and data stored in PostgreSQL database will be preserved.
Enable Remote access to your Database
Caution for Developers
Enabling Remote access to your Postgres Database makes your database venerable to malicious attacks by hackers. Remote access should be enabled only for temporary period when its required and you should disable it as soon as your needs are solved.
We use PostgreSQL as our primary Database. Sometimes you may need / want to have direct access to your Database. This need can arise due to various reasons. You may want to make some update in the database which you are not able to do using the app. You may want to analyze the database and do some research on your data. There could be many other reasons.
To enable access to your Database of your Server follow these instructions.
Open your docker-compose.yml file using a text editor
Terminal Commands
sudo nano docker-compose.yml
Add - "5432:5432" to the ports section of docker-compose.yml file.
After making an update to your docker-compose.yml file your
docker compose file will look
something like the file given below. Please note the addition of new line in the ports section.
This tells docker to enable access to the port 5432 from the outside world. 5432 is the default port on
which the PostgreSQL Database works. And for our installation also 5432 is the port which we use for PostgreSQL Database.
Optional Commands
version: '3.6'
services:
nbs_api:
image: nearbyshops/nearbyshops
ports:
- "80:80"
- "5432:5432"
container_name: nbs_api_container
volumes:
- db_data:/var/lib/postgresql/12/main
- api_data:/root/nearbyshops/data
restart: always
entrypoint: ["/root/nbs.sh"]
Save and Exit file using Ctrl + X and then execute the following command.
Terminal Commands
docker-compose down
We need to recompose the setup in order to enable port 5432. For that purpose we need to do docker compose down and then docker-compose up
Terminal Commands
docker-compose up
(Exit using Ctrl + C or Ctrl + D)
By default PostgreSQL is configured to be accessed only internally on a local server. This is done for Security Reasons. You need to change the PostgreSQL configuration settings in order to allow the database access from a Remote Computer or Say outside world. In order to accomplish this we need to get inside our container and follow a procedure
Commands
docker-compose start
(please ensure that you are inside the directory where docker-compose.yml resides)
docker exec -it nbs_api_container bash
(Exit container using Ctrl + C or Ctrl + D)
After getting inside the Docker Container please follow the prodecure given in this guide. Enable Remote Access to PostgreSQL
Commands
(Exit container using Ctrl + C or Ctrl + D)
docker-compose restart
(please ensure that you are inside the directory where docker-compose.yml resides)
Exit container after following above procedure. And then restart the container to apply the changes.
After following the above procedure you can connect to your database using PGadmin4 database utility. Install PGAdmin4 and add a new server with host ip and database user and password given in your api_config.properties file.
Run multiple Instances on One Server
Yes you can run multiple docker instances on one server. In order to run multiple instances you need to change and assign different port for each instance. And you also need to change the container name such that each instance has different container name.
The best practice is the create a different folder for each instance where you can download and edit the docker compose file for that instance.
Commands
cd ~
mkdir instanceOne
cd instanceOne
wget https://raw.githubusercontent.com/NearbyShops/Nearby-Shops-API/master/docker-compose.yml
nano docker-compose.yml
(Update the port number and container name to ensure that they are unique)
Commands
cd ~
mkdir instanceTwo
cd instanceTwo
wget https://raw.githubusercontent.com/NearbyShops/Nearby-Shops-API/master/docker-compose.yml
nano docker-compose.yml
(Update the port number and container name to ensure that they are unique)
After updating the docker compose file for each instance. You can proceed with the instructions given in our docker quick installation guide.
Backup and Restore your Instance Data
We will use pg dump command to backup the database into a text file. We can then restore our database from that backup file.
Commands
pg_dump -C -h localhost -U nearbyshops nearbyshopsdb > backup.sql
To restore the database please create a new database with name nearbyshopsdb and user nearbyshops and then use following command.
Commands
psql -h localhost -U nearbyshops nearbyshopsdb < backup.sql
To backup the images and config simply copy the data folder from inside the container ~/nearbyshops directory.
To download these files to your local computer please create a zip of all the backup files. And then download this zip file using scp command. To create zip please copy the backup.sql and data folder into backup directory. and then
Commands
cd /root/nearbyshops
mkdir backup
cd backup
cp /root/nearbyshops/data /root/nearbyshops/backup
cp /root/nearbyshops/backup.sql /root/nearbyshops/backup
Now create zip of the backup folder using zip command.
Commands
zip -r backup.zip /root/nearbyshops/backup
Use SCP command to download the backup locally. Replace IP address with your server IP.
Commands
scp root@139.59.79.32:/root/nearbyshops/backup.zip /home/user/localfolder
Facing difficulty in understanding anything ? ... let us help you ! If you have any questions and queries do not hesitate to get in touch with us on our forum