Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2263107
  • 博文数量: 168
  • 博客积分: 6641
  • 博客等级: 准将
  • 技术积分: 1996
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-02 11:49
文章存档

2020年(4)

2019年(6)

2017年(1)

2016年(3)

2015年(3)

2014年(8)

2013年(2)

2012年(12)

2011年(19)

2010年(10)

2009年(3)

2008年(17)

2007年(80)

分类: 架构设计与优化

2019-07-20 23:23:02

Docker CE容器部署及基本使用

TsengYia@ http://tsengyia.blog.chinaunix.net

一、基本概念
    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
    Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 AUFS 类的 Union FS 等技术,对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器。
    官方网站:
    
    仓库:用来提供/存放镜像,有官方仓库、私有仓库。
    镜像:针对某个虚拟机或某个应用封装的独立环境,作为容器的模板。
    容器:基于某个镜像启动的在内存中运行的实例。
    
    传统虚拟化与容器技术对比:

    
二、实验拓扑
准备1台虚拟机,通过NAT模式将虚拟机接入互联网,作为docker平台主机。
    node1(192.168.10.10/24),默认网关192.168.10.2,DNS服务器192.168.10.2
准备另1台虚拟机,作为测试应用访问的客户机。
    svr100(192.168.10.100/24),安装Elinks或Firefox浏览器。


三、系统环境+软件仓库
1)若使用CentOS7.6.1810本地光盘YUM源
[root@node1 ~]# yum  repolist 
源标识   源名称   状态
repo_cos7dvd   added from: file:///repo/cos7dvd 4,021
repolist: 4,021

2)或者,若使用CentOS7.6.1810官方默认YUM源
[root@node1 ~]# yum  repolist 
源标识   源名称   状态
base/7/x86_64 CentOS-7 - Base 10,019
extras/7/x86_64 CentOS-7 – Extras 419
updates/7/x86_64 CentOS-7 – Updates 2,231
repolist: 12,669


四、安装docker平台
1. 安装docker平台容器包
可以在docker-engine(旧版)、docker-ce(新社区版)中任选其一即可。
1)新社区版docker-ce的安装
离线安装:
[root@node1 ~]# yum  -y  install  /root/docker-ce-18.09/*.rpm //离线安装docker-ce
在线安装:
[root@node1 ~]# vim  /etc/yum.repos.d/docker-ce.repo   //添加docker-ce源
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=
enabled=1
gpgcheck=1
gpgkey=

[root@node1 ~]# yum  -y  install  docker-ce //直接yum在线安装
2)旧版docker-engine的安装(备用)
离线安装:
[root@node1 ~]# yum  -y  install  /root/docker-engine-17.05/*.rpm //离线安装docker-engine
在线安装:
[root@node1 ~]# vim  /etc/yum.repos.d/docker-engine.repo   //添加docker-engine源
[docker]
name=Docker Repository
baseurl=
enabled=1
gpgcheck=1
gpgkey=

[root@node1 ~]# yum  install  docker-engine //直接yum在线安装

2. 启用docker服务
1)设置docker服务自动运行,并立即启动
[root@node1 ~]# systemctl  enable  docker  --now
2)查看docker主程序版本
[root@node1 ~]# docker  -v //查看简要信息
Docker version 17.05.0-ce, build 89658be
[root@node1 ~]# docker version   //查看详细版本信息
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:06:25 2017
 OS/Arch:      linux/amd64


Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:06:25 2017
 OS/Arch:      linux/amd64
 Experimental: false
3)查看docker主机信息
[root@node1 ~]# docker info
Containers: 1
 Running: 0
 Paused: 0
 Stopped: 1
Images: 4
Server Version: 18.09.7
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
.. ..


五、管理镜像
1. 从仓库搜索xx镜像
可访问 了解官方镜像,或者直接搜索镜像:
[root@node1 ~]# docker  search  nginx
NAME       DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11659 [OK]
jwilder/nginx-proxy Automated Nginx reverse… 1623 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM … 724 [OK]
bitnami/nginx Bitnami nginx Docker Image 68 [OK]
.. ..


