First hands on with Docker

stacks of large shipping containers in storage yard

Why learn Docker?

For me, learning about containers (and Docker in particular) is just part of my journey to keep up to date on the tools of modern software development. I don’t anticipate becoming a container expert or managing large Docker swarms (although you never know!), but having a decent understanding of the tools and technologies around containers fits into my process of using the best tools available for a given problem. I am fascinated to learn new ways to accomplish a task, not to mention that using containers for software development seems to be an idea that is sticking around. With that in mind, let the learning begin!

What exactly is Docker?

I won’t even pretend to give a better executive summary of containers and Docker than Docker themselves do, so I will spare you the attempt.

For a good overall summary of what Docker (and containers and images) is, I headed to the official Docker documentation, and was quickly up to speed on the overall benefits and high level idea behind containers.

Install Docker

While I primarily develop on Windows, for this project I wanted to utilize Linux as forced way to brush up on other skills that often do not get enough attention due to time constraints. I happened to have a Linux instance available from a previous project, so I fired it back up and connected to it via Windows Subsystem For Linux and SSH.

I installed the Docker CE (Community Edition)

My Linux install was Ubuntu based (Amazon Linux AMI), so make sure to use the proper instructions for your version of Linux.

Instructions I followed for Docker

Uninstall old versions of Docker

Don’t skip this step! I had a previous version of Docker installed (without knowing what I was doing), so I wanted make sure that was cleaned up first. If nothing was previously installed, nothing bad will happen.

Install and Confirm

Once I cleaned up my old install of Docker, I was able to successfully reinstall the current version of Docker CE, add my user to the docker group, and confirm the running status of Docker. Success!!

Create a Docker image

I ended up following the AWS introduction guide to Docker and created a ‘Hello World’ image that served a static web page from an instance of Apache. The tutorial was pretty straightforward, I only needed to tweak my AWS security group for the EC2 instance to allow for inbound connections on port 80 from my machine.

Dockerfile

FROM ubuntu:16.04

# Install dependencies
RUN apt-get update
RUN apt-get -y install apache2

# Install apache and write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html

# Configure apache
RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh
RUN echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh
RUN echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh
RUN echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh
RUN chmod 755 /root/run_apache.sh

EXPOSE 80

CMD /root/run_apache.sh

Build docker image

docker build -t hello-world .

Verify image

docker images --filter reference=hello-world

Run image

docker run -t -i -p 80:80 hello-world

I added the -t -i flags to allow Ctrl+C to be used to interrupt docker run. See this GitHub issue for a more complete discussion.

test!

Summary

I obviously still have lots to learn, but this first look at Docker was relatively painless and has already given me a whole set of ideas to try out and incorporate into my development processes.


Featured image by Tim Easley @ unsplash.com