Introduction to Docker

Docker provides framework to isolate an application and its dependencies into self-contained unit called containers. These containers can be run anywhere. Containers can be considered as “light” version of virtual machines.

Installation

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

Docker Architecture

Images

Viewing available local images

1
docker image

Downloading images from Docker Hub registry

1
2
3
docker pull centos:6.8
docker pull ubuntu:14.04
docker pull centos # downloads latest image

Deleting an image

1
docker rmi <image_id>

Containers

List containers

1
2
3
4
5
# show running containers
docker ps

# show all containers (running and stopped)
docker ps -a

Running container in detached mode

In detached mode, container runs in background as a daemon.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# detached mode, with pseudo-tty. if pseudo-tty is not used, the container 
# will exit immediately after running as daemon
# with -d container keeps running in background as daemon
# -d Detached mode: Run container in the background
# -t Allocate a pseudo-tty

docker run -t -d centos:6.8

# specifying container memory
docker run -t -d --memory="1g" centos:6.8 

Running container in interactive mode

In interactive mode, containers run in foreground attached to local command line session.

1
2
3
4
5
# interactive mode
# run container, attached interactively to local command line session and run /bin/bash
# in this case, the container exits when we exit from /bin/bash

docker run -i -t centos:6.8 /bin/bash

SSH into docker container (when container is running in background)

1
docker exec -it <container id or name> bash

Stopping container

1
docker stop <container id or name>

Starting a stopped container

1
docker start <container id or name>

Deleting container

1
docker rm <container name or id>

Running container with name and port forwarding

1
docker run -t -d --name centos_container -p 80:80 centos:6.8

Sharing volume in container

1
2
# -v /host/directory:/container/directory 
docker run -i -t -v $(pwd):/opt centos:6.8 /bin/bash

Getting IP address of docker container

1
docker inspect <container_id> | grep IPAddress | cut -d '"' -f 4

Container resource usage (CPU/RAM usage)

1
docker stats

Accessing host database from a docker container

1
2
# 192.168.0.103 is host ip
docker run -t -d --add-host=docker:192.168.0.103 centos:6.8

 

Image/container cleanup

List all exited containers

1
docker ps -aq -f status=exited

Remove stopped containers

1
docker ps -aq --no-trunc | xargs docker rm

Remove dangling/untagged images

1
docker images -q --filter dangling=true | xargs docker rmi

GUI to manage docker containers

DocStation provides GUI for monitoring, configuring and managing services and containers.