虚拟化技术 -- docker 2

虚拟化 -- docker2

  • 一.docker-Compose
    • 1.1 docker提倡理念:
    • 1.2 容器编排工具:
  • 二、容器管理
    • 2.1 docker基础概念及命令

一.docker-Compose

1.1 docker提倡理念:

    “一个容器一个进程”,假设一个服务器需要由多个进程组成,就需要多个容器组成一个系统,相互分工和配合对外提供完整服务。
比如:博客系统
▶组件1:mariadb
▶组件2:wordpress 的Apache web
    在启动容器时,同一台主机下如果两个容器之间需要由数据交流,使用–link选项建立两个容器之间的互联,前提是建立时mariadb已经开启。
        docker start db
        docker start MywordPress
停止:
        docker stop db MywordPress 或docker stop MywordPress 在docker top db

1.2 容器编排工具:

    允许用户在一个模板(YAML格式)中定义一组相关联的容器,会根据 --link等参数,对启动的优先级进行排序。
curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-` uname-s '- 'uname-m ’ >/usr/local/bin/docker-compose docker version

[root 09:21 @ docker ~] curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` >/usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617  100   617    0     0    551      0  0:00:01  0:00:01 --:--:--   551
100 8084k  100 8084k    0     0  1646k      0  0:00:04  0:00:04 --:--:-- 2887k
[root 09:23 @ docker ~] chmod a+x /usr/local/bin/docker-compose
[root 09:24 @ docker ~] docker-compose version
docker-compose version 1.14.0, build c7bdf9e
docker-py version: 2.3.0
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
-f    指定使用的yaml文件位置       up  -d     启动容器项目
ps    指定所有容器信息             pause      暂停容器
restart     重新启动容器          unpause    恢复暂停
logs        查看日志信息           rm        删除容器
config -q   验证yaml配置文件是否正确
stop        停止容器              start     启动容器
[root 09:40 @ docker ~] vim wordpress.yaml 
[root 09:42 @ docker ~] mv wordpress.yaml docker-compose.yaml
version: '2'

services:
   db:
     image: mysql:5.7
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

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

        ▶启动compose时出现报错:强制要求的,会在容器创建时将root用户密码设置成改变量的值。没有启动会报错,如下所示:(此步问题目前未解决,yaml文件中有MYSQL_ROOT_PASSWORD: somewordpress,启动依然报错)

db_1         | 2020-03-19 02:07:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian10 started.
db_1         | 2020-03-19 02:07:10+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1         | 2020-03-19 02:07:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian10 started.
db_1         | 2020-03-19 02:07:10+00:00 [Note] [Entrypoint]: Initializing database files
db_1         | 2020-03-19T02:07:10.605353Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

[root 09:43 @ docker ~] docker-compose up -d
[root 09:46 @ docker ~] docker ps -a
[root 09:48 @ docker ~] docker-compose logs
[root 09:52 @ docker ~] docker-compose ps
      Name                    Command               State          Ports         
--------------------------------------------------------------------------------
root_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp  
root_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:8000->80/tcp 

二、容器管理

2.1 docker基础概念及命令

docke三个重要概念:仓库(repository)、镜像(image)和容器(container)

docker run --name MyWordPress --link db:mysql -p 8080:80 -d wordpress
docker 指令的基本用法:
docker + 命令关键字(COMMAND)+ 一系列的参数

docker info          守护进程的系统资源设置
docker search        docker仓库的查询
docker pull          docker仓库的下载
docker images        docker镜像的查询
docker rmi           docker镜像的删除
docker ps            容器的查询
docker run           容器的创建启动
docker start/stop    容器启动停止
#docker指令除了单条使用外,还支持赋值、解析变量、嵌套使用

1.1.1 docker info

[root 10:13 @ docker ~]  docker info
Containers: 5
 Running: 2
 Paused: 0
 Stopped: 3
Images: 4
Server Version: 17.03.0-ce
Storage Driver: overlay  #overlay比DM更加稳定以及更强大
 Backing Filesystem: xfs
 Supports d_type: true

