1. Hello Docker
Docker is an open standards platform for developing, packaging and running portable distributed applications. Using Docker, developers and sysadmins may build, ship and run applications on any platform such as a PC, the cloud, data center or a virtual machine. Getting all the required dependencies for a software application including the code, the runtime libraries, and the system tools and libraries is often a challenge when developing and running an application. Docker simplifies the application development and execution by packaging all the required software for an application including the dependencies into a single software unit called a Docker image that may be run on any platform and environment.
What makes Docker images unique and different from virtual appliances, which are also software images (virtual machine images), is that while each virtual machine image runs on a separate guest OS, the Docker images run within the same OS kernel. Docker software runs in an isolated environment called a Docker container that includes its own filesystem and environment variables. Docker containers are isolated from each other and from the underlying OS.
A Docker container for a software application includes all that is required to run the software, and files may be copied from the host OS to a Docker container if required. As an application could require other software to develop a linked application, Docker containers may be linked, which makes the environment variables and software from another Docker container available to a Docker container.
Docker makes use of a Dockerfile to build an image. A Dockerfile consists of all the instructions such as what software to download, which commands to run, which network ports to expose, which files and directories to add to the filesystem, and which environment variables to set. A Docker image may be made an executable by providing an entrypoint. A Docker image may be built by providing a Dockerfile, or pre-built Docker images may be downloaded from the Docker Hub ( https://hub.docker.com/ ). The complete instruction set supported by Dockerfile can be found at http://docs.docker.com/engine/reference/builder/ .
In this chapter, we shall install the Docker engine on Linux, download a Hello World Docker image, and run a Docker container for a Hello World application. We have used Linux because some of the other software we have used, such as Apache Hadoop, is supported (both in development and production) only on Linux. We have used two commonly used distributions of Linux, Red Hat 7 and Ubuntu 14, but any of the supported installations ( https://docs.docker.com/v1.8/installation/ ) could be used.
Setting the Environment
Installing Docker on Red Hat 7
Uninstalling Docker
Installing a Specific Docker Version
Installing Docker on Ubuntu
Starting the Docker Service
Finding the Docker Service Status
Running the Docker Hello World Application
Downloading a Docker Image
Running an Application in a Docker Container
Listing Running Docker Containers
Accessing the Application Output on Command Line
Accessing the Application Output in a Browser
Stopping a Docker Container
Removing a Docker Container
Removing a Docker Image
Stopping the Docker Service
Setting the Environment
We shall use Amazon EC2 instances based on Linux for deploying Docker and Docker images. Linux is required to support 64 bit software. We have made use of two different 64 bit (required) Amazon Machine Images (AMIs):
Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-d05e75b8 64 bit
Red Hat Enterprise Linux version 7.1 (HVM), EBS General Purpose (SSD) Volume Type (ami-12663b7a) 64 bit
An Amazon EC2 instance based on the Ubuntu AMI is shown in Figure .
Figure 1-1.
Amazon EC2 Instance Based on Ubuntu AMI
To connect to an Amazon EC2 instance, the public IP address is used. The public IP address may be obtained from the EC2 Console as shown in Figure .
Figure 1-2.
Obtaining the Public IP Address
Connect to an Amazon EC2 Ubuntu instance using SSH and the public IP address with the following command in which docker.pem is the private key format (.pem) generated by Amazon EC2.
ssh -i "docker.pem" ubuntu@54.86.12.113
The Ubuntu instance gets connected to as shown in Figure .
Figure 1-3.
Connecting to Ubuntu Instance on Amazon EC2 from Local Host
If a Red Hat AMI is used the command to connect to the Amazon EC2 instance is slightly different. Instead of the user ubuntu use the ec2-user user. For example, connect to the Linux instance using the following command in which docker.pem is the private key format (.pem) generated by Amazon EC2.
ssh -i "docker.pem" ec2-user@54.175.182.96
The RHEL 7.1 instance gets connected to as shown in Figure .
Figure 1-4.
Connecting to RHEL Instance
Run the following command to find if the Linux architecture supports 64 bit software.
uname -r
The x86_64 in the output as shown in Figure indicates that 64 bit is supported.
Figure 1-5.
Finding Architecture Support
Installing Docker on Red Hat 7
Two different methods for installing Docker on Red Hat are available: install with yum or install with script. Installing with yum requires a user to add the yum repo, which could be more involved than the script option. We have used the Docker installation script to install Docker.
As a user with sudo or root privileges, update the local repository packages with the following command.
sudo yum update
Run the Docker installation script to install Docker Engine.
curl -sSL https://get.docker.com/ | sh
Docker Engine gets installed as shown in Figure .
Figure 1-6.
Installing Docker Engine
Before starting the Docker service, you should modify the docker.service file to disable the Docker start timeout. The docker.service file is in the /usr/lib/systemd/system directory, which has permissions set. Either run a sudo command or copy the file to a directory which does not have permissions set. For example, copy the docker.service to the root directory with the following command.