docker images

docker container

单个应用程序 --非集群

Docker machine

docker machine是一个便于在多平台上部署Docker的一个工具;多平台比如:

  • Amazon Web Services
  • Microsoft Azure
  • Digital Ocean
  • Exoscale
  • Google Compute Engine
  • Generic
  • Microsoft Hyper-V
  • OpenStack
  • Rackspace
  • IBM Softlayer
  • Oracle VirtualBox
  • VMware vCloud Air
  • VMware Fusion
  • VMware vSphere
  • VMware Workstation(plugin)

install docker-machine

$ curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

Configure Script

scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.13.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done

A demo

docker-machine create -d virtualbox local

docker-machine create --driver generic --generic-ip-address 10.0.0.4 vm02

root@vm:/home/marion# docker-machine ls
NAME    ACTIVE   DRIVER       STATE     URL                   SWARM   DOCKER        ERRORS
local   -        virtualbox   Stopped                                 Unknown       
vm01    -        generic      Running   tcp://10.0.0.3:2376           v17.11.0-ce 

root@vm:/home/marion# docker-machine create -d virtualbox default
Running pre-create checks...
Creating machine...
(default) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env default

# 这是一个没有提供驱动的并且已经安装好docker engine的docker host. 但是依旧可以使用docker-machine管理
$ docker-machine create --driver none --url=tcp://50.134.234.20:2376 custombox
$ docker-machine ls
NAME        ACTIVE   DRIVER    STATE     URL
custombox   *        none      Running   tcp://50.134.234.20:2376

docker machine默认使用的操作系统

  • https://docs.docker.com/machine/drivers/os-base/

docker machine的新特性

  • https://docs.docker.com/release-notes/docker-machine/

Docker machine的provider

docker machine provider

  • 常见的linux系统
  • 虚拟化平台-virtualbox,vmware,hyper-v
  • openstack
  • Amazon web services,Microsoft Azure,Google computer Engine,Digital Ocean等

安装docker machine

curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

安装bash completion脚本

  • 手动下载位置
  • 自动下载
scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.13.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done

安装virtualbox

Virtualbox package download url

# 下载deb安装包安装
# 注意:如果你是在vmware上做的实验,请开启虚拟化Inter-VT/AMD-V

Docker-machine命令使用

Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.13.0, build 9ba6da9

Author:
  Docker Machine Contributors - 

Options:
  --debug, -D                   Enable debug mode
  --storage-path, -s "/root/.docker/machine"    Configures storage path [$MACHINE_STORAGE_PATH]
  --tls-ca-cert                 CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  --tls-ca-key                  Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  --tls-client-cert                 Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  --tls-client-key              Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  --github-api-token                Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  --native-ssh                  Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  --bugsnag-api-token               BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  --help, -h                    show help
  --version, -v                 print the version

Commands:
  active        Print which machine is active
  config        Print the connection config for machine
  create        Create a machine
  env           Display the commands to set up the environment for the Docker client
  inspect       Inspect information about a machine
  ip            Get the IP address of a machine
  kill          Kill a machine
  ls            List machines
  provision     Re-provision existing machines
  regenerate-certs  Regenerate TLS Certificates for a machine
  restart       Restart a machine
  rm            Remove a machine
  ssh           Log into or run a command on a machine with SSH.
  scp           Copy files between machines
  mount         Mount or unmount a directory from a machine with SSHFS.
  start         Start a machine
  status        Get the status of a machine
  stop          Stop a machine
  upgrade       Upgrade a machine to the latest version of Docker
  url           Get the URL of a machine
  version       Show the Docker Machine version or a machine docker version
  help          Shows a list of commands or help for one command

参考链接

  • Docker Machine安装
  • Docker machine下载地址

Docker compose

docker compose就是通过yml文件来定义和运行多个容器docker应用程序的工具,三步过程就能跑起一个compose:

  • 定义应用程序的环境(yml中)
  • 定义组成应用程序的服务(docker-compose.yml)
  • docker-compose up启动整个应用程序

安装docker compose

依赖:docker engine

# download the latest version
sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# excutable
sudo chmod +x /usr/local/bin/docker-compose
# check version
docker-compose --version

docker compose

Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f ...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate Compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)

  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pulls service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

6.3 create a demo

  • Create a directory for the project
  • create application
  • Create another file called requirements.txt in your project directory and paste this in
  • Create a Dockerfile
  • Define services in a Compose file (docker-compose.yml)
  • Build and run your app with Compose
  • access your application

6.4 deploy wordpress with docker-compose

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: rootpasswd
       MYSQL_DATABASE: wordpress
       MYSQL_USER: marion
       MYSQL_PASSWORD: marionpasswd

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: marion
       WORDPRESS_DB_PASSWORD: marionpasswd
volumes:
    db_data:

docker compose ref link

  • docker-compose user guide
  • composefile Formate
  • docker-compose overview
  • 关于docker-compose的一些资料可以通过以上链接进行搜索
  • docker-compose github
  • compose and wordpress

docker service

分布式负载均衡服务方式,定义了服务在集群中是怎么运行的

docker swarm

  • docker daemon port(2376)
  • docker swarm manager port(2377)
# install docker-machine
# 如果是在虚拟机中使用virtualbox,请开启虚拟化模式
docker-machine create \
 --driver virtualbox \
 --virtualbox-boot2docker-url file:///home/marion/boot2docker-v17.09.1-ce.iso \
 manager

# docker-machine在创建docker host时会下载默认最新版本的boot2docker,但是因为网络会下载很慢甚至报错,因此先下载到本地
docker-machine create \
 --driver virtualbox \
 --virtualbox-boot2docker-url file:///home/marion/boot2docker-v17.09.1-ce.iso \
 worker

