A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Once you have a Docker file ready you can “build” this file into an image and then spawn a container based on this image. In this article we will create a simple Dockerfile to build an image with Ubuntu, Node.js, Express.js and a sample app installed into it.
A Dockerfile is nothing but a simple text file. Just create a plain text file in any of your favorite text editor and name it Dockerfile. This file should be in the project folder since we will be using the
Generally the convention is that you should name this file Dockerfile and keep this in your project folder.
The Dockerfile that we will be using will be as follows
RUN apt-get update
RUN apt-get install -y --no-install-recommends apt-utils \
RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nv.sh
RUN bash install_nv.sh
RUN . "/root/.nvm/nvm.sh" && nvm install stable && nvm use stable
RUN apt-get install -y npm
RUN npm install express-generator -g
RUN ln -s /usr/bin/nodejs /usr/bin/node
RUN express -e
RUN npm install
ENTRYPOINT ["node", "bin/www"]
Once you have the Dockerfile in your project folder you can then use the following command to build the Docker image
$ docker build -t <image_tag> /path/to/docker_folder
Once the build starts you will notice the progress that happens with each line of instruction.
Step 1/15 : FROM ubuntu:latest – This step states that the Docker container will be based on the latest version of Ubuntu Docker image. So, our container will have Ubuntu as its operating system.
Step 2/15 : WORKDIR /tmp – This step is to change the directory. This is something like the cd command which is used in most Linux or UNIX based operating systems.
Step 3/15 : RUN apt-get update – This creates a list of packages for updates
Step 4/15 : RUN apt-get install -y –no-install-recommends apt-utils curl build-essential libssl-dev ca-certificates – In this step we start installing some required packages for Ubuntu which are used later. The -yswitch makes this an unattended installed.
Step 5/15 : RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nv.sh – Here we are downloading Node Version Manager (NVM)
Step 6/15 : RUN bash install_nv.sh – This will install NVM in the container.
Step 7/15 : RUN . “/root/.nvm/nvm.sh” && nvm install stable && nvm use stable – This step installed the latest stable version of Node.js via NVM and sets it as the default version to be used in the container
Step 8/15 : RUN apt-get install -y npm – This step is going to install Node Package Manager (NPM)
Step 9/15 : RUN npm install express-generator -g – This step is going to install the Express app generator
Step 10/15 : WORKDIR /srv – We are now changing our directory to /srv where our app files will reside
Step 11/15 : RUN ln -s /usr/bin/nodejs /usr/bin/node – This step creates a “symlink” for the nodejs package so that the command node can work as expected
Step 12/15 : RUN express -e – This will create a barebones Express app in the /srv directory with the application folder structure
Step 13/15 : RUN npm install – This command will install all Node.js dependencies from the express apps package.json file
Step 14/15 : EXPOSE 3000 – This step exposes port 3000 to the host machine. This is the port where Express runs by default
Step 15/15 : ENTRYPOINT nodejs bin/www – This step says that when the container is started run the node bin/www command so that the Express app starts.
Once the image is built successfully you now have to run the container using this image. You can first use the following command to check if your image has been created
$ docker images
You should notice on your screen the image with the tag name that you may have given at build time if the build was successful
To run the container you can use the following command
$ docker run -p 3000:3000 -tid --name=<container_name> <image_tag>
You can use the following command to check if your container is running
$ docker ps -a
If your container has started without any errors then you can go to http://localhost:3000/ on your browser, where you should be able to see the Express’ default Welcome Page.
That’s it! Go ahead and create your own Dockerfile. A good place to look at various Dockerfiles to understand how all the other statements work will be http://hub.docker.com/
Technologies: Docker, Linux / Unix
post via Codincafe
We have been using Express.js for all our projects so far and there are have been no regrets. But it was time that we developed a Bolierplate template that consists of all the dependencies and repeated stuff like Session management, CSRF, File Uploads etc. that are often needed for every project. So we started working on it, but stumbled upon Sails.js. It’s true that Sails.js has been out there from a long time and we should have looked at it earlier, but we are glad that we worked on Express.js first, since Sails.js is built tightly on top of Express.js which makes it easier to understand the inner workings of Sails.js
One of the features that we thought was very cool is a very neat and tidy Models & ORM which works out of box with most of the SQL and No-SQL database. Sails.js makes use of Waterline adapter as its default ORM which can be used for data storage and retrieval for Node.js with support for MySQL, MongoDB, PostgreSQL, Redis, and more.
So, with all the nice features and bells and whistles ready to be used lets get started. This tutorial shows how to install and start your first app.
Assuming you have Node.js and NPM installed you can install Sails.js with the following command
$ npm install sails -g
Once sails is install you can create your sails app with
$ sails new my-app
*Note: You can change my-app to the name of your project. A new project directory with the name supplied will be created
Once your project has been created you can start your app with the following command
$ sails lift
Once the app has started you will see the output as below in your terminal.
Your app will now started on the frameworks default port 1337
The main file where the app is spawned is app.js. So you can run the app with node, forever or any other way in situations where Sails CLI is not relevant or useful. We like to work with nodemon as it gives a nice way to watch file changes and restart the app automatically to speed up the development.
You can install nodemon by
$ npm install -g nodemon
The following command will start the app with nodemon
$ nodemon app.js
Once the nodemon starts the app, you will see the same app output as above and the app starts on port 1337. Now in your browser type http://localhost:1377 where you will see the default Sails.js home page. We will look where it comes from and how its handles in a little bit.