First of all, what is Docker software?
It is recognised that Wikipedia defines Docker software as, “an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualisation on Linux.” Therefore, Docker software is a tool that allows us to package our Docker app with all of its dependencies needed in order to run it into a standardised unit for software development. Therefore, main Docker elements include:- Containers: directories containing everything for your application.
- Images: snapshots of containers or base OS (e.g. Ubuntu) images.
- Dockerfiles: scripts automating the building process of images.
Docker Containers
Most noteworthy, a Docker container includes the Docker app and all of its dependencies. The kernel can be shared with other containers. It can also run as an isolated process in the user space on the host operating system. Since Docker containers are not tied to any specific infrastructure, they can run on any computer, on any infrastructure, and in any cloud.Comparison Between Containers & Virtual Machines
Therefore, each Docker container starts from a Docker image which forms the base for other applications and layers to come.Docker Images
It’s important to note that all Docker images constitute the base of Docker containers from which everything starts to form. As a result of this, they are very similar to default operating-system disk images. These are also used to run applications on servers or desktop computers. Furthermore, these base images can be explicitly stated when working with the Docker CLI to directly create a new container. In addition, they might be specified inside a Dockerfile for automated image building.Dockerfiles
Especially relevant, Dockerfiles are scripts containing a successive series of instructions, directions, and commands which are executed to form a new Docker image. Each command executed translates to a new layer of the onion, forming the end product. In other words, they basically replace the process of doing everything manually and repeatedly. Furthermore, when a Dockerfile is finished executing, it forms an image that you can then use to start a new container.Installation
An installations guide is available on https://docs.docker.com/. We can also follow this step-by-step guide to install docker depending on our OS. Once you are done installing the docker software, open a command-line terminal, and run a few Docker commands to verify Docker is working as expected. A useful command to try is the Docker version to check you have the latest release installed. In addition, use Docker ps and Docker run hello-world to verify that Docker is running correctly.$ docker run hello-world Hello from Docker!Furthermore, this message shows that your installation appears to be working correctly.
Create Nodejs App
Create a directory for this app named “src.” In this directory, create a package.json file with the following content:{ "name": "helloworld", "version": "0.0.1", "description": "Node.js Hello world app on CentOS using docker", "dependencies": { "express": "3.2.4" } }Open the terminal in your app directory and run:
$ npm installAn index.js file must be created in order to define a web app using the Express.js framework code.
var express = require('express'); var PORT = 8080; var app = express(); app.get('/', function(req, res) { res.send('Hello world\n'); }); app.listen(PORT); console.log('Running on http://localhost:' + PORT);
Running This App
In these next few steps, we’ll look at how you can run this app inside a CentOS container using Docker. First, you’ll need to build a Docker image of your app. Now, open the terminal in your app directory and run:$ touch DockerfileFirst, open the Dockerfile in your favourite text editor. Next, define the parent image you want to use to build your own image on top of. Here, we’ll use CentOS available on the Docker Hub:
FROM centos:centos6Since we’re building a Node.js app, you’ll have to install Node.js as well as npm on your CentOS image. It’s important to note that Node.js is required to run your app. And npm is required to install your app’s dependencies defined in package.json. In addition, to install the right package for CentOS, we’ll use the instructions from the Node.js wiki: *Tech Tip: If you find yourself wanting to learn more about Node.js, be sure to visit our blog post about how to build a website with Node.js.
# Enable Extra Packages for Enterprise Linux (EPEL) for CentOS RUN yum install -y epel-release # Install Node.js and npm RUN yum install -y nodejs npm Now, install your app dependencies using the npm binary: # Install app dependencies COPY package.json /src/package.json RUN cd /nodejs; npm install --productionNext, in order to bundle your app’s source code inside the Docker image, use the COPY instruction:
# Bundle app source COPY . /srcSince your app binds to port 8080, you’ll use the EXPOSE instruction to have it mapped by the Docker daemon:
EXPOSE 8080
Last Step
Finally, define the command to run your Docker software using CMD which defines your runtime, i.e. node, and the path to our app, i.e. nodejs/index.jsCMD ["node", "/src/index.js"]In conclusion, your Dockerfile should now look like this:
FROM centos:centos6 # Enable Extra Packages for Enterprise Linux (EPEL) for CentOS RUN yum install -y epel-release # Install Node.js and npm RUN yum install -y nodejs npm # Install app dependencies COPY package.json /src/package.json RUN cd /src; npm install --production # Bundle app source COPY . /src EXPOSE 8080 CMD ["node", "/src/index.js"]
Build Image
In your directory Dockerfile, run the following command to build your Docker image. Since the -t flag lets you tag your image, it’s easier to find it later using the docker images command.$ docker build -t /centos-node-helloworld .Now, your image will now be listed by Docker:
$ docker imagesRun the Image Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port in the container. Next, run the image you previously built:
$ docker run -p 49160:8080 -d /centos-node-helloworldIn order to receive your app information, use:
$ docker ps $ docker logsAs a result, in the example above, Docker mapped the 8080 port of the container to 49160. To test your application, run:
$ curl -i localhost:49160 HTTP/1.1 200 OK X-Powered-By: Express Content-Type: text/html; charset=utf-8 Content-Length: 12 Date: Mon, 03 Oct 2016 13:34:26 GMT Connection: keep-alive Hello worldAnd there you have it! We hope this tutorial on Docker software has been helpful and inspires you to try out your coding techniques with other programs. For more coding tutorials and news on the latest tech trends, be sure to visit our tech blog.