Docker Tutorial: Get Going From Scratch

This is a post I wrote for Stackify a while back. You can find the original here. Docker is one of the most exciting technologies I’ve seen in a long time. I enjoy working with it.

Docker is a platform for packaging, deploying, and running applications. Docker applications run in containers that can be used on any system: a developer’s laptop, systems on premises, or in the cloud.

Containerization is a technology that’s been around for a long time, but it’s seen new life with Docker. It packages applications as images that contain everything needed to run them: code, runtime environment, libraries, and configuration. Images run in containers, which are discrete processes that take up only as many resources as any other executable.

It’s important to note that Docker containers don’t run in their own virtual machines, but share a Linux kernel. Compared to virtual machines, containers use less memory and less CPU.

However, a Linux runtime is required for Docker. Implementations on non-Linux platforms such as macOS and Windows 10 use a single Linux virtual machine. The containers share this system.

Containerization has enjoyed widespread adoption because of its

  • Consistent test environment for development and QA.
  • Cross-platform packages called images.
  • Isolation and encapsulation of application dependencies.
  • Ability to scale efficiently, easily, and in real time.
  • Enhances efficiency via easy reuse of images.

We’ll look at these basic concepts as we install the Docker tools, and create images and containers.

Get Started with Docker

We’ll start by installing the Docker desktop tools found here. Download the correct installer for your operating system and run the installation.

Running a container

Once we install the tools, we can run a Docker image:

output of docker hello-world image

docker run hello-world does exactly what it sounds like. It runs an image named “hello-world.”

Docker looks for this image on our local system. When it can’t find the image, Docker downloads it from Docker Hub for us.

Hello-world displays a message telling us everything’s working. Then it spells out the process for us before recommending some additional steps.

Under the covers

Let’s take a look at a few more Docker commands that tell us more about the environment.

docker ps -a lists the containers on our system:

output of docker ps -a

From this, we can see that the hello-world container is still in memory. The status column tells us that it’s exited. The names column has a name, kind_bose, that Docker assigned to the container for us. We’ll cover container names below.

Let’s run this image again with docker run hello-world. The output is almost the same…

output from docker hello world

…except this time we don’t see information about downloading the image. It was already available on our system.

But what does docker ps -a show us now?

output of docker ps -a with 2 containers

We see two stopped instances of hello-world, with two different names. Docker created an additional container. It didn’t reuse the first. When we told Docker to run an image named hello-world, it did exactly that; it ran a new instance of the image. If we want to reuse a container, we refer to it by name.

Reuse a container

Let’s try starting one of the stopped containers:

ouput of restarting hello world container

This time, we used docker start –attach  instead of docker run. We use the start command, and rather than naming the image, we specify the name of a container that’s already loaded. The –attach tells Docker to connect to the container output so we can see the results.

We stop containers with docker stop  and remove them with docker rm . We’ll take a look at that below when we work with applications designed to keep running in the background.

If we check docker ps again, we still see two containers.

Let’s run a container that doesn’t exit immediately. Hello-world’s instructions gave us an interesting example:

output from Ubuntu container

With a single Docker command, docker run -it ubuntu bash, we downloaded an Ubuntu Linux image and started a login shell as root inside it. The -it flags allow us to interact with the shell.

When we open another window and list containers, we see a different picture:

docker ps -a with running container

The Ubuntu container’s status is Up. Let’s see what’s going on inside:

docker top

docker top looks inside the container and shows us the running processes. The Ubuntu container is running a single process—the root shell.

Let’s look at one last Docker command before we create a container of our own:

docker image ls

Docker image ls produces a listing of images on our system. We see Ubuntu and the single hello-world image since we only needed that single image to run two containers.

Share system resources with a container

So far, we’ve run a couple of self-contained images. What happens when we want to share local resources from our host system with a container? Docker has the ability to share both the file system and the networking stack with containers.

Let’s create a web server that serves a web page from the local filesystem. We’ll use a public Nginx image.