2. 从仓库下载xx镜像
1)配置国内镜像加速(推荐)
[root@node1 ~]# vim  /etc/docker/daemon.json //建立服务配置
{
  "registry-mirrors": [  "",  ""   ]
}
[root@node1 ~]# systemdtl  restart  docker //重启docker服务


[root@node1 ~]# docker info | grep -A2 "Registry Mirrors:" //确认更新结果
Registry Mirrors:
 /
 /
2)下载xx镜像
[root@node1 ~]# docker  pull  hello-world   //下载名为hello-world的镜像
[root@node1 ~]# docker  pull  nginx //下载名为nginx的镜像
.. .. 
3)查看本地镜像列表,检查下载结果
[root@node1 ~]# docker  images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f68d6e55e065 7 days ago 109MB
hello-world latest fce289e99eb9 6 months ago 1.84kB


3. 从xx备份文件导入镜像
1)从/root/docker-images/目录下导入centos.tar、ubuntu.tar镜像
[root@node1 ~]# docker  load  <  /root/docker-images/centos.tar
Loaded image: centos:latest
[root@node1 ~]# docker  load  <  /root/docker-images/ubuntu.tar
Loaded image: ubuntu:latest
2)查看本地镜像列表,检查导入结果
[root@node1 ~]# docker  images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f68d6e55e065 7 days ago 109MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
ubuntu latest 452a96d81c30 14 months ago 79.6MB
centos latest e934aafc2206 15 months ago 199MB


4. 为xx镜像设置新标记(根据ID或镜像名称定位)
1)为ID以fce2开头的镜像设置新标签
[root@node1 ~]# docker  tag  fce2  hello-world:1.0
2)查看本地镜像列表,检查设置结果
[root@node1 ~]# docker  images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f68d6e55e065 7 days ago 109MB
hello-world 1.0   fce289e99eb9 6 months ago 1.84kB
hello-world latest fce289e99eb9 6 months ago 1.84kB
ubuntu latest 452a96d81c30 14 months ago 79.6MB
centos latest e934aafc2206 15 months ago 199MB


5. 删除xx镜像(根据ID或镜像名称定位)
1)删除镜像 hello-world:1.0
[root@node1 ~]# docker  rmi  hello-world:1.0
Untagged: hello-world:1.0
2)查看本地镜像列表,检查删除结果
[root@node1 ~]# docker  images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f68d6e55e065 7 days ago 109MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
ubuntu latest 452a96d81c30 14 months ago 79.6MB
centos latest e934aafc2206 15 months ago 199MB


6. 将xx镜像导出为备份文件(.tar格式)
1)将名为nginx的镜像导出为/root/nginx.tar备份文件
[root@node1 ~]# docker  save  nginx  >  /root/nginx.tar
2)确认备份结果
[root@node1 ~]# ls  -lh  /root/nginx.tar
-rw-r--r--. 1 root root 108M 7月   9 18:56 /root/nginx.tar
[root@ct opt]# docker  import  /opt/nginx.tar.gz  nginx-new:latest
sha256:cf3ca2039174d88a14abf65ac0da0bdd1a0e27e958d21022b2f15495788df217
[root@ct opt]# docker  images  nginx-new
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-new           latest              cf3ca2039174        35 seconds ago      181.2 MB


六、管理容器
1. 运行hello-world测试容器
[root@node1 ~]# docker  run  hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.


To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 

For more examples and ideas, visit:
 

2. 启动xx容器
1)启动nginx容器,执行容器内的一条命令(nginx -v、grep root .. ..)后自动退出
[root@node1 ~]# docker  run  nginx  nginx  -v //检查nginx容器的程序版本
nginx version: nginx/1.17.1 

[root@node1 ~]# docker  run  nginx  nginx  -V //检查nginx容器的程序版本详细信息
nginx version: nginx/1.17.1
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1) 
built with OpenSSL 1.1.0j  20 Nov 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx .. ..
2)启动一个nginx容器,并进入容器内的/bin/bash命令行环境(-i 允许交互,-t 开启终端)
[root@node1 ~]# docker  run  -it  nginx  bash
root@ffb9ea8e2edd:/# ls  /etc/nginx/*conf*   //查找nginx配置文件
/etc/nginx/nginx.conf   //主配置

