Developer Automation Tools Bootcamp

Learning about development tools and automation

View project onGitHub

Introduction to Docker


Let's start with a little Q & A.

Answer: Docker. Now - what's the question?

"Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications." Docker is open source.

Official What is Docker
Or even better - Understanding Docker

Docker Basics

Getting started with Docker requires a few things:

  1. Install Docker on your local machine with Docker for <YOUR PLATFORM>. Or if you've previously installed Docker you might have the Docker Toolbox.
  2. Pick a container with your favorite technology from the Hub to run.
  3. docker run --name automation-tools-web -d -p 8080:8080 nginx to start things up.
Your image is downloaded, deployed and running as a container.

Did you think there'd be more?

Docker Hub/Repositories

Docker's ability to find public containers is based on using the Docker Hub. But you can run private registries as well, so you can keep your Docker images inside your firewall/data center. Check out Artifactory to be your Docker Registry (and npm cache and Ruby Gem repo and Maven repo). Artifactory is very helpful.

Application developers will maintain their own Docker images, so you just need to overlay your application code. This can rapidly speed your development and deployment cycles. When an application is updated, the provider will update their Docker image and you'll get the new version of the app.

That said don't go trusting images from out in the wild.

  • Test
  • Verify
  • Test each release
  • Dig in to their Dockerfile
  • Know the commit history
  • Or...
  • Building your own Docker image

    A Dockerfile will generate your own Docker image for you with your applications and even your code installed.

    Docker image history is similar to a project's Git history. Docker images start from a read-only base image and then have (usually) small diffs as they are versioned. You can view the changes with the docker history <image> command.
    The pieces of a Dockerfile:

    • FROM - which image your build container will begin form.
    • MAINTAINER - you. Own your work!
    • RUN - Run this command on the build container. A Dockerfile may have mulitple RUN lines. Poor man's Config Management (unless you RUN chef-client ;).
    • WORKDIR - akin to using cd. Subsequent commands are exec'd in the WORKDIR
    • ADD - adds files and directories from our build context into our image. Can unpack archived files.
    • COPY - like ADD but without extraction or decompression capabilities. Both COPY and ADD will also make parent directories if they don't already exist.
    • EXPOSE - any ports you need opened up into the container. The docker command will later manage port mapping with the -p option at runtime.
    • CMD - command that will run when the container starts. Can be overridden from the command line.
    • ENTRYPOINT - like CMD but *cannot* be overridden, although options can be passed to the ENTRYPOINT.

    Other pieces of a Dockerfile, which aren't applicable to our work today:
    • VOLUME - create a volume on the container. Often with data from the host. Can be shared with multiple containers.
    • USER - specifies a user the image should be run as.
    • ONBUILD - executes a trigger (e.g. ADD or COPY) when the image is used as the basis for another image.
    • ENV - set environment variables during image build process.

    The other pieces of Container infrastructure

    So Docker is fast and makes it easy to deploy a container. Are their any downsides? Well, not necessarily downsides, but there are lots of moving parts to a production Docker environment.

    Alas, there are numerous topics on that list which could individually be covered here in their own topics. Just know that there are people successfully using Docker/containers in production. You can do it too.

    Learn more Docker

    This is the tip of the iceberg for Docker. Go forth and learn more!

    Workout

    We are going to complete the following actions:

    1. The VM for our class has Docker installed.
    2. Make sure you have run a vagrant destroy on any VMs from the Vagrant section.
    3. From the root level of the ATB git repository there is a Vagrantfile which will spin up the VM we will use the rest of the day.
    4. If you are using a local copy of the atb-lisa-2017-1.0.0_vbox.box VM change the comments in line 21-23 of that Vagrantfile to reference you local copy of class VM instead of downloading it over the Internet.
    5. Now - vagrant up and we're ready to begin.
    6. In the VM cd /vagrant/docker which will put you in to the Automation Tools git repo's docker directory with the Dockerfile to be completed by you.
    7. Make yourself the MAINTAINER of the Dockerfile.
    8. You'll want to EXPOSE port 8085 to view the sample app.
    9. Create a directory using RUN to hold our application mkdir -p /usr/src.
    10. Change the WORKDIR to /usr/src/ which will make it the directory where we will check out our example app.
    11. Use RUN to git clone our example app from Github from https://github.com/automation-tools-bootcamp/example-nodejs-app.git in to the container.
    12. WORKDIR again to the directory (/usr/src/example-nodejs-app) where you checked out the repo.
    13. RUN npm install which is the node.js command to install all the app's dependencies.
    14. Final thing for the Dockerfile: Use CMD to start the app with CMD [ "npm", "start" ].
    15. Make a docker build of your new image. Be sure to tag it -t atb when you do. docker build -t atb ./
    16. Now it's time to docker run that new image as a container. docker run -it --rm -p=8085:8085 atb.
    17. View the app in your browser.
    18. Since the container is not daemonized (which you'd do with the -d switch, instead of -it switches), use Ctrl-C to kill npm start and exit the container.

    Bonus Workout

    If you're really feeling empowered, you have the option to complete the following actions to push your container to the Docker Hub:

    1. If you do not already have a Docker Hub account, create a Docker Hub account so you can push your finished container to the Docker Hub.
    2. Tag our atb container with your Docker Hub userID. docker tag atb <YourDockerHubID>/atb
    3. Make sure you are logged in Docker Hub by running docker login.
    4. Use docker push <YourDockerHubID>/atb to upload your image to the Docker Hub.