First, we need an HTML file to display when we connect to the web server. Start in an empty directory that we’ll call my-nginx and create a single subdirectory named html. Inside html, create index.html:



Hello, World!



We’re ready to go. Here’s our command line:

$ docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx

When we execute this command line, we see Docker download the Nginx image and then start the container.

We used four command line options to run this container:

  • -v /full/path/to/html/directory:/usr/share/nginx/html:ro maps the directory holding our web page to the required location in the image. The ro field instructs Docker to mount it in read-only mode. It’s best to pass Docker the full paths when specifying host directories.
  • -p 8080:80 maps network service port 80 in the container to 8080 on our host system.
  • -d detaches the container from our command line session. Unlike our previous two examples, we don’t want to interact with this container.
  • nginx is the name of the image.

After executing this command, we should be able to reach the web server on port 8080:
Our test page in Chrome
We see our test page! You can also access the page from our devices on your network using your host system’s IP address.

When we ran the Nginx image, we needed to tell it where to get the web files. We did this by mounting a directory on our host system to a directory inside the container, overriding the files that are already inside the image. Docker also supports volumes, which can contain filesystems and be shared between containers.

We also needed to map port 80 in our container to a port on our host system so the web server can communicate with the outside world. Containers don’t automatically have access to the host network. With our port mapping directive, the container can be accessed via the host network. Since we only mapped this port, no other network resources are available to the container.

This exercise illustrates one of Docker’s key advantages: easy reuse of existing resources. We were able to create a web server in minutes with virtually no configuration.

Stop and remove a container

Our web server is still running:

list docker container

We can stop it with docker stop

$ docker stop compassionate_ritchie

…and remove the container with docker rm.

$ docker rm compassionate_ritchie

After running these two commands, the container is gone:

stop and remove docker container

Create a Docker image

Now let’s build on this example to create an image of our own. We’ll package the Nginx image with our html file.

Images are created with a Dockerfile, which lists the components and commands that make up an image.

In my-nginx, create a Dockerfile:

FROM nginx

COPY html /usr/share/nginx/html

This Dockerfile contains two instructions:

  1. First, create this image from an existing image, which is named nginx. The FROM instruction is a requirement for all Dockerfiles and establishes the base image. Subsequent instructions are executed on the base image.
  2. The second instruction, COPY, tells Docker to copy our file tree into the base image, overriding the contents of /usr/share/nginx/html in the base image.

Next, build the image:

$ docker build -t mynginx .

Sending build context to Docker daemon 3.584kB

Step 1/2 : FROM nginx

—> b175e7467d66

Step 2/2 : COPY html /usr/share/nginx/html

—> Using cache

—> a8b02c2e09a4

Successfully built a8b02c2e09a4

Successfully tagged mynginx:latest

We passed two arguments to build:

  • -t mynginx gave Docker a tag for the image. Since we only supplied a name, we can see that Docker tagged this build as the latest in the last line of the build output. We’ll look more closely at tagging below.
  • The final argument, dot (or “.”), told Docker to look for the Dockerfile in the current working directory.

The build output shows Docker using the nginx image and copying the contents of html into the new image.

When we list images, we can see mynginx:
docker image ls with view of our new image

Run a custom image

Next, we run our new image:

$ docker run –name foo -d -p 8080:80 mynginx

Let’s break that command down.

  • –name foo gives the container a name, rather than one of the randomly assigned names we’ve seen so far.
  • -d detaches from the container, running it in the background, as we did in our previous run of Nginx.
  • -p 8080:80 maps network ports, as we did with the first example.
  • Finally, the image name is always last.

Now point your browser at and you can see the test web page again.

While the web server is still running, let’s take a look at docker ps:

docker ps with web app running

We can see that the ports column has the mapping we used to start the container, and names displays the container name we used.

We’ve created a self-contained web server that could easily contain a complete set of web documents instead of only one. It can be deployed on any platform that supports Docker.

Create a more customized image

