Docker

From Bonus Bits
Jump to: navigation, search

Description

This article gives common docker command syntax examples on a Mac with Docker Toolbox (VirtualBox) installed.


Prerequisites


Images

List Images

docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
<none>                        <none>              88c75ebcd6e9        9 hours ago         291.6 MB
<none>                        <none>              44e83afd49db        11 hours ago        291.6 MB
centos                        centos7             40dae1fe8777        9 days ago          196.7 MB
kitematic/hello-world-nginx   latest              04b455fad7b9        13 months ago       7.913 MB


Create Image from Dockerfile

  1. Write a Dockerfile
    Example
    FROM amazonlinux:latest
    MAINTAINER Levon Becker "first.last@domain.com"
    
    # Install Basics
    RUN yum clean all
    RUN yum update -y
    RUN yum install -y sudo openssh-server openssh-clients which curl vim-enhanced openssl ca-certificates net-tools htop git
    
  2. Build Container
    docker build -f Dockerfile -t <name to give container> .
    


Import Tarball to Create Image

docker import </path/to/tar | URL> <name>


Export Image to Tarball

docker export -o </path/to/file.tar> <container name>


Remove Image

docker rmi <image name>


Containers

List Running Containers

List running containers

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
5c1f53e43598        88c75ebcd6e9        "/usr/sbin/sshd -D -o"   4 minutes ago       Up 4 minutes        0.0.0.0:32770->22/tcp   kitchen-base-centos72


List All Containers

List running and stopped containers

docker ps -a


List Lastest Container

List latest created container

docker ps -l


Run Container from Image

Run an image

docker run --name <name to give container> -it <imagename>

Example

docker run --name amzntest -it amazonlinux:latest


Create Container from Docker Hub Image

Create a new container from a Docker Hub Image.

docker create <imagename> --name <name to give container>


Run Command on a Container

docker exec [OPTIONS] <container name> <command> [ARGS]


Login to a Container

docker exec -i -t <container name> /bin/bash


Copy Files to a Container

Copy files from local system to a container.

docker cp [OPTIONS] <container name>:<source path> <destination path>


Fetch Logs of a Container

docker logs <container name>


Start, Stop, Restart and Kill a Container

docker start <name>
docker stop <name>
docker restart <name>
docker kill <name>


Remove a Container

docker rm <name>


Server (Deprecated)

List Current Docker Server Information

List information about the Docker Server that is currently configured in the Shell with environment variables.

docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 34
Server Version: 1.12.0
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 45
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: host bridge null overlay
Kernel Version: 4.4.16-boot2docker
Operating System: Boot2Docker 1.12.0 (TCL 7.2); HEAD : e030bab - Fri Jul 29 00:29:14 UTC 2016
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.955 GiB
Name: default
ID: HBGP:2HDA:CVJV:XZ3E:WPOF:7SI4:EBVP:EVQN:BMCV:WETL:LS3E:UNB5
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug mode (client): false
Debug mode (server): true
 File Descriptors: 13
 Goroutines: 24
 System Time: 2016-07-29T15:07:54.676584538Z
 EventsListeners: 0
Registry: https://index.docker.io/v1/
Labels:
 provider=virtualbox


List Docker Servers

List all Docker Servers that the containers are spun up on.

docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.0


Start, Stop & Restart a Docker Server

docker-machine start <name>
docker-machine stop <name>
docker-machine restart <name>


List Docker Server Environment

List a Docker Server environment.

docker-machine env <docker machine name>


Switch Default Docker Server in Shell

To switch the environment variables to point to another Docker Server (machine) temporarily in a shell we can use the eval command and Docker machine name.

eval $(docker-machine env <docker machine name>)


Upgrade Docker Server

Upgrade a Docker Server to the latest Docker version.

docker-machine upgrade <name>


Upgrade Docker Server

Upgrade a Docker Server to the latest Docker version.

docker-machine upgrade <name>


Related Articles


Sources