-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started Docker
-
First of all, we are using a docker version of our application because, it is very easy to scale and we can create highly available systems with Docker.
-
In this, we will be showing you how to create two containers of the auction-away and one container of mongodb and one container of nginx.
-
nginx container will act as reverse proxy so that, we can send traffic to both of our auction-away containers using round robin algorithm.
-
mongodb container will have a persisted storage on the container host so that, when and if a mongodb container goes down, we will have a backup and also, when spinning up new container of mongodb it will use the persisted data to get started.
To get started,
-
Make sure you have docker installed on your system.
-
If you don't have docker installed on your system, you can install it by just heading over to Docker Install.
-
That's all the system setup required :)
-
Go to Auction Away Releases and choose the docker release you want and copy the address to the tar file.
-
Assume the link looks something like this
https://github.com/s-xync/auction-away/releases/tag/v1.2.0-auction-away-docker
-
Download the file using
wget https://github.com/s-xync/auction-away/releases/tag/v1.2.0-auction-away-docker
-
Now open a terminal and unzip the file using
tar xvzf v1.2.0-auction-away-docker.tar.gz
-
Open a terminal and navigate to the project folder.
-
First run
docker build -t auction-away -f AuctionAwayDockerfile .
which builds the docker image and tags it with a name(dot at the end of command is necessary). -
Run
docker run --name mongodb1 -p 27018:27017 -v data:/data/db -d mongo:latest
to create and start a mongodb container. Here, the container uses data folder of the host system for persistance. So that, when you stop mongodb container and create another one, it uses this data folder to get the data written by previous mongodb container. The container runs with the name mongodb1 and maps to the host port 27018 which can be anything else. -
Run
docker run --name auction-away1 -p 3001:3000 -d --link=mongodb1:mongodb auction-away
anddocker run --name auction-away2 -p 3002:3000 -d --link=mongodb1:mongodb auction-away
which creates and runs two containers of our app with names auction-away1, auction-away2 mapping to host ports 3001, 3002 which can be anything else. The important thing is that they are linked to conatainer named mongodb1 as mongodb which is important so that our app can recognize the host and port of the mongodb server in app.js file. -
Now, you can visit
localhost:3001
andlocalhost:3002
to check if everything is working. We need to unite these both urls using reverse proxy and that is where nginx comes in. -
Run
docker build -t auction-away-nginx -f NginxDockerfile .
which builds the docker image and tags it with a name(dot at the end of command is necessary). -
As you can see, we did not need any configuration changes in mongodb and so, we did not create a local docker image. Instead, we just used what is already there. But nginx needed some configuration and so, we have to build new image with our configurations added which are specified in nginx.conf.
-
Finally, run
docker run --name nginx1 -p 3000:80 -d auction-away-nginx
which creates and runs a nginx container with name nginx1 mapped to host port 3000. -
Head on over to localhost:3000 or any other host:port and check if everything you have done has been useful :)
You can scale up containers of our app horizontally or vertically with this. The persisted storage lets only one mongodb container access it and so, you would have to search other avenues to scale database and if I have time, I will do it.