Each Docker image executes a command when it’s run. In our Nginx Dockerfile, we didn’t define one, so Docker used the command specified in the base image.

Let’s try a slightly more complicated image that requires more setup and a specific command instruction.

Start in another empty directory. This time, we’ll create two new text files.

First, we’ll create a small Python script named

from flask import Flask

import os

import socket

app = Flask(__name__)


def hello():

html = ”

Hello {name}!

Hostname: {hostname}

return html.format(name=os.getenv(“NAME”, “world”), hostname=socket.gethostname())

if __name__ == “__main__”:’′, port=4000)

This script creates a web server listening on port 4000 and serves a small HTML document with a greeting and the container’s hostname.

Next, we’ll create a Dockerfile:

# Use an official Python runtime as a parent image

FROM python:2.7-slim


ADD . /app

RUN pip install –trusted-host Flask


CMD [“python”, “”]

This Dockerfile starts with an image that contains a Python runtime. We can see from the name that it provides version 2.7 in a slim configuration that contains a minimal number of Python packages.

Next, it establishes a WORKDIR (working directory) named /app and ADDs the current working directory to it.

After adding the script to the image, we need to install the Flask Python package, the library we use for the web server. The RUN instruction executes pip install for this. Dockerfiles can run commands as part of the image build process.

Next, it sets the environment variable NAME, which is used in the HTML page returned by

And finally, the Dockerfile specifies the command to run when the image is run. CMD accepts a command and a list of arguments to pass to the command. This image executes the Python interpreter, passing it

Let’s build this image:

$ docker build -t mypyweb .

Sending build context to Docker daemon 4.096kB

Step 1/6 : FROM python:2.7-slim

—> b16fde09c92c

Step 2/6 : WORKDIR /app

—> Using cache

—> e8cfc6466e29

Step 3/6 : ADD . /app

—> Using cache

—> b0ed613be2d4

Step 4/6 : RUN pip install –trusted-host Flask

—> Using cache

—> 255f51709816

Step 5/6 : ENV NAME World

—> Using cache

—> d79d78336885

Step 6/6 : CMD [“python”, “”]

—> Using cache

—> 687bc506dd46

Successfully built 687bc506dd46

Successfully tagged mypyweb:latest

Run our Python image

$ docker run –name webapp -p 8080:4000 mypyweb

Let’s navigate to 8080 again with a browser:

Python web page in Chrome

We see our new web page. We’ve created another portable web server with just a few lines of Python!

Pass environment variables

Our Dockerfile set an environment variable…


…which the Python script uses in this greeting:

html = ”

Hello {name}!

Hostname: {hostname}

We can override this variable from the command line:

$ docker run –name webapp -p 8080:4000 -e NAME=”Dude” mypyweb

Then look at the web page again:

web oage with different greeting

Share an image

As we’ve been running images and using them as the basis for our own, we’ve seen Docker download them from Docker Hub:

Step 1/6 : FROM python:2.7-slim

2.7-slim: Pulling from library/python

b0568b191983: Pull complete

We can upload our own images to Docker Hub for distribution, too.

The first step is to create an account on Docker Cloud. If you don’t already have an account, go and create one.

Next, we’ll log in to the Docker registry:

$ docker login

Username: ericgoebelbecker


Login Succeeded

We’ll upload mypyweb to Docker Hub.

Before we do that, we should tag it. The format for Docker tags is username/repository:tag. Tags and repository names are effectively freeform.

$ docker tag mypyweb ericgoebelbecker/stackify-tutorial:1.00

If we list our images now, we see this tag:


ericgoebelbecker/stackify-tutorial 1.00 0057736e26ce Less than a second ago 150MB

mypyweb latest 0057736e26ce Less than a second ago 150MB

mynginx latest a8b02c2e09a4 41 hours ago 109MB

nginx latest b175e7467d66 4 days ago 109MB

python 2.7-slim b16fde09c92c 3 weeks ago 139MB

Note that our image tag and mypyweb have the same image ID and size. Tags don’t create new copies of images. They’re pointers.

