Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.

Building Your First Docker Container

By - Source: Toms IT Pro

Find out what you need to know to build and maintain a Docker Container.

Credit: Cherezoff/ShutterstockCredit: Cherezoff/ShutterstockDocker is one of a few container management platforms available across operating systems such as Windows, Linux or MacOS. It manages the build, deployment and tear-down of containers and eliminates the "works on my machine" problem by essentially creating an abstraction layer between your application and the underlying operating system while at the same time describing the application including the operating system as code, the so-called Dockerfile.

You use Docker Containers to create and contain everything you need in one image. Once created, it would be easy to manually install an application or change settings inside of the container. However, due to the nature of containers, if we exit the container our settings would have been reverted or, more precise, the layer on top of the container image containing our changes would have been removed.

MORE: Getting Started with Windows Containers

How to build a container image

The process behind container images is fairly simple. They all start with a so-called "Dockerfile" or "Dockerfile.dockerfile" (you can omit the extension if you like). This file is the definition of our container image and might look a bit like this:

This file currently does two things:

  • Tell Docker to get the existing "microsoft/nanoserver" image from
  • Set a LABEL onto the container """

The next step is to now send this "Dockerfile" to Docker to create a local container image of this.

    docker build -t foundation .

This line needs to be executed from the directory where the "Dockerfile" is stored in. We are asking Docker to build an image with the name "foundation" and are pointing it at the Dockerfile in "." (period).

We will now run this container calling "docker run -d foundation powershell". This will ask Docker to execute the container image "foundation" in the detached ("-d") mode and execute powershell.exe.

While this is executing you can now also go ahead and inspect that new container on your host by using "docker inspect [containerId]".

Check the output from "docker inspect" and you will find our custom LABEL that we specified in the Dockerfile.

Layer on Layer on Layer

Docker is great for a common development platform from your local laptop to your actual production container host because it abstracts the actual host away and stores all the configuration in one Dockerfile. This means it is reasonable to assume that a container image that works on your machine will also work on my machine.

The workflow is also very simple as every change to the Dockerfile can easily and effortlessly be tested locally. Docker will only execute those steps again it needs to in order to go back to a working container image. Execute "docker build …" once and it will build the container image, execute it again without changing the Dockerfile and Docker will not execute the steps again, knowing that nothing has changed.

A Dockerfile describes everything that happens inside of a container and uses a specific vocabulary for this. Check out the latest documentation on this on the official Docker website:

A working application described in a Dockerfile can look like this:
FROM microsoft/nanoserver:latest
LABEL maintainer=""
ARG downloadUriGrafana=
RUN powershell -command "Invoke-WebRequest -Uri $env:downloadUriGrafana -Method Get -OutFile"
RUN powershell -command "Expand-Archive -DestinationPath ."
WORKDIR grafana-4.4.1/bin
ENTRYPOINT ["grafana-server.exe"]

In this Dockerfile I am building a Microsoft Nano Server image with Grafana running in it.

The ARG statement tells Docker to expect an argument on the command line during "docker build" phase that will point Docker at the download URI for Grafana. If none is specified, pick this default.

The next two RUN statements now execute PowerShell inside of this container, download Grafana (note how the ARG is referenced as an environment variable) and expand the zip file locally.

The Grafana server automatically gets started whenever this container image gets run by setting a WORKDIR and an ENTRYPOINT.

This Dockerfile is built by running this:
docker build --build-arg downloadUriGrafana= -t grafana-nanoserver .

Again, do not forget the "." (period) as the last character pointing to the Dockerfile.
After having successfully built this image we can now connect to the Grafana website in the container by running a few simple commands:

  • "docker ps" to find the container ID of our just spun up container
  • "docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' containerId"

Now use a browser of your choice and enter http://containerIP:3000 into your address where "containerIP" is the result of the second command.