docker search

[root 11:26 @ docker ~] docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                              Official build of Nginx.                        12836     [OK]       
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1753                 [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   760                  [OK]

https://hub.docker.com

docker pull

[root 11:28 @ docker ~] docker pull nginx 
Using default tag: latest
latest: Pulling from library/nginx
68ced04f60ab: Already exists 
28252775b295: Pull complete 
a616aa3b0bf2: Pull complete 
Digest: sha256:2539d4344dd18e1df02be842ffc435f8e1f699cfc55516e2cf2cb16b7a9aea0b
Status: Downloaded newer image for nginx:latest

docker images

[root 11:31 @ docker ~] docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              6678c7c2e56c        2 weeks ago         127 MB
mysql               5.7                 84164b03fa2e        2 weeks ago         455 MB
mariadb             latest              1fd0e719c495        2 weeks ago         356 MB
wordpress           latest              126aa00ecc0c        2 weeks ago         539 MB
hello-world         latest              fce289e99eb9        14 months ago       1.84 kB

docker rmi #删除镜像
docker rm #删除容器

[root 11:34 @ docker ~] docker rmi -f fce2 #-f=force 强制删除   同时删除时还可以使用镜像名称加版本号,如:hello-world:latest 
Untagged: hello-world:latest
Untagged: hello-world@sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e

docker ps

[root 11:34 @ docker ~] docker ps
#只能查看当前正在运行的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0a629e00da9b        wordpress:latest    "docker-entrypoint..."   About an hour ago   Up About an hour    0.0.0.0:8000->80/tcp   root_wordpress_1
230dd00e2733        mysql:5.7           "docker-entrypoint..."   About an hour ago   Up About an hour    3306/tcp, 33060/tcp    root_db_1
[root 11:34 @ docker ~] docker ps -a

docker指令除了单条使用外,还支持赋值、解析变量、嵌套使用

[root 11:34 @ docker ~] docker ps -a -q #只显示容器ID号
0a629e00da9b
230dd00e2733
845918a7ac50
5ffff67360ab
9c96f4f8e610
[root 11:34 @ docker ~] docker rm -f $(docker ps -a -q) 

    每一个容器创建后,都会分配一个CONTAINER ID作为容器的唯一标示,后续对容器的启动、停止、修改、删除等所有操作,都是通过CONTAINER ID来完成,偏向于数据库概念中的主键。

docker ps --no-trunc               查看
docker stop/start CONTAINER ID     停止
docker stop/start MyWordPress      通过容器别名启动/停止
docker inspect MyWordPress         查看容器所有基本信息
docker logs MyWordPress            查看容器日志
docker stats MyWordPress           查看容器所占用的系统资源
docker exec 容器名 容器名内执行的命令  容器执行命令
docker exec -it 容器名 /bin/bash    登入容器的bash
[root 11:53 @ docker ~] docker ps --no-trunc
CONTAINER ID                                                       IMAGE               COMMAND                                     CREATED             STATUS              PORTS                  NAMES
0a629e00da9bc8133efabda734aea414b4d7e58fabaeda1ddf1298fb98780345   wordpress:latest    "docker-entrypoint.sh apache2-foreground"   About an hour ago   Up About an hour    0.0.0.0:8000->80/tcp   root_wordpress_1
230dd00e2733b36a6c78ece2e007c958e15abebedf83a66222e7a46f5827c173   mysql:5.7           "docker-entrypoint.sh mysqld"               About an hour ago   Up About an hour    3306/tcp, 33060/tcp    root_db_1
[root 09:29 @ docker ~]#docker exec -it 0a629 /bin/bash
root@0a629e00da9b:/var/www/html#

run延伸:

--restart=always                 容器的自动启动      
-h x.xx.xx                       设置容器主机名
--dns xx.xx.xx.xx                设置容器使用的DNS服务器
--dns-search                     DNS搜索设置
--add-host hostname:IP           注入hostname<>IP解析
--rm                             服务停止时自动删除

你可能感兴趣的:(虚拟化技术)