Now we can push the image to Docker Hub:

$ docker push ericgoebelbecker/stackify-tutorial:1.00

The push refers to repository []

7d7bb0289fd8: Pushed

acfa7c4abdbb: Pushed

8d2f81f035b3: Pushed

d99e7ab4a34b: Mounted from library/python

332873801f89: Mounted from library/python

2ec65408eff0: Mounted from library/python

43efe85a991c: Mounted from library/python

1.00: digest: sha256:e61b45be29f72fb119ec9f10ca660c3c54c6748cb0e02a412119fae3c8364ecd size: 1787

docker push accepts a tag name and pushes it to the default repository, which is Docker Hub.

Now, if we visit our account area on, we can see the new repository, the image, and the tag:

Docker Hub Repository Page

If you look closely, you’ll notice a size discrepancy. This is because the image on Docker Hub only contains the changes from the Python:2.7-slim image it’s based on.

We can pull the image down and run it from any system:

$ $ docker run -p 8080:4000 –name webapp -e NAME=”Docker Hub” ericgoebelbecker/stackify-tutorial:1.00

Unable to find image ‘ericgoebelbecker/stackify-tutorial:1.00’ locally

1.00: Pulling from ericgoebelbecker/stackify-tutorial

b0568b191983: Pull complete

55a7da9473ae: Pull complete

422d2e7f1272: Pull complete

8fb86f1cff1c: Pull complete

9b622183190d: Pull complete

cf5af0f3fb51: Pull complete

3292695f8261: Pull complete

Digest: sha256:e61b45be29f72fb119ec9f10ca660c3c54c6748cb0e02a412119fae3c8364ecd

Status: Downloaded newer image for ericgoebelbecker/stackify-tutorial:1.00

* Running on (Press CTRL+C to quit)

This is the output of run on a different system from the one I built on. Similar to the way we ran hello-world, we passed the image tag to docker run. And since the image was not available locally, Docker pulled it from Docker Hub and Python:2.7-slim, assembled the image, and ran it.

We published the image, and it’s now publicly available from Docker Hub.


Docker is a powerful platform for building, managing, and running containerized applications. In this tutorial, we installed the tools, downloaded and run an off-the-shelf image, and then built images of our own. Then we published an image to Docker Hub, where it can be downloaded and run on any Docker-enabled host.

Now that you understand the basics, keep experimenting and see how you can use Docker to package and distribute your applications.

Getting Started With the Swift iOS Feature Flag

This is another post I wrote for the Rollout blog. You can find the original right here.

This post was a lot of fun to write since I hadn’t done any IOS development in a few years.

Mobile users expect more. They want their applications to be as stable as yesterday while looking and acting more and more like tomorrow. How do you keep up? How can you reliably test and push new features without risking a disastrous release?

Feature toggles (also known as feature flags) are one way. They provide app developers with a way to add new behavior to an application and then enable or disable it without deploying a new release.

So how do feature toggles work? Add some new behavior to the application, wrap it in a boolean, default it to “off,” and then flip it when it’s safe, right?

There’s more to it than that. Effectively using feature flags requires understanding feature flag strategy and tactics.

I’ll get you to your first iOS feature flag quickly, and then give you a deeper understanding of feature flags and how to avoid creating technical debt when you are using them.

This tutorial assumes familiarity with basic iOS concepts and the ability to set up and use Xcode. While I’ll use an iPhone simulator for testing the application, you can use whatever method you feel most comfortable with. I’ll be using the latest version of Xcode, which is 9.3.1 at the time of this writing, and Swift 4.1.

The source code for the completed project is available here.

A Basic Feature Toggle

Let’s start with the basic single view iOS application offered by Xcode 9.

select xcode project for feature flags

We’ll add a Label to the application’s single view like below:

Add label to Swift feature toggle application

We’ll add the label to the viewDidLoad() method of our ViewController, with the name GreetingLabe, and assign a greeting to it.