/etc/nginx/conf.d:
default.conf //其他配置
root@ffb9ea8e2edd:/# grep  root  /etc/nginx/nginx.conf /etc/nginx/conf.d/default.conf 
/etc/nginx/conf.d/default.conf:        root   /usr/share/nginx/html; //默认网页目录
/etc/nginx/conf.d/default.conf:        root   /usr/share/nginx/html;
/etc/nginx/conf.d/default.conf:    #    root           html;
/etc/nginx/conf.d/default.conf:    # deny access to .htaccess files, if Apache's document root


root@ffb9ea8e2edd:/# exit //退出容器
exit
[root@node1 ~]# 
3)在后台启动一个nginx容器(-d 在后台运行,-p 本地端口:容器端口)
将docker主机的8000端口映射到此nginx容器的80端口
[root@node1 ~]# docker  run  -d  -p  8000:80  nginx
b76bbae0ef9900597bfc119d8dd83263711d06def48b5a101ec212ba95aaed28


[root@node1 ~]# docker  ps   //列出活动中的容器
CONTAINER ID IMAGE COMMAND CREATED             STATUS PORTS NAMES
b76bbae0ef99 nginx "nginx -g 'daemon ..."   5 seconds ago       Up 4 seconds        0.0.0.0:8000->80/tcp   eloquent_goldstine


4)在后台启动一个nginx容器,将docker主机的8001端口映射到此nginx容器的80端口,将docker主机的/opt/webroot映射为此nginx容器的web目录(-v 本地目录:容器内目录):
[root@node1 ~]# mkdir  /opt/webroot //准备网页目录
[root@node1 ~]# echo "Docker Test" > /opt/webroot/index.html //准备默认测试网页
[root@node1 ~]# docker  run  -d  -p  8001:80  -v  /opt/webroot:/usr/share/nginx/html  nginx
4a44cdd3da013751a3f1c6e32862eb65d8f888995806c757dfbbcfb956e13c77 //启动容器


[root@node1 ~]# docker  ps   //列出活动中的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
efcde70395f6        nginx               "nginx -g 'daemon ..."   5 seconds ago       Up 4 seconds        0.0.0.0:8000->80/tcp   admiring_curran
4a44cdd3da01        nginx               "nginx -g 'daemon ..."   33 minutes ago      Up 33 minutes       0.0.0.0:8001->80/tcp   optimistic_dijkstra


3. 访问xx容器
1)连接到ID值以b76b开头的容器的命令行
[root@node1 ~]# docker  exec  -it  b76b  bash
root@b76bbae0ef99:/# service  nginx  status
[ ok ] nginx is running.
root@b76bbae0ef99:/# exit
exit
[root@node1 ~]#
2)从docker主机向ID值为b76b的容器传输文件
[root@node1 ~]# echo  AAAA  >  /root/a.html   //建立测试网页
[root@node1 ~]# docker  cp  /root/a.html  b76b:/usr/share/nginx/html/a.html //复制文件到容器
[root@node1 ~]# docker  exec  b76b  cat  /usr/share/nginx/html/a.html //确认结果
AAAA
3)通过映射端口访问容器中的Web服务
[root@node1 ~]# elinks  -dump     //浏览:8000默认首页
                               Welcome to nginx!
   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.
.. ..
[root@node1 ~]# elinks  -dump  a.html //访问指定网页
   AAAA


[root@node1 ~]# elinks  -dump   //浏览:8001默认首页
   Docker Test


4. 关闭/杀死、删除容器
1)关闭/杀死ID值为b76b的容器
[root@node1 ~]# docker  stop  b76b   //若要杀容器改用kill
b76b
[root@node1 ~]# docker  ps  -a | grep  b76b //检查xx容器状态
b76bbae0ef99        nginx               "nginx -g 'daemon ..."   30 minutes ago       Exited (0) 5 minutes ago                           eloquent_goldstine
2)重新启动被关闭的ID值为b76b的容器
[root@node1 ~]# docker  start  b76b   //启用已关闭的xx容器
b76b
[root@node1 ~]# docker ps  -a  | grep  b76b //检查xx容器状态
b76bbae0ef99        nginx               "nginx -g 'daemon ..."   32 minutes ago      Up 6 seconds                   0.0.0.0:8000->80/tcp   eloquent_goldstine
3)删除ID值为b76b的容器
[root@node1 ~]# docker  stop  b76b   //先关闭xx容器
b76b
[root@node1 ~]# docker  rm  b76b   //删除已关闭的xx容器
b76b
[root@node1 ~]# docker  ps  -a | grep  b76b   //检查删除结果
[root@node1 ~]#


