Microservices with Docker - part 2

Monday, September 21, 2015

A gentle, pragmatic introduction to Microservice Architecture and Docker.


In the previous part 1 of this post series, we introduced the Microservice Architecture paradigm and given a preliminary introduction to the Docker platform.

In this part 2 we are going to learn how to start using Docker, installing it on OS X embracing a pragmatic approach.

Running Docker, pulling Images and running Containers, etc… requires a Linux machine.
On Mac OS X we need a Virtual Machine.
Starting from version 1.8 Docker deprecated the old Boot2Docker command line tool in favor of the new Docker Machine. Then, they provide an handy Docker Toolbox to install Docker Machine as well as the other Docker tools.

Just follow the documentation to install the Docker Toolbox and to run it (on OS X).

When the Docker Machine (which creates Docker hosts) is running on your Mac, you’re ready to interact with Docker through the Docker Client command line interface (docker).

After installing Docker Toolbox, run Docker Machine from Applications/Docker choosing Docker Quickstart Terminal.app



After some init operations you should see something like this on a Terminal window...


Basically, we’ve just created a Docker Machine and now it is ready to accept commands through our Docker Client.

A Docker Machine lets you create Docker Hosts on your computer (or on cloud). It automatically creates hosts, installs Docker on them, then configures the Docker Client to talk to them. A machine is the combination of a Docker host and a configured Docker Client.

Docker Machine provides several commands for managing them. Using these commands you can:

- start, stop, and restart a host
- upgrade the Docker Client and daemon
- configure a Docker client to talk to your Docker Host
- etc...


Summarizing: our previous step created a new Docker Host on OS X, run a new Docker machine called default and configured a Docker Client. The following picture shows our Docker architecture on OS X.

The configured Docker system on Mac OS X



We're ready to explore some essential Docker commands.

Basic Docker Machine commands

$ docker-machine --help
Prints the command general help.


$ docker-machine ls
Shows all the existing Docker machines.

$ docker-machine start <machine>
$ docker-machine stop <machine>
Start and Stop machines. E.g.,  $ docker-machine start default


$ docker-machine ip <machine>
Get the IP address of a machine.



For a comparison between old (deprecated) Boot2Docker commands and Docker Machine please refer to this documentation.

Now that we have a Docker host running with a default machine we’re ready to know how to use the Docker Client command line interface.



Common Docker Commands


$ docker --help
$ docker <CMD> --help

it prints the help, where CMD is a specific docker command, like run


$ docker pull <IMAGE_NAME>
Downloads an Image from the Docker Hub repository given its name.

$ docker run <IMAGE_NAME>
Downloads locally, if not already done, an Image from repository and runs a Container based on it.


Examples:

$ docker run -i -t ubuntu

Runs an Ubuntu container:
-i : interactive connection grabbing STDIN
-t: pseudo tty-terminal

$ docker run -i -t -p 8080:3000 node-express
Runs an Image as a Container, mapping internal 3000 port to 8080 as visible by external requests.



$ docker ps -a
Lists all Containers (not only running Containers)



$ docker images
Lists all downloaded Images



$ docker commit -a "Your Name <youremail@email.com>" -m "ubuntu and node" CONTAINER_ID node-ubuntu:0.1
Creates a new image from the container given its CONTAINER_ID, tagging it with a version.

$ docker tag node-ubuntu:0.1 node-ubuntu:latest
Adds the latest tag to the existing image node-ubuntu
When a docker run command is launched on that Image without a version, the latest tagged version is used.


$ docker rm <YOUR_CONTAINER_ID>
Removes a Container


$ docker rmi <YOUR_IMAGE_ID>
Removes an Image



Example n.1: Running the hello-world container


The “hello-world” is a Docker Image, thus published on the Docker Hub, in order to explain how Docker works. Simply, it allows to run a container which prints some useful information.

To download the Image and run a Container based on that, simply run the command:

$ docker run hello-world

Resulting in:


Reading the printed message on the Terminal show you what’s happened.

Now, running the command:

$ docker ps -a
it will show you all the containers, running or not, on your Docker Host and you should be able to find the hello-world Container and its current (exited) status.

Running the command:

$ docker images
it will show you all the locally downloaded Images, including the hello-world one.


Example n.2: Running an interactive Ubuntu Container


Let’s try something a little bit more “complex”. We’d want to run an Ubuntu-based Container in an interactive way, saying to Docker to open a shell on it. There’s an official Ubuntu Image on Docker Hub, thus, we can run the command:

$ docker run -i -t ubuntu
which runs interactively an Ubuntu Container, the parameters are:

-i: interactive connection grabbing STDIN
-t: pseudo tty-terminal

The command tells the Docker daemon to download the Ubuntu Image and to interactively run a container based on that Image.
So, we can use our Linux shell on the running container, for example launching the ll command:



Running the Linux exit command shuts down our Container, exiting.


Summarizing, in this second post of the series we started learning:

- how to run Docker on Mac OS X installing the new Docker Toolbox
- the basic Docker Machine and Docker Client commands
- how to run two simple containers

In the next post of this article series we will learn how to dockerize a node.js application.
Stay tuned!


You Might Also Like

0 commenti

Subscribe