We could set the default value in the UILabel definition if we want, but we’ll define it explicitly throughout the tutorial to make the behavior easy to see.

We don’t need to make any further changes to the sample code. Let’s build and run the application.

Test Swift iOS application


We have a basic iOS app to work with. We’ll use feature toggles to modify the application’s greeting.

An iOS Feature Toggle

So let’s imagine we want to replace our greeting with a different one depending on the season.

A fundamental mechanism would be a boolean flag.

When we build and run this version we see the new greeting below:

Feature toggle in Swift iOS application

And we have our first feature toggle!

But this means modifying, building, and getting a release through the App Store whenever we want to change our greeting.

Modifying a configuration file would be a bit simpler and shave a little bit of time and effort off the release cycle.  Let’s create a property list and add our toggle in the form of a configuration property to it.

Feature flag in iOS plist

We have a property list named featureflag.plist. In it, there is one parameter: a boolean flag named isNewYear.

We’ll load it and use the property to set our flag. We’ll load the file in the controller, even though that’s not what we would do in production code, to keep things simple.

Now we can toggle the value of isNewYear to YES or NO via the properties file, and we’ll see the proper greeting.

The plist gets us out of modifying code when we want to toggle a feature, but we still need to build and release.

Let’s look at a better way to change, and more importantly, manage feature toggles.

Getting Started With Rollout

First, you’ll need to create a free Rollout account here.

Once that’s done, sign in to your Rollout account and create a new application.

Rollout new Feature Toggle iOS Application
Be sure to select iOS, as shown above.

Next, you’ll see instructions for adding Rollout to your application.

Install feature toggle api iOSFirst, download, unzip it, and add the rox directory to your project’s directory tree.

Next, add RoxSdk.xcodeproj and ROXCore.framework to your project.

Then, select Add Files to… from the Project Navigator.

add files to projectNow, select the files from the RoxSdk directory in the zip file output.

select rollout files


You’ll see ROXCore.framework icon in the Project Navigator. Add those files.

Next, we need to remove the framework from the project’s linked frameworks. Select your project’s name at the top of Navigator tab.

Select feature flag project



And scroll down in the application information page to Linked Frameworks and Libraries.

Xcode linked frameworks



Next, select ROXCore.framework and then click the minus to remove it.

We want to add it back to embedded binaries. Click the plus in Embedded Binaries.

embedded binaries

Then, select ROXCore.framework and ROX.frameworkiOS and click Add.

Last, we need to set up a build step to strip out unneeded libraries from our production application.

First, add a script build phase:

Add script

Then locate the script:

Locate script

And drag it into the script target:

Drag in script

Initialize the API

The Xcode project is ready now, and we can initialize the API.

Finally, click Setup SDK back at the Rollout dashboard.

Setup Feature Toggle SDK

Add the import ROX declaration at the top of AppDelegate.swift.

ROllout assigned a unique initialization key to our application. Add ROX.setup() to didFinishLaunchingWithOptions. The top of AppDelegate.swift should look similar to this:

Now we can run the application to initialize Rollout. Click Activate App on the Rollout dashboard. After the application builds and runs, you are rewarded with this:

Feature flag success

Add a Managed Feature Flag

Let’s create a feature flag and register it with Rollout. If we look at our Rollout dashboard and check flags, we see this:
Create Spring Feature Toggle image
There are no flags, and there is no way to add one. That’s because we create them from our application, not from the dashboard.

In Swift, feature flags are RoxFlags, and are managed in RoxContainers.

First, add this class to the project:

We’ve created a single flag named isNewYear with a default value of true.

We register the container with the API before calling Rox.setup(). Make this change to AppDelegate:

We’re registering the container with Rollout. The first argument to register() is a name that rollout will associate with it.

Run the application and then go to the Flags menu item in the Rollout dashboard.

Feature Flag in Rollout DashboardThe new flag is there. We see the container name and the flag name in the dashboard.

Reading a Managed Feature Flag

