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
Your email address will not be published. Required fields are marked *