5. 启动并测试centos、ubuntu容器
1)分别启动centos、ubuntu这两个操作系统容器(开启终端、在后台运行)
[root@node1 ~]# docker  run  -t -d  centos   //启动centos容器
eebf70c1c797e22993bbe0514e201b1f24ea1ab9b357977bcd1f5bada17ed753
[root@node1 ~]# docker  run  -t -d  ubuntu //启动ubuntu容器
8c437b5fa3de51654c511628cd691654b446f719ff4c692e57b7a351b9b80815
2)检查容器状态
[root@node1 ~]# docker  ps | egrep  'centos|ubuntu'
8c437b5fa3de        ubuntu              "/bin/bash"              3 minutes ago       Up 3 minutes                               stupefied_lumiere
eebf70c1c797        centos              "/bin/bash"              3 minutes ago       Up 3 minutes                               trusting_kepler
3)访问容器的命令行界面
[root@node1 ~]# docker  exec  -it eebf  bash //连接到eebf-centos命令行界面
[root@eebf70c1c797 /]# cat  /etc/redhat-release //检查系统版本
CentOS Linux release 7.4.1708 (Core) 
[root@eebf70c1c797 /]# exit
Exit


[root@node1 ~]# docker  exec  -it 8c43  bash //连接到8c43-ubuntu命令行界面
root@8c437b5fa3de:/# cat  /etc/lsb-release //检查系统版本
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS"
root@8c437b5fa3de:/# exit
exit
5)查看容器的IP地址信息
[root@node1 ~]# docker inspect eebf | grep '"IPAddress"'
            "IPAddress": "172.17.0.2",   //eebf-centos的IP地址
                    "IPAddress": "172.17.0.2",


[root@node1 ~]# docker inspect 8c43 | grep '"IPAddress"'   //8c43-centos命令行界面
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",




七、自定义镜像
1. 以xx容器为基础,commit存为新镜像(不推荐)
1)启动一个nginx容器
[root@node1 ~]# docker  run  -itd  nginx //启用容器
5021e86d92b683c438101853979dc8ed73dba7139d9ed30f66358bc99140f573
[root@node1 ~]# docker  ps //确认结果
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5021e86d92b6 nginx "nginx -g 'daemon of…"   31 seconds ago Up 30 seconds 80/tcp         adoring_boyd
2)访问ID以5021开头的容器,修改nginx默认Web首页内容
[root@node1 ~]# docker exec -it 5021 bash //连接5021容器
root@5021e86d92b6:/# echo  "Nginx Test Site."  >  /usr/share/nginx/html/index.html   //修改Web页
root@5021e86d92b6:/# exit //退出
exit
[root@node1 ~]#
3)另存为新镜像nginx-test3(若不指定新名称,则更新到原镜像)
[root@node1 ~]# docker  commit  5021  nginx-test3 //提交更新,另存为新镜像
sha256:4b8901568ffb5bf0ba4ec20900af9d7a90ce6df20bf6793ad666f588fa10d72a
[root@node1 ~]# docker  images //检查新生成的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-test3 latest 4b8901568ffb 6 seconds ago 109MB
nginx latest f68d6e55e065 7 days ago 109MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
ubuntu latest 452a96d81c30 14 months ago 79.6MB
centos latest e934aafc2206 15 months ago 199MB
4)以新镜像nginx-test3启动一个容器,访问网页测试效果
[root@node1 ~]# docker  run  -itd  -p  8003:80  nginx-test3
a998f3e2b0f6643660151f79b129987aa5fe40889a269c24883f9a7fa6237c85
[root@node1 ~]# elinks  -dump  
   Nginx Test Site.
[root@node1 ~]#


