Books

Docker in Action (English ver.)

Docker入门到实践(中文)

速查

Docker Cheat Sheet

全量CLI

docker cheat sheet

容器管理CLI

container management commands

查看容器CLI

inspect container commands

容器交互CLI

interact with container commands

镜像管理CLI

image management commands

镜像传输CLI

image transfer commands

DOCKERFILE主要命令

builder main commands

Dockerfile

基底

1
FROM ruby:2.2.2

变量

1
2
ENV APP_HOME/myapp
RUN mkdir $APP_HOME

初始化

1
RUN bundle install
1
WORKDIR /myapp
1
2
VOLUME ["/data"]
# Specification for mount point
1
2
ADD file.xyz /file.xyz
COPY --chown=user:group host_file.xyz /path/container_file.xyz

Onbuild

1
2
ONBUILD RUN bundle install
# when used with another file

命令

1
2
EXPOSE 5900
CMD ["bundle", "exec", "rails", "server"]

Entrypoint

1
ENTRYPOINT exec top -b

Metadata

1
LABEL version="1.0"
1
2
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
1
2
LABEL description="This text illustrates \
that label-values can span multiple lines."

Docker Compose

基本用法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# docker-compose.yml
version: '2'

services:
  web:
    build: .
    # build from Dockerfile
    context: ./Path
    dockerfile: Dockerfile
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: redis

指令

1
2
docker-compose start
docker-compose stop
1
2
docker-compose pause
docker-compose unpause
1
2
3
docker-compose ps
docker-compose up
docker-compose down

Reference(例子)

构建

1
2
3
web:
  # build from Dockerfile
  build: .
1
2
3
4
  # build from custom Dockerfile
  build:
    context: ./dir
    dockerfile: Dockerfile.dev
1
2
3
4
5
6
 # build from image
  image: ubuntu
  image: ubuntu:14.04
  image: tutum/influxdb
  image: example-registry:4000/postgresql
  image: a4bc65fd

端口

1
2
3
  ports:
    - "3000"
    - "8000:80"  # guest:host
1
2
  # expose ports to linked services (not to host)
  expose: ["3000"]

指令

1
2
3
  # command to execute
  command: bundle exec thin -p 3000
  command: [bundle, exec, thin, -p, 3000]
1
2
3
  # override the entrypoint
  entrypoint: /app/start.sh
  entrypoint: [php, -d, vendor/bin/phpunit]

环境变量

1
2
3
4
5
  # environment vars
  environment:
    RACK_ENV: development
  environment:
    - RACK_ENV=development
1
2
3
  # environment vars from file
  env_file: .env
  env_file: [.env, .development.env]

依赖

1
2
3
4
5
  # makes the `db` service available as the hostname `database`
  # (implies depends_on)
  links:
    - db:database
    - redis
1
2
3
  # make sure `db` is alive before starting
  depends_on:
    - db

其他选项

1
2
3
4
  # make this service extend another
  extends:
    file: common.yml  # optional
    service: webapp
1
2
3
  volumes:
    - /var/lib/mysql
    - ./_data:/var/lib/mysql

高级特性

打标签

1
2
3
4
services:
  web:
    labels:
      com.example.description: "Accounting web app"

DNS服务器

1
2
3
4
5
6
services:
  web:
    dns: 8.8.8.8
    dns:
      - 8.8.8.8
      - 8.8.4.4

设备绑定

1
2
3
4
services:
  web:
    devices:
    - "/dev/ttyUSB0:/dev/ttyUSB0"

外部链接

1
2
3
4
5
services:
  web:
    external_links:
      - redis_1
      - project_db_1:mysql

主机设置

1
2
3
4
services:
  web:
    extra_hosts:
      - "somehost:192.168.1.100"

Services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# To view list of all the services runnning in swarm
docker service ls 

# To see all running services
docker stack services stack_name

# to see all services logs
docker service logs stack_name service_name 

# To scale services quickly across qualified node
docker service scale stack_name_service_name=replicas

Clean up

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# To clean or prune unused (dangling) images
docker image prune 

# To remove all images which are not in use containers , add - a
docker image prune -a 

# To Purne your entire system
docker system prune 

# To leave swarm
docker swarm leave

# To remove swarm ( deletes all volume data and database info)
docker stack rm stack_name 

# To kill all running containers
docker kill $(docekr ps -q )