Introduction to Docker

September 29, 2017    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.


Docker Architecture


Viewing available local images

docker image

Downloading images from Docker Hub registry

docker pull centos:6.8
docker pull ubuntu:14.04
docker pull centos # downloads latest image

Deleting an image

docker rmi <image_id>


List containers

# 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.

# 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

Running container in interactive mode

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

# 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)

docker exec -it <container id or name> bash

Stopping container

docker stop <container id or name>

Starting a stopped container

docker start <container id or name>

Deleting container

docker rm <container name or id>

Running container with name and port forwarding

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

Sharing volume in container

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

Getting IP address of docker container

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

Container resource usage (CPU/RAM usage)

docker stats

Accessing host database from a docker container

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


Image/container cleanup

List all exited containers

docker ps -aq -f status=exited

Remove stopped containers

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

Remove dangling/untagged images

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.