Docker has become the de facto containerization platform. The main appeal of Docker over virtual machines is that it is lightweight. Whereas a virtual machine packages a complete OS in addition to the application binaries, a Docker container is a lightweight abstraction at the application layer, packaging only the code and dependencies required to run an application. Multiple Docker containers run as isolated processes on the same underlying OS kernel. Docker is supported on most commonly used OSes, including several Linux distributions, Windows, and MacOS. Installing Docker on any of these platforms involves running several commands and also setting a few parameters. CoreOS Linux has Docker installed out-of-the-box. We will get started with using Docker Engine on CoreOS in this chapter. This chapter sets the context of the subsequent chapters, which discuss design patterns for managing Docker Engine using the Swarm mode. This chapter does not use Swarm mode and provides a contrast to using the Swarm mode. This chapter includes the following sections:
Setting the Environment
We will be using CoreOS on Amazon Web Services (AWS) EC2, which you can access at , to launch an instance .
Figure 1-1.
Selecting an AMI for CoreOS Linux
From Choose an Instance Type, choose the t2.micro Type and click on Next. In Configure Instance Details, specify the number of instances as 1. Select a network or click on Create New VPC to create a new VPC. Select a subnet or click on Create New Subnet to create a new subnet. Select Enable for Auto-Assign Public IP. Click on Next.
From Add Storage, select the default settings and click on Next. In Add Tags, no tags need to be added. Click on Next. From Configure Security Group, add a security group to allow all traffic of any protocol in all port ranges from any source (0.0.0.0/0). Click on Review and Launch and subsequently click on Launch.
Select a key pair and click on Launch Instances in the Select an Existing Key Pair or Create a New Key Pair dialog, as shown in Figure .
Figure 1-2.
Launch instances
An EC2 instance with CoreOS is launched. Obtain the public DNS or IPv4 public IP address of the EC2 instance from the EC2 Console, as shown in Figure , to SSH login into the instance.
Figure 1-3.
Public DNS and public IPv4
SSH login into the EC2 instance as user core.
ssh -i "coreos.pem" core@
Running a Docker Application
As mentioned earlier, Docker is pre-installed on CoreOS. Run the docker command to list its usage, as shown in the following bash shell:
core@ip-172-30-4-75 $ docker
Usage: docker [OPTIONS] COMMAND [arg...]
docker [ --help | -v | --version ]
A self-sufficient runtime for containers.
Options:
--config=/.docker Location of client config files
-D, --debug Enable debug mode
-H, --host=[] Daemon socket(s) to connect to
-h, --help Print usage
-l, --log-level=info Set the logging level
--tls Use TLS; implied by --tlsverify
--tlscacert=/.docker/ca.pem Trust certs signed only by this CA
--tlscert=/.docker/cert.pem Path to TLS certificate file
--tlskey=/.docker/key.pem Path to TLS key file
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
Output the Docker version using the docker version command. For native Docker Swarm support, the Docker version must be 1.12 or later as listed in the bash shell output.
core@ip-172-30-4-75 $ docker version
Client:
Version: 1.12.6
API version: 1.24
Go version: go1.7.5
Git commit: a82d35e
Built: Mon Jun 19 23:04:34 2017
OS/Arch: linux/ amd64
Server:
Version: 1.12.6
API version: 1.24
Go version: go1.7.5
Git commit: a82d35e
Built: Mon Jun 19 23:04:34 2017
OS/Arch: linux/amd64
Run a Hello World app with the tutum/hello-world Docker image.
docker run -d -p 8080:80 --name helloapp tutum/hello-world
The Docker image is pulled and a Docker container is created, as shown in the following listing.
core@ip-172-30-4-75 $ docker run -d -p 8080:80 --name helloapp tutum/hello-world
Unable to find image 'tutum/hello-world:latest' locally
latest: Pulling from tutum/hello-world
658bc4dc7069: Pull complete
a3ed95caeb02: Pull complete
af3cc4b92fa1: Pull complete
d0034177ece9: Pull complete
983d35417974: Pull complete
Digest: sha256:0d57def8055178aafb4c7669cbc25ec17f0acdab97cc587f30150802da8f8d85
Status: Downloaded newer image for tutum/hello-world:latest
1b7a85df6006b41ea1260b5ab957113c9505521cc8732010d663a5e236097502
List the Docker container using the docker ps command.
core@ip-172-30-4-75 $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b7a85df6006 tutum/hello-world "/bin/sh -c 'php-fpm " 19 minutes ago Up 19 minutes0.0.0.0:8080->80/tcp helloapp
The port mapping for the Docker container is also listed using the docker ps command, but it may also be obtained using the docker port command.
core@ip-172-30-4-75 $ docker port helloapp
80/tcp -> 0.0.0.0:8080
Using the 8080 port and localhost , invoke the Hello World application with curl .
curl localhost:8080
The HTML markup for the Hello World application is output, as listed shown here.
core@ip-172-30-4-75 $ curl localhost:8080
body {
background-color: white;
text-align: center;
padding: 50px;
font-family: "Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif;
}
#logo {
margin-bottom: 40px;
}
Hello world!
My hostname is 1b7a85df6006
Using the public DNS for the EC2 instance, the Hello World application may also be invoked in a browser. This is shown in the web browser in Figure .
Figure 1-4.
Invoking the Hello World application in a web browser