Docker Cheat Sheet

My last post took you though setting up boot2docker on your system and provided a quick cheat sheet on how to configure and interact with it.

In this post I will be laying out a running list of the commands I use with docker to get the most out of my containers.

What is Docker? (From docker.com)

Docker allows you to package an application with all of its dependencies into a standardized unit for software development.

Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in.

Docker Images vs Docker Containers

Docker Images are the basis from which Docker containers are created.  When you startup a Docker image, a docker container is created.  I liken it to classes and objects.  The image (class) represents all of the capabilities of the container (object) once it is instantiated but an image cannot do anything.  Once a container is created, it can be started and stopped freely and it saves its state.  You can create multiple instances of a particular image as long as you give them different names.

Working with Docker Images

Installing Docker using homebrew

$ brew install docker

If you are not a user of HomeBrew for package management, I highly recommend it.  You can get more information on it and how to install it at : Homebrew

Open bash prompt in a container

$ docker images $ CONTAINERID = $(docker run -rm -t -i $image_id /bin/bash)

-i = Keep STDIN open even if not attached

–rm = Automatically remove the container when it exits

-t = Allocate a pseudo-TTY

This command creates a container from the specified image ($image_id), opens a bash shell into it and returns the container id (CONTAINERID)

List all images in your local repository

$ docker images

Remove all untaged images (registry cleanup)

$ docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

Working with containers

Open bash shell in a container

To launch a container, simply use the command docker run + the image name you would like to run + the command to run within the container. If the image doesn’t exist on your local machine, docker will attempt to fetch it from the public image registry.

$ docker run -t -i ubuntu /bin/bash

-i = Keep STDIN open even if not attached

-t = Allocate a pseudo-TTY

List all of the running containers

$ docker ps

List all containers

$ docker ps -a

Get the full container id

$ docker inspect -f '{{.Id}}' $NSM_CONTAINER_NAME

Stop a container and remove it

$ docker rm $(docker stop $CONTAINERID)

Stop all containers

$ docker stop $(docker ps -a -q)

Remove all containers that are not running

$ docker rm $(docker ps -a -q)

Execute a command in a running container

(Requires docker v1.3 or later)

$ docker exec $CONTAINERID <command>

Tail the log on a running container (Requires docker v1.3)

(Requires docker v1.3 or later)

$ docker exec $CONTAINERID tail -f <path to log file>

Fetch the logs of a running container

$ docker logs $CONTAINERID

Copy files from a container

Start the container

$ CONTAINERID=$(docker run -d $DOCKER_TAG /usr/local/bin/ncm)

Copy file(s) from the container to the destination path

$ docker cp $CONTAINERID:$source_path $destination_path

Shut down the container

$ docker rm $(docker stop $CONTAINERID)

Copy files to a running container

$ FULL_CONTAINER_ID = $(docker inspect -f '{{.Id}}' $NSM_CONTAINER_NAME) $ sudo cp file.txt /var/lib/docker/aufs/mnt/$(docker inspect -f '{{.Id}}' $NSM_CONTAINER_NAME)/root/file.txt

or

$ sudo cp file.txt /var/lib/docker/aufs/mnt/$FULL_CONTAINER_ID/root/file.txt 

Enjoy,

Dwain

What next?
Let Axian come to the rescue and help define your custom application strategy, develop a roadmap, work with your business community to identify the next project, and provide clarity and direction to a daunting task. For more details about Axian, Inc. and the Custom Application practice click here to view our portfolio or email us directly to setup a meeting.