目錄

  1. Get information
  2. Basic operations
  3. Some advanced applications

Usage: [sudo] docker [command] [flags] [arguments] ..

Get information

The version of docker:

1
2
3
4
5
6
7
8
9
10
example> docker version
Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.3.3
Git commit (client): 5bc2ff8
OS/Arch (client): linux/amd64
Server version: 1.4.1
Server API version: 1.16
Go version (server): go1.3.3
Git commit (server): 5bc2ff8

Show the information about docker status:

1
2
3
4
5
6
7
8
9
example> docker info
Containers: 5
Images: 5
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Dirs: 15
Execution Driver: native-0.2
Kernel Version: 3.13.0-32-generic
WARNING: No swap limit support

Basic operations

Download image file provided from Docker Hub:

1
2
docker pull <image-name>
example> docker pull ubuntu:14.04

Run specified cmd on this image:

1
2
3
docker run <image-name> <cmd>
example> # Run interactively in the foreground
example> docker run -i -t ubuntu /bin/bash

Flags:

-t flag assigns a terminal inside our new container.

-i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.

-d flag tells Docker to run the container and put it in the background, to daemonize it.

-p flag is new and tells Docker to map any required network ports inside our container to our host.

-P flag, a shortcut for -p, maps any required network ports exposed in our image to a high port (from the range 49153 to 65535) on the local Docker host.

-v flag mount the host directory into container directory, ex -v <host-dir>:<container-dir> or -v <container-dir>

--name flag assigns a name to this container

<image-name>: ubuntu:14.04

<cmd>: /bin/echo “hello world”

View info of container:

1
2
docker inspect <container-id-or-name>
example>

Lists containers:

1
2
3
4
5
6
example> # To check which docker is running
example> docker ps
example> #
example> docker ps -l
example> # Show all docker in execution
example> docker ps -a

Stops running containers:

1
2
3
docker stop <container-id-or-name>
example> # This will stop the container id of cfc68
example> docker stop cfc68

Commit a modified image:

1
2
3
4
docker commit <container-id-or-name> <image-name>
example> docker commit -m="Add PHPUnit and xdebug" \
example> -a="Huang Yi-Ming" aeeb1980c96e \
example> ymhuang0808/phpunit-testing:php5.5.9

Flags:

-m flag is used to give a comment to this commit.

-a flag declare the maintainer’s name.

Remove the image:

1
docker rmi <image-id>

Login Docker Hub (before you push a image to repository):

1
2
3
example> docker login
Username: [ENTER YOUR USERNAME] Password:
Email: [ENTER YOUR EMAIL] Login Succeeded

Push image to the repository (usually on Docker Hub):

1
2
docker push <image-name>
example> docker push a504082002/ubuntu:14.04

Shows us the standard output of a container:

1
docker logs

Some advanced applications

Query the port mapping of a container:

1
2
3
4
docker port <container-id-or-name> <port-exposed>
example> # which port was mapped to the port 5000 on container named of nostalgic_morse
example> docker port nostalgic_morse 5000
0.0.0.0:49155

Running a web application in docker:

1
2
example> # we're going to run a Python Flask application with image: training/webapp
example> docker run -d -P training/webapp python app.py

Assign command to specific docker:

1
2
example> # Return to docker again after exit
example> docker exec -t -i 0f83f1728262 bash

To remove docker that you don’t need anymore:

1
example> docker rm 0f83f1728262

To mount a host directory as a disk in docker:

1
2
example> # Mount /myData into docker
example> docker run -t -i -v /myData centos:centos6 bash