标签:加速 false ref xxxxxx 常用 打包 有助于 性能测试 统一
1.1.1 介绍
容纳其它物品的工具,可以部分或完全封闭,被用于容纳、储存、运输物品。物体可以被放置在容器中,而容器则可以保护内容物。
1.1.2 要使用容器必须需要在内核级支持2中技术
(1)为什么centos6 版本不能使用容器?
因为centos6 内核版本是2.6;容器需要一个user的名称空间,直到内核3.8版本才有:
namespace | 系统调用参数 | 隔离内容 | 内核版本 |
UTS | CLONE_NEWUTS | 主机名和域名 | 2.6.19 |
IPC | CLONE_NEWIPC | 信号量、消息队列和共享内存 | 2.6.19 |
PID | CLONE_NEWPID | 进程编号 | 2.6.24 |
Network | CLONE_NEWNET | 网络设备、网络栈、端口等 | 2.6.29 |
Mount | CLONE_NEWNS | 挂载点(文件系统) | 2.4.19 |
User | CLONE_NEWUSER | 用户和用户组 | 3.8 |
(2)Control Group(cgroups)
1.2.1 介绍
1.2.2 docker 运行架构
(1)依赖的基础环境
(2)查询自己服务器的环境
① 使用的服务器版本
[root@along ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
② 内核版本
[root@along ~]# uname -r
3.10.0-514.el7.x86_64
③ ip地址
[root@along ~]# hostname -I
192.168.130.101 192.168.10.101
实际上就是下载一个安装脚本,再执行安装(不推荐,因为不能选择版本安装)
[root@centos7 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
(1)添加docker-ce 源信息
[root@along ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
(2)修改docker-ce 源
[root@along ~]# sed -i ‘s@download.docker.com@mirrors.tuna.tsinghua.edu.cn/docker-ce@g‘ /etc/yum.repos.d/docker-ce.repo
(3)更新并安装 Docker-CE
[root@centos7 ~]# yum makecache fast
[root@centos7 ~]# yum -y install docker-ce 安装的是默认最新版本
(4)安装指定版本的docker
① 查看都有哪些版本
[root@centos7 ~]# yum list docker-ce.x86_64 --showduplicates | sort -r
② 下载指定版本,我这里下载的稳定版本
[root@centos7 ~]# yum -y install docker-ce-17.03.2.ce
③ 安装报错(虚拟机中可能会遇到,如果没有报错请忽略)
Error: Package: docker-ce-18.03.1.ce-1.el7.centos.x86_64 (docker-ce-stable)
Requires: container-selinux >= 2.9
报错原因: docker-ce-selinux 版本过低
解决办法:在https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/网站下载对应版本的docker-ce-selinux,安装即可
[root@along ~]# yum -y install https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm
④ 再次安装docker 成功
[root@centos7 ~]# yum -y install docker-ce-17.03.2.ce
(1)安装最新版本
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
(2)安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
# docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]
多种加速方式:
(1)docker cn 加速
[root@along ~]# mkdir -p /etc/docker
[root@along ~]# sudo tee /etc/docker/daemon.json <<-‘EOF‘
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
(2)阿里云加速器
① 注册阿里云账号,专用加速器地址获得路径:
https://cr.console.aliyun.com/#/accelerator
② 添加加速器到配置文件
[root@along ~]# sudo tee /etc/docker/daemon.json <<-‘EOF‘
{
"registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}
EOF
③ 阿里云页面有操作步骤
① 重载docker启动配置
[root@along ~]# systemctl daemon-reload
② 将docker设为开机自启
[root@along ~]# systemctl start docker.service
③ 启动docker服务
[root@along ~]# systemctl enable docker.service
④ 查看docker版本
[root@along ~]# docker version
Client:
Version: 17.03.2-ce
API version: 1.27
Go version: go1.7.5
Git commit: f5ec1e2
Built: Tue Jun 27 02:21:36 2017
OS/Arch: linux/amd64
Server:
Version: 17.03.2-ce
API version: 1.27 (minimum version 1.12)
Go version: go1.7.5
Git commit: f5ec1e2
Built: Tue Jun 27 02:21:36 2017
OS/Arch: linux/amd64
Experimental: false
[root@along ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 10659 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1497 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 671 [OK]
搜索结果解释:
参数 |
说明 |
NAME |
镜像名称 |
DESCRIPTION |
镜像说明 |
STARS |
点赞数量 |
OFFICIAL |
是否是官方的 |
AUTOMATED |
是否是自动构建的 |
(1)根据镜像名称(tag指定版本)拉取镜像
[root@along ~]# docker pull nginx:1.14-alpine
1.14-alpine: Pulling from library/nginx
cd784148e348: Pull complete
12b08f7ef616: Pull complete
65071a4e699c: Pull complete
9936647427be: Pull complete
Digest: sha256:e3f77f7f4a6bb5e7820e013fa60b96602b34f5704e796cfd94b561ae73adcf96
Status: Downloaded newer image for nginx:1.14-alpine
[root@along ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
b4a6e23922dd: Pull complete
Digest: sha256:8ccbac733d19c0dd4d70b4f0c1e12245b5fa3ad24758a11035ee505c629c0796
Status: Downloaded newer image for busybox:latest
注:alpine 版本:构建容器小镜像的发型版本
(2)查看当前主机镜像列表
[root@along ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 758ec7f3a1ee 7 days ago 1.15 MB
nginx 1.14-alpine c5b6f731fbc0 13 days ago 17.7 MB
[root@along ~]# docker image save busybox > docker-busybox.tar.gz
[root@along ~]# ls docker-busybox.tar.gz
docker-busybox.tar.gz
[root@along ~]# docker image rm busybox
Untagged: busybox:latest
Untagged: busybox@sha256:8ccbac733d19c0dd4d70b4f0c1e12245b5fa3ad24758a11035ee505c629c0796
Deleted: sha256:758ec7f3a1ee85f8f08399b55641bfb13e8c1109287ddc5e22b68c3d653152ee
Deleted: sha256:23bc2b70b2014dec0ac22f27bb93e9babd08cdd6f1115d0c955b9ff22b382f5a
[root@along ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14-alpine c5b6f731fbc0 13 days ago 17.7 MB
[root@along ~]# docker image load -i docker-busybox.tar.gz
23bc2b70b201: Loading layer 1.37 MB/1.37 MB
Loaded image: busybox:latest
[root@along ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 758ec7f3a1ee 7 days ago 1.15 MB
nginx 1.14-alpine c5b6f731fbc0 13 days ago 17.7 MB
[root@docker01 ~]# docker image inspect centos
方法1(不推荐):
先创建一个容器:docker create 镜像名
再启动容器:docker start 容器名
方法2:docker run 镜像名
(1)格式
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
(2)options 常用命令选项
(3)示例:运行一个容器
[root@along ~]# docker run --name web1 -d -p 8888:80 nginx:1.14-alpine
ced78e522fd747635e9af01bc20882094e3b55ce50b9ae248962e8e8eeb89774
[root@along ~]# docker run --name b1 -it busybox /bin/sh 在运行容器时,交互式进入容器
/ # ls /
bin dev etc home proc root sys tmp usr var
/ # exit 退出
(4)查询容器运行状态命令
[root@along ~]# docker ps / docker container ls 两个命令是一样的效果
-a:查询所有的容器
注:容器内的第一个进程必须一直处于运行的状态,否则这个容器,就会处于退出状态!
[root@along ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ced78e522fd7 nginx:1.14-alpine "nginx -g ‘daemon ..." 5 minutes ago Up 5 minutes 0.0.0.0:8888->80/tcp web1
[root@along ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcbf3d772a65 nginx:1.14-alpine "nginx -g ‘daemon ..." 3 minutes ago Up 3 minutes 0.0.0.0:8888->80/tcp web1
9621f704b756 busybox "/bin/sh" 3 minutes ago Exited (0) 3 minutes ago b1
docker stop 关闭运行的容器
docker kill 杀死运行的容器
-s:指定信号,和kill 用法一样;-9 强制停止容器
[root@along ~]# docker kill web1
web1
[root@along ~]# docker ps 只显示运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@along ~]# docker ps -a 查询所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcbf3d772a65 nginx:1.14-alpine "nginx -g ‘daemon ..." 5 minutes ago Exited (137) 1 second ago web1
9621f704b756 busybox "/bin/sh" 5 minutes ago Exited (0) 5 minutes ago b1
docker start
(1)格式
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
(2)Options:
(3)示例
[root@along ~]# docker start web1
web1
[root@along ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ced78e522fd7 nginx:1.14-alpine "nginx -g ‘daemon ..." 9 minutes ago Up 8 seconds 0.0.0.0:8888->80/tcp web1
[root@along ~]# docker inspect web1 [root@along ~]# docker inspect web1 |grep "IPAddress" 比如我查询到容器的ip "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", [root@along ~]# curl 172.17.0.2 通过容器的IP,在宿主机上访问服务 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> [root@along ~]# curl 127.0.0.1:8888 映射到宿主机的端口是8888 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1>
[root@along ~]# docker kill web1 先关闭容器,再删除容器
b1
[root@along ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@along ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcbf3d772a65 nginx:1.14-alpine "nginx -g ‘daemon ..." 6 minutes ago Exited (137) 2 seconds ago web1
9621f704b756 busybox "/bin/sh" 6 minutes ago Exited (0) 6 minutes ago b1
[root@along ~]# docker rm web1
web1
[root@along ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9621f704b756 busybox "/bin/sh" 6 minutes ago Exited (0) 6 minutes ago b1
[root@along ~]# docker rm -f `docker ps -a -q` 删除所有容器,-f 强制删除
9621f704b756
[root@along ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@along ~]# docker exec / docker container exec
(1)格式
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
(2)options 选项
(3)示例
[root@along ~]# docker run --name web1 -d nginx:1.14-alpine 81f336e878c0fb3187596f2acd12705d94f532978a8ad37c9f8ae33cc39bfb61 ① 交互式进入容器 [root@along ~]# docker exec -it web1 /bin/sh / # ls / bin etc lib mnt root sbin sys usr dev home media proc run srv tmp var / # exit ② 查询ip [root@along ~]# docker exec web1 ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 43: eth0@if44: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fe11:2/64 scope link valid_lft forever preferred_lft forever
[root@along ~]# curl 172.17.0.2
[root@along ~]# docker logs web1
172.17.0.1 - - [03/Jan/2019:09:00:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
标签:加速 false ref xxxxxx 常用 打包 有助于 性能测试 统一
原文地址:https://www.cnblogs.com/along21/p/10215701.html