├── README.md ├── exercise-0.md ├── exercise-1.md ├── exercise-2.md ├── exercise-3.md └── exercise-4.md /README.md: -------------------------------------------------------------------------------- 1 | # Docker hands-on workshop 2 | 3 | **NOTE: Docker moves fast and this tutorial is now out of date. It is kept here as a reference to students who attended this workshop.** 4 | 5 | This two hour workshop will take you from "Hello Docker" to deploying a containerized web application to a server. 6 | 7 | ## Prerequisites 8 | 9 | There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor. 10 | 11 | It won't make much sense, though, unless you have some experience deploying and maintaining software applications in production. 12 | 13 | ## Philosophy 14 | 15 | **There are a few things you should know about this tutorial before you begin:** 16 | 17 | This tutorial is designed to be **self-paced** to make the most of your time. 18 | 19 | **The exercises won't always tell you exactly what you need to do.** Instead, I will try to point you to the right resources (documentation, blog posts, etc.) to find the answer. I prefer this approach because it helps you learn how to find answers about Docker questions you have in the future. 20 | 21 | **Please don't be shy about asking questions to the instructors or your neighbors!** We're all friendly here, and it wasn't long ago that all of us were Docker beginners. :grin: 22 | 23 | ## Get started 24 | 25 | Ready to begin? Head to [Exericse 0](exercise-0.md). 26 | -------------------------------------------------------------------------------- /exercise-0.md: -------------------------------------------------------------------------------- 1 | # Exercise 0: Install Docker 2 | 3 | The first thing you need to do is install Docker on your laptop! Follow the instructions in the section for your operating system. 4 | 5 | ## Linux 6 | 7 | Check out the list of Linux distributions here: https://docs.docker.com/installation/. Find yours and follow the instructions to install Docker. 8 | 9 | For today, it's probably okay if you're running the version of Docker that's part of your distributions package manger. In the future, though, you should consider adding the official Docker repository to make sure you get the latest version of Docker. 10 | 11 | **While Docker is dowloading and installing, you can continue to [Exercise 1](exercise-1.md).** 12 | 13 | ## Mac OS X 14 | 15 | A handy open source project called [boot2docker](https://github.com/boot2docker/boot2docker) makes it easy to run Docker on OS X and Windows. 16 | 17 | Download the file at this URL to install the latest version of boot2docker for OS X: https://github.com/boot2docker/osx-installer/releases/latest 18 | 19 | After you have installed boot2docker, run these commands in a terminal window: 20 | 21 | ```bash 22 | $ boot2docker init 23 | $ boot2docker start 24 | $ $(boot2docker shellinit) 25 | ``` 26 | 27 | **While Docker is dowloading and installing, you can continue to [Exercise 1](exercise-1.md).** 28 | 29 | ## Windows 30 | 31 | A handy open source project called [boot2docker](https://github.com/boot2docker/boot2docker) makes it easy to run Docker on OS X and Windows. 32 | 33 | Follow the instructions here to install boot2docker on your machine: https://docs.docker.com/installation/windows/ 34 | 35 | **While Docker is dowloading and installing, you can continue to [Exercise 1](exercise-1.md).** 36 | -------------------------------------------------------------------------------- /exercise-1.md: -------------------------------------------------------------------------------- 1 | # Exercise 1: Docker basics 2 | 3 | This exercise will make sure you understand the fundamental concepts of Docker. You don't need to have Docker installed to complete this exercise. 4 | 5 | ## What is Docker? 6 | 7 | Before we dive into Docker and get our hands dirty, you should know a little about how Docker works - especially how Docker is different than Virtual Machines. 8 | 9 | > If you have attended one of my "Introduction to Docker" talks before, you already have the background you need. You can skip to the next section. 10 | 11 | You can start by reading the official ["What is it?"](https://www.docker.com/whatisdocker/) page on Docker.com. 12 | 13 | If you want more information, you can check out the ["What is Docker?"](http://player.oreilly.com/videos/9781491916179?toc_id=197359) chapter from my [O'Reilly video tutorial](http://shop.oreilly.com/product/0636920035732.do), which is available for free. 14 | 15 | ## Docker commands crash course 16 | 17 | There's no better place to write your first Docker commands than with the official Docker tutorial: https://www.docker.com/tryit/ 18 | 19 | It will only take you a few minutes to complete it, but you'll get a taste of the main commands you will use for the rest of this tutorial. 20 | 21 | ## Dockerfiles 101 22 | 23 | Dockerfiles are a very important Docker concept. They allow you to easily compose and modify your own custom Docker images. 24 | 25 | Skim [DigitalOcean's Dockerfile primer](https://www.digitalocean.com/community/tutorials/docker-explained-using-dockerfiles-to-automate-building-of-images). Don't worry if you don't understand it all - just try to get the gist of it. 26 | 27 | You're now ready to move to [Exercise 2](exercise-2.md) as soon as Docker is installed on your laptop. 28 | -------------------------------------------------------------------------------- /exercise-2.md: -------------------------------------------------------------------------------- 1 | # Exercise 2: Fire it up! 2 | 3 | It's time to start your first Docker containers! This exercise will start with "Hello World" and end with some more advanced Docker commands. 4 | 5 | **I won't provide all the answers here. To complete this (and future) exercises, you will need to use the [Docker documentation](https://docs.docker.com/) and your intuition!** 6 | 7 | ## "Hello Docker!" 8 | 9 | Pull down the `busybox` image from the Docker Hub. You will need to use the [docker pull](https://docs.docker.com/reference/commandline/cli/#pull) command to do so. 10 | 11 | Once you've done that, start a new container from that image which runs the command `echo Hello Docker!`. You will need to use the [docker run](https://docs.docker.com/reference/commandline/cli/#run) command. 12 | 13 | If your container returns the string `Hello Docker!` to your terminal, then you succeeded! 14 | 15 | ## A basic webserver 16 | 17 | Pull down this Docker image from the Docker Hub: `atbaker/nginx-example` 18 | 19 | This Docker image uses the [Nginx](http://nginx.org/) webserver to serve a static HTML website. 20 | 21 | Start a new container from the `atbaker/nginx-example` image that exposes port 80 from the container to port 8000 on your host. You will need to use the `-p` flag with the docker run command. 22 | 23 | Open a web browser and go to http://localhost:8000 (see below if on Mac or Windows). If you see "Hello Docker" then you're done! Press control + c in your terminal window to stop your container. 24 | 25 | **Note that if you're on a Windows or Mac, exposing your container to a port on your "host" actually exposes that port to the boot2docker virtual machine.** In a different terminal window, use the command `boot2docker ip` to get the virtual machine's IP address. Use that IP address instead of localhost to access your container. 26 | 27 | ## More advanced docker commands 28 | 29 | Before you go on, use the [docker command line interface](https://docs.docker.com/reference/commandline/cli/) documentation to try a few more commands: 30 | 31 | - Start the same container exposing the same port, but run the container in "detached" mode, so that it doesn't tie up your command line 32 | - While your detached container is running, use the `docker ps` command to see what silly name Docker gave your container 33 | - While your detached container is still running, look at its logs. Try following its logs and refreshing your browser. 34 | - Stop your detached container. Then delete that container from your system. 35 | 36 | When you're ready to create your own Docker images, move on to [Exercise 3](exercise-3.md)! 37 | -------------------------------------------------------------------------------- /exercise-3.md: -------------------------------------------------------------------------------- 1 | # Exercise 3: Working with images 2 | 3 | This exercise starts off with exploring a Docker image's history, checks out an easy way to get a Postgres database, and ends with your first Docker image on the Docker Hub. 4 | 5 | **I won't provide all the answers here. To complete this (and future) exercises, you will need to use the [Docker documentation](https://docs.docker.com/) and your intuition!** 6 | 7 | ## Anatomy of an image 8 | 9 | Docker images are just portable, saved states of Docker containers. They're how you move your Dockerized applications from your development machine to the world. 10 | 11 | Let's examine the Docker image we used in the last exercise, `atbaker/nginx-example`. Look at the commands used to create that image with the `docker history` command. 12 | 13 | If you're curious about how Docker images work, you can read more about it here: https://docs.docker.com/introduction/understanding-docker/#how-does-a-docker-image-work 14 | 15 | ## The fastest database installation you'll ever do 16 | 17 | Pull down the `9.4` tag on the `postgres` image from the Docker Hub. While it's pulling down, learn more about it on its [official page on the Docker Hub](https://registry.hub.docker.com/u/library/postgres/). 18 | 19 | Start a new container from the Postgres database with the docker run command. Watch with delight as you start a new database service in seconds. 20 | 21 | This is one reason why using Docker in development environments is so fun. You can get virtually any programming language or any server appliance running on your machine in minutes. And they won't clutter up anything else on your system. 22 | 23 | ## Creating your first image 24 | 25 | Pull down the `atbaker/redis-example` image. This image contains a [redis](http://redis.io/) server. 26 | 27 | As it is right now, anyone can connect to our redis server when it's running. In this exericse, we will add a password to the redis server's configuration to make it more secure. 28 | 29 | Start a new container from that image running the `bash` command so you can get an interactive terminal inside that container. 30 | 31 | **NOTE:** You will need to pass two additional flags to the docker run command to get it to launch an interactive terminal session. 32 | 33 | Using the nano command-line text editor (unfortunately you cannot install any others in this image), edit the file `/usr/src/custom-redis.conf`. 34 | 35 | Uncomment the `requirepass` line in the file (you can optionally change the password provided). Then save the file, exit your text editor, and use control + d to end your shell session in the container. 36 | 37 | Now that we've added a password to our Redis server in a container on our laptop, we want to use that updated Redis server somewhere else - maybe on a production in the cloud. To make the changes we just made in our container portable, we're going to create a new image. 38 | 39 | Use the `docker ps -l` command to see the most recently run container. Copy the container id (or the silly Docker name) of that container. 40 | 41 | Make a new commit using the docker commit command. Make sure the last argument in your command is `redis-password` - that will be the name of our new image. Reference the [documentation](https://docs.docker.com/reference/commandline/cli/#commit) if you need a hint. 42 | 43 | ## Pushing to the Docker Hub 44 | 45 | Congratulations, you're the parent of a new Docker image! The best place to share that image with the world is the Docker Hub. 46 | 47 | Create an account on the Docker hub by running the `docker login` command. 48 | 49 | Once logged in, use the [docker tag command](https://docs.docker.com/reference/commandline/cli/#tag) to put our `redis-password` image inside your Docker Hub namespace. If I was doing it for my account, I would run: 50 | 51 | ``` 52 | docker tag redis-password atbaker/redis-password 53 | ``` 54 | 55 | After that, all we need to do is push our image up to the Docker Hub! Do that with the Docker push command: 56 | 57 | ``` 58 | docker push [your namespace]/redis-password 59 | ``` 60 | 61 | Now your new image is available for anyone to use! https://hub.docker.com/u/ 62 | 63 | Once you're done with that, you can wrap things up with [Exercise 4](exercise-4.md). 64 | -------------------------------------------------------------------------------- /exercise-4.md: -------------------------------------------------------------------------------- 1 | # Exercise 4: Dockerfiles and deploying 2 | 3 | In this exercise, you'll combine everything you've learned so far to Dockerize an app from scratch and deploy it to the cloud. 4 | 5 | **I *really* won't provide all the answers here. To complete this exericse, you will need to use the [Docker documentation](https://docs.docker.com/) and your intuition!** 6 | 7 | ## Dockerizing a sample application 8 | 9 | Clone or download this Git repository: https://github.com/atbaker/nginx-exercise. It's essentially an un-Dockerized version of the Nginx server we used in Exercise 2. 10 | 11 | Using the instructions from [Docker's User Guide](https://docs.docker.com/userguide/dockerimages/#building-an-image-from-a-dockerfile), fill in the blanks in the `Dockerfile` that builds this repository. 12 | 13 | You will need to reference the [Nginx installation instructions](http://nginx.org/en/linux_packages.html) as well. The extra configuration file you need for the `CMD` part of the Dockerfile is: 14 | 15 | ``` 16 | CMD ["nginx", "-g", "daemon off;"] 17 | ``` 18 | 19 | When your Dockerfile can successfully build the image, test it by starting a new container from it. If it works, push that image up to the Docker Hub. 20 | 21 | ## Running your container in the cloud 22 | 23 | DigitalOcean is the best hosting service for starting a Docker server quickly. 24 | 25 | If you don't have a DigitalOcean account, you can get $10 in free credit using this link: https://www.digitalocean.com/?refcode=f93f75043f14 26 | 27 | Start a new DigitalOcean server (droplet) by clicking "Create Droplet" after logging in. Under the "Select Image" section, choose the "Docker on 14.04" image. 28 | 29 | Once your server has booted, SSH into it using the password DigitalOcean sends you. Then use the `docker pull` command to pull down your Dockerized Nginx image from the previous section. 30 | 31 | Start a new container exposing port 80 from the container to port 80 on the host, and then visit your DigitalOcean server's IP address in your browser. 32 | 33 | Then, bask in the glory of a successful deployment. 34 | --------------------------------------------------------------------------------