Exercise 1: Fire it up!¶
It’s time to start your first Docker containers! This exercise will start with a different version of “Hello World” and end with some more advanced Docker commands.
Join the chat room for this exericse: https://gitter.im/atbaker/oscon-exercise-1
I won’t provide all the answers here. To complete this and future exercises, you will need to use the Docker documentation and your intuition!
In the world of Docker, work gets done inside containers. Let’s start by running a very simple command inside a very simple container.
docker pull command uses this basic syntax:
docker pull IMAGE_NAME
Once you have the
busybox image on your laptop, start a new container from that image which runs the command
echo Hello Docker!. You will need to use the docker run command.
docker run command uses this basic syntax:
docker run IMAGE_NAME COMMAND
If your container returns the string
Hello Docker! to your terminal, then you succeeded! Congratulations on your very first Docker container.
Before moving to the next section, try starting another container from the
busybox image with a different Linux command.
telnet towel.blinkenlights.nl is always a fun one, but you will need to pass an extra option to the
docker run command to allocate a TTY to the container.
Press control + c to stop the container when you’re ready.
A basic webserver¶
Running arbitrary Linux commands inside a Docker container is fun, but let’s do something more useful.
Pull down the
atbaker/nginx-example Docker image from the Docker Hub. This Docker image uses the Nginx webserver to serve a static HTML website.
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.
Mapping ports between your host machine and your containers can get confusing. Here is the syntax you will use:
docker run -p 8000:80 IMAGE_NAME
The trick is to remember that the host port always goes on the left, and the container port always goes on the right.
I like to think of it from the perspective of an incoming request, which would first go through port 8000 on your host and then be forwarded to port 80 on your container.
Open a web browser and go to port 8000 on your host. The exact address will depend on how you’re running Docker today:
- Native Linux - http://localhost:8000
- Mac or Windows - Run
docker-machine ip defaultto get your hostname. Ex: http://192.168.99.100:8000
- Cloud server - Use the hostname from column A of the Google Spreadsheet. Ex: http://ec2-54-69-126-146.us-west-2.compute.amazonaws.com:8000
If you see “Hello Docker” then you’re done! Press control + c in your terminal window to stop your container.
More advanced docker commands¶
Before you go on, use the Docker command line interface documentation to try a few more commands:
- 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
- While your detached container is running, use the
docker pscommand to see what silly name Docker gave your container. This is one command you’re going to use often!
- While your detached container is still running, look at its logs. Try following its logs and refreshing your browser.
- Stop your detached container. Then delete that container from your system.
When running most docker commands, you only need to specify the first few characters of a container’s ID.
For example, if a container has the ID
df4fd19392ba, you can stop it with
docker stop df4. You can also use the silly names Docker provides containers by default, such as
Who needs Node.js?¶
One area where Docker shines is when you need to use a command line utility that has a large number of dependencies.
Take a quick look at the gifify project. It’s a Node.js tool that creates gifs from movie files. gifify is easy to use and produces really efficient GIFs, but to use it, we would need to:
- Install Node.js and npm
- Install the gifify package
- Install FFmpeg
- Install convert (an ImageMagick utility)
- Install giflossy, a fork of gifsicle
That’s a lot of dependencies just to make a gif on the command line. Fortunately, someone out there already made a Docker image with all those dependencies installed for us.
Pull down the
maxogden/gifify image. While it’s pulling, open a web browser and go to the New York Times Video account on Instagram.
$("video").attr('src') - the result is the URL of your video.
If the above jQuery code doesn’t work for you, try:
Use a tool like
curl to pull that video down to your laptop and name it
nyt.mp4. For example:
curl -o nyt.mp4 https://scontent-ord.cdninstagram.com/hphotos-xaf1/t50.2886-16/11078985_985322351492341_1000709153_n.mp4
wget -O nyt.mp4 https://scontent-ord.cdninstagram.com/hphotos-xaf1/t50.2886-16/11078985_985322351492341_1000709153_n.mp4
Or just open the URL you copied in a new browser tab and download the file with the File -> Save Page As... menu.
Now it’s GIF time!
To share your movie file from your laptop to a Docker container, we will need to use the volumes option. You will learn more about them later - for now, just run this command in the directory with your
nyt.mp4 video file:
docker run -it --rm -v $(pwd):/data maxogden/gifify nyt.mp4 -o output.gif --to 5 --compress 0 --colors 255 --resize 320:-1
It will take a couple minutes, but eventually your container will add a new file,
output.gif, to your current directory.
If you are using one of my cloud servers, you can’t view your glorious gif through the terminal session. I used the Imgur API to create a command line tool that automatically uploads your gif to Imgur so you can see it.
The command line tool is already installed on your cloud server. Run this command to upload your gif to Imgur:
After a moment, you should receive a link to your uploaded gif.
If you made a good gif, maybe you can tweet it at @soyeahdjango :P.
When you’re ready to create your own Docker images, move on to Exercise 2: Docker images!