0%

Docker Cheatsheet

参考:Use the Docker command line

Common

1
2
3
4
docker version

docker --help
docker attach --help

Images

Pull an image or a repository from a registry

1
docker pull debian:jessie

Build an image from a Dockerfile

1
2
docker build -t vieux/apache:2.0 .
docker build -f Dockerfile.debug .

To see which images are present locally

1
docker images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

docker pull xxx:v1

docker search xxx



# create image from container
docker commit -m "comments" -a "" <md5> xxx:v1

# Remove image
docker rmi xxx:v1
# Clean dangling images
docker images|grep \<none\>|awk '{print $3}'|xargs docker rmi
docker rmi $(docker images -f "dangling=true" -q)

docker history xxx:v1

Containers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
docker ps -a

docker run ubuntu /bin/echo 'Hello world'

docker run --name mynginx -d nginx:latest

# -t flag assigns a pseudo-tty or terminal inside our new container
# -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container
docker run -t -i ubuntu /bin/bash
# -d flag tells Docker to run the container and put it in the background, to daemonize it
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
docker run -it nginx:latest /bin/bash
# -P flag is new and tells Docker to map any required network ports inside our container to our host
# -p 80:5000 map port 5000 inside our container to port 80 on our local host
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
docker run -p 80:80 -v /data:/data -d nginx:latest
docker run -P -d nginx:latest

docker logs 2b6333eb87ea
docker logs -f elegant_colden

docker top elegant_colden

docker inspect elegant_colden

docker inspect -f '{{ .NetworkSettings.IPAddress}}' elegant_colden

docker stop 2b6333eb87ea

docker restart elegant_colden

docker rm elegant_colden
docker rm `docker ps --no-trunc -aq`
docker rm $(docker ps -a -q)

Save & Load

Save running container

1
docker save -o xxx.tar.gz 2b6333eb87ea

Save one or more images to a tar archive (streamed to STDOUT by default)

1
docker save busybox-1 > /home/save.tar

Load saved container

1
2
docker load -i xxx.tgz
docker load < xxx.tgz

Export命令用于持久化容器(不是镜像): Export a container’s filesystem as a tar archive

1
2
docker export <CONTAINER ID> > /home/export.tar
docker export --output="latest.tar" red_panda

Monitor

A live data stream for running containers

1
docker stats [OPTIONS] [CONTAINER...]

Volumn

Create data volumn

1
docker create -v /tools/ --name tools centos:6.6 /bin/true

Start a container with a new data volumn

1
docker run -i -t -v /tools centos:6.6 /bin/bash

Mount a host directory as a data volume

1
docker run -i -t -v /tmp/tools:/tools centos:6.6 /bin/bash

A read-only data volumn

1
docker run -i -t -v /tmp/tools:/tools:ro centos:6.6 /bin/bash

两个参数是:主机绝对路径or一个name:容器绝对路径:只读

Mount a host file as a data volume

1
docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash

Remove a volumn

1
docker rm -v