2. 以xx镜像为基础,通过dockerfile定制新镜像
1)准备镜像构建临时目录、准备要复制到镜像中的测试文件
[root@node1 ~]# mkdir  /root/tmpdir
[root@node1 ~]# echo  TTEESSTT  >  /root/tmpdir/test.txt
2)编写好镜像构建配置文件
[root@node1 ~]# vim  /root/tmpdir/dockerfile
FROM centos
MAINTAINER TsengYia xxxx@yyyy.zzz
WORKDIR /var/www/html
ENV LSB_RELEASE="CentOS 7.4"
ADD test.txt  /root/new.txt
RUN yum -y install httpd && echo "It's OK"  > /var/www/html/index.html
EXPOSE 80
CMD ["httpd", "-DFOREGROUND"]
    
!!!! dockerfile常用配置语法
    FROM:指定以哪一个镜像为基础
    MAINTAINER:镜像创建者信息
    WORKDIR:定义容器的默认工作目录(若镜像中无此目录,构建时会自动创建)
    ENV:设置环境变量
    COPY:复制文件到镜像(若需直接释放.tar.gz类的文件到镜像中,请改用ADD指令)
    RUN:在镜像中执行的命令,可以有多条RUN(尽量少,必要时可以&&合并)
    EXPOSE:说明开放了哪个端口
    CMD:容器启动时执行的主命令,仅可以有一条CMD
    
3)以centos镜像为基础,建立新镜像centos-test1
[root@node1 ~]# docker  build  -t  centos-test1  /root/tmpdir/
Sending build context to Docker daemon  3.072kB
Sending build context to Docker daemon  3.072kB
Step 1/8 : FROM centos
 ---> e934aafc2206
Step 2/8 : MAINTAINER TsengYia xxxx@yyyy.zzz
 ---> Using cache
 ---> 04adfd7012bb
.. ..
Step 7/8 : EXPOSE 80
 ---> Running in d86f330e7aee
Removing intermediate container d86f330e7aee
 ---> ba743a485520
Step 8/8 : CMD ["httpd", "-DFOREGROUND"]
 ---> Running in 10bb84fb32c7
Removing intermediate container 10bb84fb32c7
 ---> 8bcf79b4e22f
Successfully built 8bcf79b4e22f
Successfully tagged centos-test1:latest


[root@node1 ~]# docker  images //检查新生成的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-test1 latest 8e9d31184dc3 About a minute ago 334MB
nginx-test3 latest 4b8901568ffb 6 seconds ago 109MB
nginx latest f68d6e55e065 7 days ago 109MB
hello-world latest fce289e99eb9 6 months ago 1.84kB
ubuntu latest 452a96d81c30 14 months ago 79.6MB
centos latest e934aafc2206 15 months ago 199MB
4)以新镜像centos-test1启动一个容器,确认容器运行效果
[root@node1 ~]# docker  run  -itd  -p  8004:80  centos-test1
2e75066121c6937e5d84c8b0131aa8a37e24ecaaa4f42da0dee8efc1fa37a348
[root@node1 ~]# docker  exec  2e75  env | egrep 'LSB|AUTHOR' //检查容器中的环境变量
Author=TsengYia
LSB_RELEASE=CentOS 7.4
[root@node1 ~]# docker  exec  2e75  cat  /root/new.txt   //检查容器中的文件
TTEESSTT
[root@node1 ~]# docker  exec  2e75  cat  /var/www/html/index.html //检查容器中的文件
It's OK
[root@node1 ~]# elinks  -dump   //访问容器的Web服务
   It's OK
[root@node1 ~]#




八、获取docker相关指令帮助
1. 查看docker指令清单
[root@node1 opt]# docker
Usage: docker [OPTIONS] COMMAND [arg...]
       docker [ --help | -v | --version ]
.. ..
Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders between a container and the local filesystem
.. ..


2. 获取具体指令的帮助
[root@node1 ~]# man  docker-images
[root@node1 ~]# man  docker-run
[root@node1 ~]# man  docker-cp
[root@node1 ~]# man  docker-ps
[root@node1 ~]# man  docker-start
[root@node1 ~]# man  docker-stop
[root@node1 ~]# man  docker-kill
[root@node1 ~]# man  docker-exec








阅读(1067) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~