everythingpossible - Fotolia


How to build an application on a Docker host

From building Docker images to executing Docker machines, there are plenty of opportunities as a result of building an application on top of a Docker host in vCloud Air.

As Docker continues to gain momentum in the data center, users are becoming more curious as to how they can utilize its technology. Cloud providers have been particularly interested, relying on Docker-based applications to create an infrastructure that supports containers.

In a recent article, I offered a step-by-step guide to creating a Docker host on vCloud. In the interest of continuing this Docker-centric education, we'll be taking a look at how to build a Docker application that is easy to create, manage and deploy and that sits on the Docker host. This application isn't meant to do much other than just display what is possible.

Each Docker file is a series of file systems stages glued together to provide a specific service or number of services. Docker files should be stateless -- they shouldn't contain any important data because Docker is designed to provide elastic services. This means all significant transactions need to be written on a properly configured, permanent database service or another appropriate persistent storage option. Nothing important should be written to a Docker instance, as it is only temporary. Docker machines are designed to scale up and down, depending on demand.

Creating a Docker instance

The first thing to do is open a Docker account that can be used to host your image files. Single user noncommercial accounts are free on the Docker Hub, though there are other options and providers available.

With that said, let's look at how to go about creating a trivial Docker instance. If you have any experience with Docker, you may be familiar with the whalesay application. Whalesay comes from a repository that belongs to Docker, so you can see the basic repository format. Repositories are, without a doubt, the most efficient way to manage Docker images.

Docker repositories hold Docker images as binary images; this means users can pull as many instances as they wish onto various Dockerized hosts. Thanks to Docker repositories, it's also possible to have multiple versions of the images available so you can have a quality assurance release as well as a production release that can be pulled down and run quickly and easily. Even those Docker hosts not on vCloud can pull down the images you create and run them in an identical way.

The first thing to do is open a Docker account that can be used to host your image files. Single user noncommercial accounts are free on the Docker Hub, though there are other options and providers available. For the purpose of this article, I created an example account on the Docker Hub. Set your account to private while you experiment with the software.

Docker image repository.
Figure A. Creating a Docker image repository.

To enable Docker repository uploads and downloads, you must first enable the Docker host to securely interact with the Docker repository. A standard Docker installation has a built-in mechanism to do this -- the "docker login" command. Running the command is quite straightforward, and it asks you for your name, repository and password. These credentials are then cached to save you from having to log in every time. If you want to stop the caching, use the command "docker logout."

Figure B. Running the

Creating a Dockerfile

Once that's taken care of, you can create a basic Dockerfile that contains all the build instructions for constructing your software images. Note that your Dockerfile must be named Dockerfile and is case sensitive.

I am using a networked Ubuntu VM with Docker installed locally to build my images. On your VM, create a folder to put all the Docker data in. I've included a very simple Dockerfile below:

FROM ubuntu:14.04

Maintainer Joe Public <[email protected]>

# Very simple example of building a docker instance with a

# .Dockerfile

# Install base Ubuntu server installation

FROM ubuntu:14.04

# Run update. The RUN command is used to run commands within the image at build time.

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -yq install htop

# The CMD file shows the command that will be run if no

# command line is specified at runtime.

CMD ["htop"]

The first line tells Docker that we want to build from Ubuntu 14. It's also good practice to make sure the maintainer line is filled out correctly. Comments can be included by prefixing any comment with a # symbol, as is demonstrated above.

Once you have saved the Dockerfile to disk, you can build and test it locally by issuing the "docker build" command. For the build to work, you must be in the same directory as the Dockerfile.

A word of caution: If you intend to upload your Docker instance to the Docker Hub, you will need to build your Docker instance using the repository name you received when you signed up for a Docker account. To build the Dockerfile so it can be uploaded, use the following syntax:

            docker build -t user\repository:v1

Building a Dockerfile.
Figure C. Building a Dockerfile.

The "v1" denotes a tag. Tags can be used to define specific build versions so you can have multiple versions to pull. If you don't specify a tag, Docker will assume the build requires the "latest" tag and will take ownership from the most recent version, so be careful. To see all the local images, use the command "docker images." You can see this in my example in Figure D.

Docker images in a repository.
Figure D. An example of all local images in a repository.

If I wanted to, I could specify an older build by appropriate use of the ":" function. For example, if I have several versions all tagged "v1" through "v12," I could specify pulling version 10 by adding ":v10" to the end of the build command. If the file is not present, it will be downloaded into the local image store.

Pushing an image to a Docker registry

The Docker build command creates a Docker image, and any errors in the build process will be highlighted here. When the image is built, you can then push it upstream to your Docker registry by using the following command:

            docker push user/repository

This may take several minutes. At this point, if you had set your registry to public anybody could have downloaded your code by doing a Docker pull version of that same command.

Pushing a Docker image.
Figure E. Pushing image upstream to a Docker registry.

Now that you have a Docker host built in the vCloud environment, you can pull down the new image to your Docker host and run it on the Photon host.

Log in to vCloud, locate the machine you created and open the console button in the top right corner. This will open a Web console to your host.

To pull down the newly created image, use the console and Docker login just as you did before. Once you are connected, run a Docker pull, substituting your values in the appropriate spots. The command below uses my example.

            docker pull sburns2016/examplehub:v1

You should see the image download. You can then execute the machine by running the following command:

            docker run sburns2016/examplehub

This will run the Docker image in the foreground and show the results of the htop command as auto-executed.

Since this is running in the foreground, we'll only execute the one instance. Full applications will run in daemon mode and will return the console.

Next Steps

How does storage work in Docker?

The top Docker tips of 2015

Docker leads the container charge in the cloud

Dig Deeper on Using VMware cloud computing tools