# 查看docker host分配的信息
root@dockermaster:~# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
manager   -        virtualbox   Running   tcp://192.168.99.100:2376           v17.09.1-ce   
worker    -        virtualbox   Running   tcp://192.168.99.101:2376           v17.09.1-ce   

# 将manager节点启用集群管理模式,设置当前的manager为集群管理器
docker-machine ssh manager "docker swarm init --advertise-addr 192.168.99.100"
# 初始化后会返回添加node的命令
# 添加worker加入swarm
root@dockermaster:~# docker-machine ssh worker "docker swarm join --token SWMTKN-1-2kcyacweb1kno0szfjrjob5ao2v8ie0k428riedz042hnzwhmq-7eegju6j34jisuhqbq9pet3mk 192.168.99.100:2377"
This node joined a swarm as a worker.
root@dockermaster:~# 

# 在manager上查看swarm中的节点
root@dockermaster:~# docker-machine ssh manager "docker node ls"
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
p0fmsvxbpptktwwjjey7hzfat *   manager             Ready               Active              Leader
i8ef198m02bowqkx99nisgsjc     worker              Ready               Active              

# 配置你的docker-machine shell终端
scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.13.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done

# 查看docker host的环境变量配置
docker-machine env manager

# Run this command to configure your shell
eval $(docker-machine env manager)

# 查看处在Active状态的docker host(有星号提示)
root@dockermaster:~# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
manager   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.09.1-ce   
worker    -        virtualbox   Running   tcp://192.168.99.101:2376           v17.09.1-ce 

root@dockermaster:~# docker stack deploy -c docker-compose.yaml managertest
Creating network managertest_webnet
Creating service managertest_web
root@dockermaster:~# docker stack ps managertest
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE              ERROR               PORTS
kno7kg5g2eil        managertest_web.1   nginx               manager             Running             Preparing 25 seconds ago                       
kzu28xbn6th6        managertest_web.2   nginx               worker              Running             Preparing 25 seconds ago                       
706xagf904f2        managertest_web.3   nginx               worker              Running             Preparing 25 seconds ago                       
6phm8f6c9n2p        managertest_web.4   nginx               manager             Running             Preparing 25 seconds ago                       
3ee4hhlspmme        managertest_web.5   nginx               worker              Running             Preparing 25 seconds ago                       
root@dockermaster:~# eval $(docker-machine env -u)
root@dockermaster:~# docker ps
CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                    NAMES
a7ea6e30b068        grafana/grafana             "/run.sh"              6 hours ago         Up 39 minutes       3000/tcp                 grafana1
a31b65a7fd4d        prom/node-exporter:latest   "/bin/node_exporter"   18 hours ago        Up 39 minutes       9100/tcp                 monitoring_node_exporter
7c8311be2b4f        grafana/grafana             "/run.sh"              18 hours ago        Up 39 minutes       0.0.0.0:3000->3000/tcp   grafana

root@dockermaster:~# eval $(docker-machine env worker)
root@dockermaster:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
73745f698d12        nginx:latest        "nginx -g 'daemon ..."   2 minutes ago       Up 2 minutes        80/tcp              managertest_web.2.kzu28xbn6th6c7dxnotn7f90v
99c0c638629f        nginx:latest        "nginx -g 'daemon ..."   2 minutes ago       Up 2 minutes        80/tcp              managertest_web.3.706xagf904f2j97tlwy2kkh1o
67dc57a4278e        nginx:latest        "nginx -g 'daemon ..."   2 minutes ago       Up 2 minutes        80/tcp              managertest_web.5.3ee4hhlspmmef8ksdstb92drd

root@dockermaster:~# eval $(docker-machine env manager)
root@dockermaster:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4ca0c1899c55        nginx:latest        "nginx -g 'daemon ..."   3 minutes ago       Up 3 minutes        80/tcp              managertest_web.1.kno7kg5g2eilylcybo642493k
354e61f3665b        nginx:latest        "nginx -g 'daemon ..."   3 minutes ago       Up 3 minutes        80/tcp              managertest_web.4.6phm8f6c9n2pb9811ud6e7wmp

root@dockermaster:~# curl -I http://192.168.99.100
HTTP/1.1 200 OK
Server: nginx/1.13.7
Date: Tue, 26 Dec 2017 08:32:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Nov 2017 14:28:04 GMT
Connection: keep-alive
ETag: "5a1437f4-264"
Accept-Ranges: bytes

root@dockermaster:~# curl -I http://192.168.99.101
HTTP/1.1 200 OK
Server: nginx/1.13.7
Date: Tue, 26 Dec 2017 08:32:28 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Nov 2017 14:28:04 GMT
Connection: keep-alive
ETag: "5a1437f4-264"
Accept-Ranges: bytes

# 增加副本或者减少副本,执行修改docker-compose.yaml文件
vim docker-compose.yaml(replicats:3)
eval $(docker-machine env manager)
docker stack deploy -c docker-compose.yaml managertest
docker ps
root@dockermaster:~# docker service ps managertest_web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
706xagf904f2        managertest_web.3   nginx               worker              Running             Running 14 minutes ago                       
6phm8f6c9n2p        managertest_web.4   nginx               manager             Running             Running 15 minutes ago                       
3ee4hhlspmme        managertest_web.5   nginx               worker              Running             Running 14 minutes ago

docker stack ls                                            # List stacks or apps
docker stack deploy -c    # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps                   # List tasks associated with an app
docker inspect                    # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm                              # Tear down an application
docker swarm leave --force      # Take down a single node swarm from the manager