The 15 Min Docker Tutorial / “Docker-Starter-Kit” (Part 1/3)πŸ‹

1. Overview

This tutorial is meant to give you the very basic of docker without any unnecessary baggage. So let’s dive into it!

Why user Docker? Many web apps run on virtual machine servers and thus suffer from reduced performance compared to non-virtualized setups. Docker tries to have the best from both worlds: the security and platform-independence of virtualization and the speed of single operation system servers.

The ease-of-use is the third success factor explaining its meteoric rise. Let’s see how fast we can get going.

2. The most important commands

2. 1 Docker Installation

While Docker is available on Mac and Windows we assume to use it on a Linux (Ubuntu / Debian based) system.

 sudo apt-get install docker

This command sets up a docker daemon that will run our containers later. All following commands need elevated privileges so we run them as root:

 sudo su

2. 2 Getting our first image

Docker images are the blueprints of Docker containers. In OO-speak, containers are instances of docker images. If you are new to Docker imagine them like a virtual machine image, just without the hardware part. You can create your own images or download them from a repository like DockerHub or your company’s repository.

For reasons of popularity, we get Ubuntu’s latest LTS version. Docker will fetch it from the repository if it is not available locally. The -it will establish a terminal there we work like as usual.

docker run ubuntu:latest

Now we have downloaded a docker image and even created (and immediately stopped) a container.

2. 2 Creating a container from an image

We have to set the -d flag to make a container run independently or log into our machine with the -it flag. Let’s try the ladder:

docker run -it ubuntu:latest

You will see a prompt from your docker container. Congratulations, we have our first running container. Notice the speed how fast a container is up and running.

If you run uname -a in our container you will see that docker “sees” the same underlying machine.

2. 3 Managing containers

Open a new terminal while leaving your docker container running and execute

docker ps

We see our running container with uptime and so on.

Next run

docker ps -a


Now we also see our very first run attempt on the image that has concluded immediately . Side note: exited containers will take up disk space. If you want a container to be removed after it concludes, run it with the –rm flag!

2. 3 Running commands and apps in containers

Let’s run a new container and expose the container’s http port 80 on our host system on port 5000. It’s like port mapping / port forwarding of virtual machines.

docker run -it -p 5000:80 ubuntu:latest

If we want to install node for example it will fail since there is no apt cache yet. So we fix that by running

apt-get -qq update

Now let’s set up a nodejs server and make an http request (we need the tool curl for that):

apt-get install nodejs curl nano

We create an index.js in a directory of our choice:

nano index.js

The content (exit and save with Ctrl+X and then Y and hit Enter):

const http = require('http')
const port = 80

const requestHandler = (request, response) => {
  console.log(request.url)
  response.end('\nHello World!\n')
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {
  if (err) {
    return console.log('Something went wrong', err)
  }

  console.log(`Server listening on ${port}`)
})

Finally, we can run our http server in our container terminal with

node index.js &

Hit Enter again. If we make an http request via curl we see the output of our server:

curl localhost:80


On our host machine we can get the same on port 5000:

3. Conclusion & what’s next?

If you want to go big, learn how to create your own image of your current nodejs server with docker build (you need a docker file for that). You will learn how to do that in our tutorials continuation. Also you will be able to push your image to a repository like DockerHub and pull and run it on another server (say a coworker’s).

You can also split up your web app into microservices that run in separate containers. In order to connect them utilize docker network.

See how to remove stopped containers and learn how to attach to a container and start it again.

Have fun and enjoy the new world containerization!

Leave a Reply

Your email address will not be published. Required fields are marked *