So in my current workplace, just finished a greenfield project where we built a few application components (microsite, adaptive api, microservice, datawarehouse etc). When I started, I had to go through a somewhat ridiculous process of running a bunch of powershell scripts to setup my dev machine. Its still better than what I did in my previous company, but can’t ignore the fact that every new developers is wasting so much of their time trying to setup their machines, which ideally should be a few lines of scripts to keep things sane.
Anyway, as docker is already out there to solve all these crazy issues, I was wondering rather than just doing a hello-world lets try to docker the entire app buckets to different containers, so that anyone can just download the repo, install docker for windows and then run a single command to setup the entire app. It seems really easy to achieve but as the greenfield have a fair bit of dependencies over the legacy app (the word greenfield in that context makes no sense, right?), I have to be really carefull to handle it case by case.
So this blog I will share my experience how I have been able to containerize the app in my recently finished dev works.
I started with the adaptive api, as that’s built in node js, rather than C#, because if you use C#, either have to choose dot net core or need to use windows container. Also the adaptive api somehow lives way far from the legacy apps, it seems to be an easy one to start with.
So after a few days of struggle, finally come up with this docker file:
#BASE IMAGE FROM node:4-onbuild MAINTAINER Ankan Sircar <firstname.lastname@example.org> # INSTALL TOOLS FOR PORT TROUBLESHOOTING RUN apt-get update \ && apt-get install -y net-tools # CREATE APP DIRECTORY RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # INSTALL APP DEPENDENCIES COPY package.json /usr/src/app/ RUN npm install # BUNDLE APP SOURCE COPY . /usr/src/app EXPOSE 8089 CMD [ "npm", "start" ]
This is basically inspired from the blog : https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ . What this docker file does, is quite self explanatory. It bascially creates an app directory where all the source code been copied, then it just goes through the package installation. and finally it runs the command to start the app.
So to get this docker file going, all I need to do is to run the following commands,
//to build the image docker build -t asircar/adaptive-api //to run the image as a container docker run --add-host=<enter your host name>:<host ip address> -p 49161:8089 -d asircar/adaptive-api
That’s all! Your app is now ready to run in your host computer as a container. and you can access it via http://localhost:49161 . So now my microsite can talk to Adaptive Api and then adaptive api can talk back to microservice running on my host, as I have not containerized that yet, hopefully coming soon in windows container! That add-host command is allowing me to add the host name associating with the ip address, without that my node app can’t talk back to my microservice.
Alright! that was quite easy. Will come up soon with the microservice being hosted in a windows container and how the service and the api can talk to each other.