September 20, 2013

Beginner

How To Use the DigitalOcean Docker Application

Tagged In: Ubuntu, Docker



An Article From Docker
Submitted by Andy Rothfusz

Introduction


So you’d like to try out Docker to create and run some services as containers? Thanks to DigitalOcean’s one-click “application” installation, you can create a droplet with Docker pre-installed, running and ready to go!

Docker in a Droplet


Start by creating a new Droplet. Under Select Size, 512MB is enough for the basic Docker exercises we’ll present here.

As you prepare to Select Image, choose the Applications tab instead of a Linux Distribution:

docker

See the Docker on Ubuntu 13.04 “application”? Select that, and you’ll create a droplet where all the steps for installing Docker on Ubuntu 13.04 are already complete. It’s a real time-saver and lets you dive right into trying out Docker.

Open a shell to your droplet and log in as root. docker -d is already running as a service and the docker command line tool ready to use on your PATH. You’re ready to start working with Docker and Linux Containers! You could jump over to the Docker Examples or stay here and we’ll walk through a few little exercises and give some hints on configuring your DigitalOcean Droplet for easiest Docker use.

Step 1: Gratuitous Hello World


Let’s run the traditional “Hello World”, Docker-style. Here my droplet is named “test” and I’m logged-in as root:

root@test:~# docker run ubuntu echo "Hello World"
Unable to find image 'ubuntu' (tag: latest) locally
Pulling repository ubuntu
8dbd9e392a96: Download complete
b750fe79269d: Download complete
27cf78414709: Download complete
Hello World

What happened there? Docker tried to start a Linux container from an image named “ubuntu” but couldn’t find it locally, so it automatically went to the Docker Central Registry to see if it could find “ubuntu” listed there. It did find the image which contains the whole “ubuntu” root file system (that’s part of containerizing all the dependencies for software). It pulled the image to your droplet (~180MB), started a container, and executed echo “Hello World in a shell, and the container exited. The standard output was captured by the Docker daemon and streamed to the terminal.

What happened to that “ubuntu” image? It is now cached on your local droplet. You’ll never need to download it again on this droplet, and it can become the base for many more images without creating multiple copies of itself (Docker uses an efficient layered file system). Here’s how you check the images available locally:

root@test:~# docker images
REPOSITORY          TAG                 ID                  CREATED             SIZE
ubuntu              12.04               8dbd9e392a96        5 months ago        131.5 MB (virtual 131.5 MB)
ubuntu              latest              8dbd9e392a96        5 months ago        131.5 MB (virtual 131.5 MB)
ubuntu              precise             8dbd9e392a96        5 months ago        131.5 MB (virtual 131.5 MB)
ubuntu              12.10               b750fe79269d        5 months ago        24.65 kB (virtual 180.1 MB)
ubuntu              quantal             b750fe79269d        5 months ago        24.65 kB (virtual 180.1 MB)

In fact, the writable file system layer used by the temporary container which echoed “Hello World” is still around too:

root@test:~# docker ps -a
ID                  IMAGE               COMMAND             CREATED             STATUS              PORTS
3d1ffd01d2f2        ubuntu:12.04        echo Hello World     4 seconds ago       Exit 0

That can be handy if the output from your software is a file on that file system. With Docker, you won’t lose anything until you explicitly remove it (docker rm or docker rmi).

Step 2: Update the Docker Version


Docker is still in rapid development, so there’s a good chance the version of docker in your droplet is old. Let’s check:

root@test:~# docker version
Client version: 0.6.1
Server version: 0.6.1
Git commit: 5105263
Go version: go1.1.2
Last stable version: 0.6.2, please update docker

Oh! Ok, version 0.6.1 was fine for our initial Hello World testing, but the current Docker Examples might not work with this older version. Let’s update with apt-get. The droplet has already been configured to check the correct PPA:

root@test:~# apt-get update
...
Get:1 http://archive.ubuntu.com raring-updates Release.gpg [933 B]   
...
Get:6 https://get.docker.io docker Release [1,522 B]                           
...
Get:8 https://get.docker.io docker/main amd64 Packages [1,154 B]               
...
Fetched 733 kB in 6s (117 kB/s)                                                                                                                   
Reading package lists... Done


root@test:~# apt-get install lxc-docker
...
Selecting previously unselected package lxc-docker-0.6.2.
Unpacking lxc-docker-0.6.2 (from .../lxc-docker-0.6.2_0.6.2_amd64.deb) ...
Processing triggers for ureadahead ...
Setting up lxc-docker-0.6.2 (0.6.2) ...
stop: Unknown instance: 
docker start/running, process 1911
Setting up lxc-docker (0.6.2) ...


root@test:~# docker version
Client version: 0.6.2
Go version (client): go1.1.2
Git commit (client): 081543c
Server version: 0.6.2
Git commit (server): 081543c
Go version (server): go1.1.2
Last stable version: 0.6.2

Looks like the new version is up and running.

Step 3: Share with the docker Group


Up until now, we’ve been using docker as root, but you may want to create other users on the droplet which can also access the docker daemon. By default, the daemon is listening on a Unix socket which is owned by root and the docker group, so we can share access by adding more people to the docker group.

First, set up a user account for working with docker as non-root. As an example, let’s call this user “dkr”:

root@test:~# useradd -m -d /home/dkr -s /bin/bash -U dkr
# Give dkr a password
root@test:~# passwd dkr

Ok, let’s see what they can do with Docker:

# Assume the dkr identity
root@test:~# su -l dkr
dkr@test:~$ docker version
Client version: 0.6.2
Go version (client): go1.1.2
Git commit (client): 081543c
2013/09/19 00:40:32 dial unix /var/run/docker.sock: permission denied

Epic fail! There’s a permission problem with the Unix socket so we can’t talk to the daemon. Let’s go back to being root and get dkr into the right group:

# exit from being dkr
dkr@test:~$ exit
# Now, as root, we add dkr to the docker group:
root@test:~# usermod -a -G docker dkr
# Ok, try running docker as dkr again!
root@test:~# su -l dkr
dkr@test:~$ docker version
Client version: 0.6.2
Go version (client): go1.1.2
Git commit (client): 081543c
Go version (server): go1.1.2
Last stable version: 0.6.2

Success! So now user dkr can use Docker without even having sudo privileges. You’re ready to start pulling or building images to run software on your droplet in nicely isolated containers.

Conclusion


DigitalOcean’s one-click application feature (and the image created by Ken Cochrane) has made it super easy to start working with Docker. If you’ve run through the Docker Tutorial and you’re eager to begin working with the real docker daemon, it would be hard to find an easier way to start!

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

4 Comments

Write Tutorial

Leave a Comment

Create an account or login:
Ajax-loader