Skip to main content

How To Use Docker


"With Docker, developers can build any app in any language using any toolchain. “Dockerized” apps are completely portable and can run anywhere - colleagues’ OS X and Windows laptops, QA servers running Ubuntu in the cloud, and production data center VMs running Red Hat.

Developers can get going quickly by starting with one of the 13,000+ apps available on Docker Hub. Docker manages and tracks changes and dependencies, making it easier for sysadmins to understand how the apps that developers build work. And with Docker Hub, developers can automate their build pipeline and share artifacts with collaborators through public or private repositories.

Docker helps developers build and ship higher-quality applications, faster." -- What is Docker

Tip:

I use Oh My Zsh with the Docker plugin for autocompletion of docker commands. YMMV

Once you have docker installed, its intuitive usage experience makes it very easy to work with. By now, you shall have the docker daemon running in the background.

If not, use the following command to run the docker daemon.

To run the docker daemon:
sudo docker -d &

Usage Syntax:

Using docker (via CLI) consists of passing it a chain of options and commands followed by arguments. Please note that docker needs sudo privileges in order to work.
sudo docker [option] [command] [arguments]

Note: Below instructions and explanations are provided to be used as a guide and to give you an overall idea of using and working with docker. The best way to get familiar with it is practice on a new VPS. Do not be afraid of breaking anything– in fact, do break things! With docker, you can save your progress and continue from there very easily.

Beginning

Let's begin with seeing all available commands docker have.

Ask docker for a list of all available commands:
sudo docker

All currently (as of 0.7.1) available commands:
attach Attach to a running container
build Build a container from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders from the containers filesystem to the host path
diff Inspect changes on a container's filesystem
events Get real time events from the server
export Stream the contents of a container as a tar archive
history Show the history of an image
images List images
import Create a new filesystem image from the contents of a tarball
info Display system-wide information
insert Insert a file in an image
inspect Return low-level information on a container
kill Kill a running container
load Load an image from a tar archive
login Register or Login to the docker registry server
logs Fetch the logs of a container
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
ps List containers
pull Pull an image or a repository from the docker registry server
push Push an image or a repository to the docker registry server
restart Restart a running container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image to a tar archive
search Search for an image in the docker index
start Start a stopped container
stop Stop a running container
tag Tag an image into a repository
top Lookup the running processes of a container
version Show the docker version information
wait Block until a container stops, then print its exit code

Check out system-wide information and docker version:
For system-wide information on docker:
sudo docker info

For docker version:
sudo docker version

Working with Images

As we have discussed at length, the key to start working with any docker container is using images. There are many freely available images shared across docker image index and the CLI allows simple access to query the image repository and to download new ones.

When you are ready, you can also share your image there as well. See the section on “push” further down for details.

Searching for a docker image:*

Usage: sudo docker search [image name]
sudo docker search ubuntu

This will provide you a very long list of all available images matching the query: Ubuntu.

Downloading (PULLing) an image:

Either when you are building / creating a container or before you do, you will need to have an image present at the host machine where the containers will exist. In order to download images (perhaps following “search”) you can execute pull to get one.

Usage: sudo docker pull [image name]
sudo docker pull ubuntu

Listing images:

All the images on your system, including the ones you have created by committing (see below for details), can be listed using “images”. This provides a full list of all available ones.

Example: sudo docker images
sudo docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
my_img latest 72461793563e 36 seconds ago 128 MB
ubuntu 12.04 8dbd9e392a96 8 months ago 128 MB
ubuntu latest 8dbd9e392a96 8 months ago 128 MB
ubuntu precise 8dbd9e392a96 8 months ago 128 MB
ubuntu 12.10 b750fe79269d 8 months ago 175.3 MB
ubuntu quantal b750fe79269d 8 months ago 175.3 MB

Committing changes to an image:

As you work with a container and continue to perform actions on it (e.g. download and install software, configure files etc.), to have it keep its state, you need to “commit”. Committing makes sure that everything continues from where they left next time you use one (i.e. an image).

Usage: sudo docker commit [container ID] [image name]
sudo docker commit 8dbd9e392a96 my_img

Sharing (PUSHing) images:

Although it is a bit early at this moment - in our article, when you have created your own container which you would like to share with the rest of the world, you can use push to have your image listed in the index where everybody can download and use.

Please remember to “commit” all your changes.

Usage: sudo docker push [username/image name]
sudo docker push my_username/my_first_image

Note: You need to sign-up at index.docker.io to push images to docker index.

Working with Containers

When you "run" any process using an image, in return, you will have a container. When the process is notactively running, this