The 10 Minute Docker’s Intro

@Olivierodo
4 min readMar 22, 2016

Hello everyone, let’s talk about Docker.
Docker is an amazing tool to deploy your apps in containers. Open-source and simple, Docker can be the devOp’s best friend.

I’ll show you how to deploy a NodeJs app on a container.

1. A simple NodeJs App

Let’s create a small nodejs app with the express package.

The package.json

// ./package.json
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.3.5",
"jade": "*"
}
}

The small hello world app.js

// ./app.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('<h1>Hello World!</h1>');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});

FYI: How to run a nodejs app

To run the app, we just need to install the package with :

$ npm install

Then run the app :

node app.js

or

npm start

2. Create the Dockerfile

The Dockerfile is an instructional file that Docker reads to create the container.

# ./Dockerfile
FROM ubuntu:14.04
# Define the Env var HOME
ENV HOME /usr/src
# Update apt and install nodejs, npm
RUN apt-get update && apt-get install -y \
nodejs \
npm
# Add the current folder in a container's folder
ADD . /usr/src
# Define the workdir folder
WORKDIR /usr/src/
# Install Express
RUN npm install -g express
# Create an alias for nodejs
RUN ln -s /usr/bin/nodejs /usr/bin/node
# Define the port to expose
EXPOSE 3000
# Command to run the app
CMD npm install && npm start

3. Build the container

Before running the container you need to build it with docker build.

$ docker build -t docker-intro .

The docker-intro image container has now been created.

See the image list :

$ docker images

Until you update the Dockerfile you won’t need to build your container anymore.
Docker has a cache system to build the image.

4. Run the container

Running the container is simple with docker run

$ docker run -d -p 80:3000 -v $(pwd):/usr/src --name din docker-intro

Parameters

  • -d detach the container
  • -p 80:3000 Do a port forwarding from the container’s port (3000) to the host’s port (80)
  • -v $(pwd):/usr/src Mount the current path ( $(pwd) ) to the workdir inside the container (/usr/src)
  • -name din Call the container din
  • docker-intro Docker image to use

Now you can see the result at the address :

http://127.0.0.1

If you use docker toolbox use the command `docker-machine ip` to know the docker VM’s ip.

If you want to run again the container you may have this error:

YYYY/mm/dd HH:ii:ss Error response from daemon: Conflict, The name din is already assigned to {CONTAINER_ID}. You have to delete (or rename) that container to be able to assign din to a container again.

That means you need to delete the container to run it again.

If you don’t modify the Dockerfile it’s not necessary to build your container before running it

5. See the running containers

To see if the container is running use docker ps :

$ docker ps -a

You should see something like:

6. Delete the container

if you update your Dockerfile you’ll need to build and run your container. If you have an error when you will want to run the container with the same name. So delete the old one.

$ docker rm -f din

7. Start, Stop, Restart…

Start a container

If you container isn’t running, start it with docker start

$ docker start din

Stop a container

If you container is running, stop it with docker stop

$ docker stop din

Restart a container

If you container is running, restart it with docker restart

$ docker restart din

8. Enter the container

Sometimes you want to enter in the container in development mode or to debug.

The container is running

In the case, your container is running you have to use the docker exec command

$ docker exec -it din bash

This way you will access by ssh on your container as root user.

The container is not running

In the case, the container is not running you have to still can use the docker run command but the the options -i -t

$ docker run -it -p 80:3000 -v $(pwd):/usr/src --name din docker-intro bash

We use the option -it and decide to override the CMD command from the Dockerfile (npm start) by bash
This way you will access by ssh on your container as root user.

Conclusion

Now you know the basics to start with Docker, do a round in the documentation and the dockerhub you will find amazing information.

Find all the files mentioned at https://github.com/olivierodo/docker-introduction

--

--

@Olivierodo

Open Source and Test automation Lover. Creator of RestQA