Next, we need to update our code to use the RoxFlag instead of the property list.

Rollout retrieves feature flag values asynchronously and caches them. It extracts the latest value from the cache on startup while initiating a fetch from the network in parallel.

This means that the first time we run our application, we will immediately get the default value for our flag (false for toggles unless we override), and then the value set at Rollout upstream will be retrieved sometime later.

Since we want to see the value of the flag before we load our view, we need to do a little bit of extra work. This will also teach us a bit more about the Rollout API.

We’re going to add something to our call to ROX.setup().

First, we create a RoxOptions. This object holds configuration settings that we can pass to the API when we initialize it. One of those options, onConfigurationFetched, is a callback for when the latest configuration is successfully fetched from Rollout.

When our callback is executed, we use NotificationCenter to notify any interested objects that new information has been retrieved.

There are a few advantages to using NotificationCenter instead of trying to call our view directly:

  • Since this callback will be called on startup, our ViewController probably hasn’t been created yet.
  • If we add more controllers to the application, later things will start to get complicated.
  • Threading issues are delegated to listeners (as we’ll see).

Next, we need to make some changes in ViewController.swift.

ViewDidLoad() is now subscribing to update events and delegating them to a method named UpdateLabels()

We moved setting the value of our label to UpdateLabels(). In it, we get a reference to AppDelegate so we can access its copy of the flags and check them. However, we have to dispatch this task to the main thread because only the UI thread can modify the view.

We check the value of isNewYear and set the label appropriately. RolloutFlags() also has an enabled block, but since we need to set the label back if the flag is toggled from “on “to “off,” the if/else logic makes more sense.


Managing a Feature Flag

We manage flags by adding them to experiments. An experiment is a scheme for controlling flags (and other variables) in production.

First, click on Production in the left-hand side menu and then click Experiments. This will bring up a screen with a Create Experiment button. Click that and fill out the new experiment window appropriately.

Select Set Audience.


And we see a console for setting flags to true, false, or split. Leave it set to False.

If we run our application now, we see “Hello, World!”

Next, change it to True and restart the application.

Feature toggle in Swift iOS application

We see the Happy New Year greeting!

Flip it back to false and click the phone’s home button. Then tap on the application icon to open it again. It flips back to Hello World! 

We can change the behavior of the application without touching code and releasing a new version. We can even alter it while the application is running on a client’s device.

Before we move on, let’s take a look at the experiment on the console again. Flip the flag from true to split.

Split Settings for a Feature Toggle

This option allows us to distribute a change to a percentage of users, hence the term “experiment.” If you play with it now, you’ll need to completely start and stop the application to see different settings to overcome the API’s caching.

This is the power of feature flags!

Extra Credit: Configurations

Tying the contents of a label to a true/false flag is an admittedly contrived example, but it kept the tutorial code focused and simple.

Let’s take a look at another mechanism for setting a label. Rollout supports managing configuration values from the dashboard.

Let’s add a new field to Flags.

RoxConfigurationString is exactly what it sounds like.

Build and run the application again, and then go back to the Rollout dashboard.

Under Production and Configurations, we see our new value.

Feture toggle configuration

If we click on it, we can set it to a value like so:

set feature toggle value


Select >= for the criteria, and enter Happy New Year! for the value.

Then we need to make a small code change. Change updateLabels() to use the value of our new member:

And run the app. We see Happy New Year!

Let’s change it.

Set Feature Toggle to w00t

Close and open the application. No need to restart or rebuild: our notification mechanism with handle the change.

Feature toggle developers say w00t!

We can change our application config with Rollout!


This guide demonstrates how to get started with Rollout with iOS and Swift. The API supports any iOS platform that runs Swift, including Apple TV. Rollout’s documentation has details on how you can do a great deal more with flags, configuration settings, experiments, versions, and grouping users.

You now have an understanding of feature flag management and how it can improve your iOS code.  You’ve seen how it can help you manage projects and eliminate unnecessary releases. Get to work!

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