Docker cheat-sheet

This page describes quick reference commands for Docker

Prerequisite


You need first need to install Docker Toolbox on your windows machine. On Mac, see below link

See https://docs.docker.com/engine/installation/

Basic Docker Commands


Environment setup

Create a Docker Machine in VirtualBox
docker-machine create --driver virtualbox default

-- In case you want to restart in future
docker-machine restart default
Create a Docker Machine in Digital Ocean
docker-machine create --driver digitalocean --digitalocean-access-token=$DIGITAL_OCEAN_TOKEN default1

-- In case you want to restart in future
docker-machine restart default1
Create a Docker Machine in Amazon EC2
docker-machine -D create --driver amazonec2 --amazonec2-access-key $AWS_ACCESS_KEY_ID 
--amazonec2-secret-key $AWS_SECRET_ACCESS_KEY --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-zone b default2


-- In case you want to restart in future
docker-machine restart default2
docker-machine env default
Find IP of Docker VM
docker-machine ip default
Set Environment variables

.. so that you can run docker executable commands (windows)

FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i
Find Docker Client/Server Version
docker version

Managing Images / Containers

List Docker images on the host
docker images
Run Images
docker run hello-world
Assign Names to containers while running images
docker run -d -P --name web training/webapp python app.py
docker ps
docker stop web
docker rm web
See Running and stopped containers
docker ps
docker ps -a -- shows both running and stopped containers
Start/Stop/Remove a container
docker start REPLACE_CONTAINER_NAME
docker stop REPLACE_CONTAINER_NAME
docker rm REPLACE_CONTAINER_NAME
See Running Application logs
docker ps
docker logs -f IMAGE_NAME

-f Command causes docker logs command to act like the tail -f command and watch the container’s standard out.

List top application processes inside container
docker ps
docker top IMAGE_NAME
Inspect container
docker ps
docker inspect IMAGE_NAME

We can also narrow down the information we want to return by requesting a specific element. Run below command to see the container’s IP address.

Find IP address of the container

docker ps
docker inspect -f ""  IMAGE_NAME

Building Images

Build an Image and create container
-- Assuming current directory has DockerFle in place
docker build -t sdalal/sinatra:v2 .

-- create a container from our new image
docker images
docker run -t -i sdalal/sinatra:v2 /bin/bash

OR

 docker run -d -p 7500:7500 IMAGE_NAME
Tag your images
docker images sdalal/sinatra:v2 -- Get the Image ID and use in Next step
docker tag  IMAGE_ID sdalal/sinatra:dev
docker images sdalal/sinatra

Compiles container’s changes into images

Assuming you have downloaded some base image and have done some changes on top of it.

docker commit -m "DESCRIPTION" -a "NAME" IMAGE_NAME YOURUSERNAME/IMAGE_NAME:latest
Push images to dockerhub
docker login -- Authentication credentials will be stored in the ~/.docker/config.json authentication file in your home directory
docker push sdalal/sinatra
Remove images from the host
docker rmi sdalal/sinatra

Building Networks

Creating networks and Attaching containers
docker network ls

-- create my-bridge-network
docker network create -d bridge my-bridge-network

-- Add containers to the network
-- Launch a container running a PostgreSQL database and pass it the --net=my-bridge-network flag to connect it to your new network:
docker run -d --net=my-bridge-network --name db training/postgres

-- Verify that container is attached to new network
docker inspect --format=""  db

-- Find IP address of the running container
docker inspect -f "" db

-- Start web container
docker run -d --name web training/webapp python app.py
-- FInd Ip address
docker inspect -f "" web

-- Open a shell to your running container 
docker exec -it db bash
ping WEB_CONTAINER_IP_ADDRESS -- will fail as containers are on different network

-- Add web to the network
docker network connect my-bridge-network web

-- Open a shell to your running container 
docker exec -it db bash
ping WEB_CONTAINER_IP_ADDRESS -- will pass as containers are on same network

Managing data

Add Data Volume
-- Create webapp volume
docker run -d -P --name web -v /webapp training/webapp python app.py

-- create webapp volume with read only permissions
docker run -d -P --name web -v /opt/webapp:ro training/webapp python app.py

-- Mount a host directory as a data volume
docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python


-- Creating and mounting a data volume container
docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python

Beginner commands


Run Simple hello world Example
docker run ubuntu:14.04 /bin/echo 'Hello world'
Run Interactive Docker Example
docker run -t -i ubuntu:14.04 /bin/bash
ls
A daemonized Hello world Example
docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
docker ps -- Find the machine_name
docker logs REPLACE_MACHINE_NAME_HERE_FROM_LAST_STEP
docker stop REPLACE_MACHINE_NAME_HERE_FROM_LAST_STEP
Run an existing web application
docker run -d -P training/webapp python app.py

docker ps -- note down

docker-machine ip default -- find ip of your vm 192.168.99.100 in my case

Test the application by going to http://192.168.99.100:32768/

Use below command to map running web application on to specific port.

docker run -d -p 80:5000 training/webapp python app.py

docker ps -- note down

docker-machine ip default -- find ip of your vm 192.168.99.100 in my case
Install Redis using Docker
docker run --name my-redis -it ubuntu:latest bash

References


https://docs.docker.com/engine/userguide/dockerizing/

Version History


Date Description
2016-01-10    Initial Version
2016-03-04    Added commands for Redis installation