Dieses Blog durchsuchen

Samstag, 3. September 2016

docker: dockerize your node.js application and your dev-enviroment and deploy it to the docker cloud

Today we want to setup a simple node.js app with express and expressgenerator and deploy it to a docker container. The goal is:
- to have a linux container with our app up and running.
- The sourcecode lives in mounted volume on the host, so, that we can directly

Prerequisits

At first, you need to have some tools installed locally on yout machine. Here they are.
  • node.js installed
  • npm installed
  • docker installed
On ubuntu you can install it like that
$ apt-get install nodejs
$ apt-get install npm
We only need node and npm installed, to create a boilerplate app. In your daily works you will only checkout your project.

On windows and mac you can do it like that
https://nodejs.org/en/download/

 

Install docker

Windows and Mac, follow the instruction under:
https://www.docker.com/products/docker#/linux

If you are on ubuntu you can install docker like that:
http://magento2-tuts.blogspot.de/2016/08/install-docker-on-ubuntu-1604.html

Install node sample app 

We want to have our node.js sourcecode on the local machine, not in the docker container directly. So we are mounting the workingdirectory into the docker container later

Install an express and express-generator
$ npm install -g express express-generator

Create an express project
$ express sampleApp
$ npm install

This will generate our skeleton app. in the current folder

Download your docker baseimage 

To make things simple, we want to use a predefined "node.js ready" ubuntu image from dockerhub. If you dont know docker hub, take your time to create a an acocunt. you will need it later. https://hub.docker.com/

pull your node container
$ docker pull node 
This will download our working image

 

Create your appcontainer

Now let's create our contrainer in which our node.js express app will live. We want, that we can browse the app on port 8080 and we want our sourcecode is mounted as a volume to the the docker container in dir "/var/www"

This can be done by follwing command
$ docker run -p 8080:3000 -v $(pwd):var/www -w "/var/www" node npm start 
To expain that  
  • -p 8080:3000 -> the host port 8080 will be forwarded to port 3000 on the docker container, so we can reach the container under http://localhost:8080
  • -v -> we want to mount a folder on the host as a volume to the container 
  • $(pwd):var/www -> That means to mount the current dir to "var/www" in the container
  • -w "var/www" -> That means to change to "/var/www" on the container. 
  • npm start -> starts the app in "/var/www"

 

Test your container

Now you can surf "http://localhost:8080" and you should see your express site
WOW. thats awesome.

But wait. We want to test, how easy it is to edit your sourcecode.

Go to your app directory where your code files lives and edit the index.hbm in the views folder.

Type "Hi there" somewhere in the html code.

Reload your browser. 

You will see your changes directly









Keine Kommentare:

Kommentar veröffentlichen