The Docker Essentials Tutorial (Part 2/3)πŸ‹

1. Overview

In the first part of the tutorial we got our feet wet with docker and ran our first container.

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

2. Docker Essentials

2. 1 List Docker images

While we have seen how to obtain images (with docker run […]) and how to list our instantiated containers (with docker ps -a) we want to also display our images we have locally available.
This is easy:

 
docker images

We can also show hidden / incomplete images with

 
docker images -a

2. 2 Dockerfile and why you need them

In order to explain why so called Dockerfiles are useful let’s assume a simple example. We want to have a ubuntu container and run nodejs on it (like in our first tutorial).
So first we would pull the image:

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

And then get the necessary software:

apt-get -qq update
apt-get install nodejs curl nano
nano index.js

And so on. This process can be put into a dockerfile that acts like a bash script.
Therefore we create a file on our host system called Dockerfile and put the following content in it.

nano Dockerfile
ARG  CODE_VERSION=latest
FROM ubuntu:${CODE_VERSION}

RUN apt-get -qq update
RUN apt-get install -yqq nodejs curl nano

CMD  /bin/bash

This Dockerfile has three parts and consists of different directives (written in capital letters). The first two lines define a version argument that will be used in the FROM directive. The FROM directive can be seen equal to pulling an image (like we did with docker run […]).

After that, the RUN commands execute the steps we had previously taken manually. An at the end we can define with the CMD directive a default command to be executed when you run docker run without any commands. In this case we want to have our command prompt.

Now we can build our own docker image with docker build.

docker build -t pushcommit/myveryfirstimage .

The -t flag allows us to use a tag for the image in the format …/… .

You will see how the steps of our dockerfile are executed sequentially.

At this point we can run our freshly baken docker image.

docker run -it pushcommit/myveryfirstimage

2. 3 Pushing images and DockerHub

Let’s say you just built a docker image and want to share it with your coworkers. You can upload (push) it to DockerHub’s public (free) /private (not free) repository.

After you created an account on Dockerhub first log in

 
docker login -u YOURUSERNAME -p YOURPASSWORD

Then just push the image via docker push.

 
docker push pushcommit/myveryfirstimage

Now it will be available for everyone and can be run by your collegues via docker run.

2. 4 Adding and exporting Data into containers

When we created the Dockerfile to set up our nodejs server we forgot to add the nodejs run file. There are two common ways to add files to your images. First you can use the ADD directive in the Dockerfile. This will plain copy files into the images. Let’s see how that works first.

We expand our Dockerfile with the said ADD directive.

ARG  CODE_VERSION=latest
FROM ubuntu:${CODE_VERSION}

ADD index.js /data/

RUN apt-get -qq update
RUN apt-get install -yqq nodejs curl nano

CMD node /data/index.js

We create the index.js file on our host system where the Dockerfile resides. It will be mapped to /data/index.js thanks to the ADD directive in our Dockerfile.

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}`)
})

Let’s rebuild and run our new Docker image.

docker build -t pushcommit/myveryfirstimage .
docker run -d -p 5000:80 pushcommit/myveryfirstimage

The -d flag runs a detached container and we can use curl to make a request to our port 5000.

2. 5 Attaching and executing commands

Our nodejs server container is running in dispatched mode. We can login by docker exec that will execute /bin/bash (start a new shell).
Before we can do that we find our docker container id by docker ps and use it in the exec command

docker ps
#...
docker exec -it 29278d0f2910 /bin/bash

A similar command is the attach command. This will open our initial terminal. You can only open one terminal this way of your container.

docker attach 29278d0f2910

We will see the output of nodejs now.

3. Conclusion & What’s next?

So far we have covered the basics to get started with docker and create & share useful docker images.

For real production environments we learn in the last part of our docker tutorial how to deploy docker containers in the cloud like AWS Beanstalk and utilize the network and more functions of the docker ecosystem.

Leave a Reply

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