Docker | Getting started

General Information


Install with Homebrew

brew install bash-completion
brew cask install docker
brew install kubectl
brew cask install minikube

After Installation, check versions

docker version
docker-compose version
docker-machine --version
kubectl version --client

First steps

Start a docker image with a given name

$ docker run --interactive --tty --name ubuntu ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
af49a5ceb2a5: Pull complete 
8f9757b472e7: Pull complete 
e931b117db38: Pull complete 
47b5e16c0811: Pull complete 
9332eaf1a55b: Pull complete 
Digest: sha256:3b64c309deae7ab0f7dbdd42b6b326261ccd6261da5d88396439353162703fb5
Status: Downloaded newer image for ubuntu:latest

Run a command

root@a5b411d609f0:/# uname -a
Linux a5b411d609f0 4.4.27-moby #1 SMP Wed Oct 26 14:21:29 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
root@a5b411d609f0:/# id
uid=0(root) gid=0(root) groups=0(root)
root@a5b411d609f0:/# hostname

Leave image

root@a5b411d609f0:/# exit

 Show running images

$ docker ps -a
b01ba9bfef78  ubuntu  "/bin/bash"  41 seconds ago Exited (0) 2 seconds ago        ubuntu

Start image

$ docker start ubuntu
$ docker ps -a
b01ba9bfef78  ubuntu  "/bin/bash"  2 minutes ago  Up 1 seconds                   ubuntu

Attach to image, e.g. „enter“ the image

Don’t forget to press enter after you entered the command do display the shell in the image again

$ docker attach ubuntu

Working with Docker

$ docker run -it --name ubuntu ubuntu bash

You are in a terminal with ubuntu and can do whatever you like.

To start again after a reboot:

$ docker start ubuntu
$ docker exec -it ubuntu bash

If you want save your changes:

$ docker commit ubuntu
$ docker images

See the unnamed image and:

$ docker tag <imageid> myubuntu

Then you can run another container using your new image.

$ docker run -it --name myubuntu myubuntu bash

Or replace the former

$ docker stop ubuntu
$ docker rm ubuntu
$ docker run -it --name ubuntu myubuntu bash

Docker Components


Create machine

$ docker-machine create --driver=virtualbox default
$ docker-machine ls
NAME         ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default      -        virtualbox   Running   tcp://           v1.12.2   
virtualbox   -        virtualbox   Stopped                                       Unknown 

Show environment of machine

$ docker-machine env
export DOCKER_HOST="tcp://"
export DOCKER_CERT_PATH="/Users/docker/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env)

Stop machine

$ docker-machine stop default
Stopping "default"...
docker-Machine "default" was stopped.

Start  machine

$ docker-machine start default
Starting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Machine "default" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the docker-machine env command.

Set environment of a machine

$ eval "$(docker-machine env default)"

Create machine from iso image

$ docker-machine create -d virtualbox --virtualbox-boot2docker-url <MACHINE-NAME>
$ docker-machine env rancheros


Images are just templates for docker containers.

List all images

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              f753707788c5        5 days ago          127.2 MB

Remove images

$ docker rmi <IMAGE ID>

Remove all images

$ docker rmi $(docker images -q)


List all container

$ docker ps -a

Run command in container

$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6bbedd9b76a4: Pull complete 
fc19d60a83f1: Pull complete 
de413bb911fd: Pull complete 
2879a7ad3144: Pull complete 
668604fde02e: Pull complete 
Digest: sha256:2d44ae143feeb36f4c898d32ed2ab2dffeb3a573d2d8928646dfc9cb7deb1315
Status: Downloaded newer image for ubuntu:latest

Run command and delete container after running

$ docker run -it --rm ubuntu hostname

Check environment in container

$ docker run -it ubuntu bash
# hostname
root@c26fc567f552:/# uname -a
Linux c26fc567f552 4.4.24-boot2docker #1 SMP Fri Oct 7 20:54:27 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

Remove all container

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

Commit container as new image

$ docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                     PORTS               NAMES
e4a50905aa9c        continuumio/anaconda   "/usr/bin/tini -- /bi"   22 minutes ago      Exited (0) 5 minutes ago                       pedantic_kirch
164daaac2349        4f3b088e1307           "/bin/sh -c 'apt-get "   4 hours ago         Exited (100) 4 hours ago                       happy_jang
817bb15d3171        i_electron             "/bin/bash"              2 weeks ago         Exited (0) 2 weeks ago                         cranky_wilson
$ docker commit e4a50905aa9c r14r_anaconda

Tipps and Tricks

Use npm as a comand wrapper

Create a  empty npm package

$ npm init --yes

Add this lines to the package.json file

  "name": "development",
  "license": "ISC",
  "scripts":  {
    "build": "docker build -t development .",
    "ssh": "docker run -i -t development /bin/bash"

Create a Dockerfile

FROM ubuntu
RUN apt-get update && apt-get install -y firefox

Now you can build the image with

$ npm run build
$ npm run ssh

Running XClients from Docker on macOS Host (Source)

Install Homebrew

Install requires parts

$ brew install socat
$ brew cask install xquartz

Now start XQuartz

$ open -a XQuartz

Expose local xquartz socket via socat on a TCP port.
Run this in another terminal window

$ socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"

The masking of the characters “ is VERY important

Run Firefox in Docker Container

$ docker run -it -e DISPLAY=server:0.0 i_firefox firefox

Create image from scratch

Scratch image

Create image from scratch

Using Linux as Docker OS

Available Linux OS’s

Alpine LinuxHome | Github | Docker Hub 
CoreOS Container LinuxHome | Github | Docker HubISO |
Rancher Labs RancherOSHome | Github | Docker Hub 
Red Hat Project AtomicHome | Github | Docker Hub 
VMware Photon OSHome | Github | Docker Hub 
de German