一、dcoker的一般应用场景
1、简化配置,统一配置,通过镜像快速启动(Simplifying)
2、代码流水线管理,开发环境->测试环境->预生产环境->灰度发布->正式发布,docker在这里实现了快速迁移(Code Oioeline Management)
3、开发效率,对开发人员,有了镜像,直接启动容器即可(Developer Productivity)
4、应用隔离,相对于虚拟机的完全隔离会占用资源,docker会比较节约资源(Applsolation)
5、服务器整合,一台服务器跑多个docker容器,提高服务器的利用率(Server Consolidation)
6、调试能力,debug调试(Debugging Capabilties)
7、多租户,一个租户多个用户,类似于阿里公有云的一个project下多个用户(Multi-tenancy)
8、快速部署,不需要启动操作系统,实现秒级部署(Rapid Deplovment)
二、docker在生产中的优点
1.Docker更快速的交付和部署
2.Docker更高效的虚拟化
3.Docker更轻松的迁移和扩展
4.Docker更简单的管理
三、企业实战:
(一)、环境
[[email protected]~]# dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product"
Manufacturer: HP
Product Name: ProLiant DL380 Gen9
[[email protected] ~]# uname -a
Linux linux-node2 3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[[email protected]~]# ~]# docker version
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:20:16 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:23:58 2018
OS/Arch: linux/amd64
Experimental: false
(二)常用指令
docker --help
Usage:
docker [OPTIONS] COMMAND [arg...]
docker daemon [ --help | ... ]
docker [ --help | -v | --version ]
A
self-sufficient runtime for containers.
Options:
--config=~/.docker Location of client config files #客户端配置文件的位置
-D, --debug=false Enable debug mode #启用Debug调试模式
-H, --host=[] Daemon socket(s) to connect to #守护进程的套接字(Socket)连接
-h, --help=false Print usage #打印使用
-l, --log-level=info Set the logging level #设置日志级别
--tls=false Use TLS; implied by--tlsverify #
--tlscacert=~/.docker/ca.pem Trust certs signed only by this CA #信任证书签名CA
--tlscert=~/.docker/cert.pem Path to TLS certificate file #TLS证书文件路径
--tlskey=~/.docker/key.pem Path to TLS key file #TLS密钥文件路径
--tlsverify=false Use TLS and verify the remote #使用TLS验证远程
-v, --version=false Print version information and quit #打印版本信息并退出
Commands:
attach Attach to a running container #当前shell下attach连接指定运行镜像
build Build an image from a Dockerfile #通过Dockerfile定制镜像
commit Create a new image from a container‘s changes #提交当前容器为新的镜像
cp Copy files/folders from a container to a HOSTDIR or to STDOUT #从容器中拷贝指定文件或者目录到宿主机中
create Create a new container #创建一个新的容器,同run 但不启动容器
diff Inspect changes on a container‘s filesystem #查看docker容器变化
events Get real time events from the server#从docker服务获取容器实时事件
exec Run a command in a running container#在已存在的容器上运行命令
export Export a container‘s filesystem as a tar archive #导出容器的内容流作为一个tar归档文件(对应import)
history Show the history of an image #展示一个镜像形成历史
images List images #列出系统当前镜像
import Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像(对应export)
info Display system-wide information #显示系统相关信息
inspect Return low-level information on a container or image #查看容器详细信息
kill Kill a running container #kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包中加载一个镜像(对应save)
login Register or log in to a Docker registry#注册或者登陆一个docker源服务器
logout Log out from a Docker registry #从当前Docker registry退出
logs Fetch the logs of a container #输出当前容器日志信息
pause Pause all processes within a container#暂停容器
port List port mappings or a specific mapping for the CONTAINER #查看映射端口对应的容器内部源端口
ps List containers #列出容器列表
pull Pull an image or a repository from a registry #从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to a registry #推送指定镜像或者库镜像至docker源服务器
rename Rename a container #重命名容器
restart Restart a running container #重启运行的容器
rm Remove one or more containers #移除一个或者多个容器
rmi Remove one or more images #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除)
run Run a command in a new container #创建一个新的容器并运行一个命令
save Save an image(s) to a tar archive#保存一个镜像为一个tar包(对应load)
search Search the Docker Hub for images #在docker
hub中搜索镜像
start Start one or more stopped containers#启动容器
stats Display a live stream of container(s) resource usage statistics #统计容器使用资源
stop Stop a running container #停止容器
tag Tag an image into a repository #给源中镜像打标签
top Display the running processes of a container #查看容器中运行的进程信息
unpause Unpause all processes within a container #取消暂停容器
version Show the Docker version information#查看容器版本号
wait Block until a container stops, then print its exit code #截取容器停止时的退出状态值
Run ‘docker COMMAND --help‘ for more information on a command. #运行docker命令在帮助可以获取更多信息
(三)安装dokcer镜像
$docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 4268 [OK]
ansible/centos7-ansible Ansible on Centos7 109 [OK]
.......
第一个是官方镜像。
# docker pull centos 获取镜像
新版本默认下载的是最新版本,老版本docker会把所甩centos版本下下来
$ docker pull centos 下载镜像
Using default tag: latest
latest: Pulling from library/centos
469cfcc7a4b3: Pull complete
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Status: Downloaded newer image for centos:latest
$ docker image list 查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest e38bc07ac18e 4 weeks ago 1.85kB ##hello-world 是安装docker时的测试镜像
centos latest e934aafc2206 4 weeks ago 199MB ##刚下载的镜像
$docker rmi imageID(e934aafc2206 ) #删除docker镜像
############
1、检查本地是否存在指定的镜像,不存在就从公有仓库下载 docker search
2、利用镜像创建并启动一个容器 docker run
3、分配一个文件系统,并在只读的镜像层外面挂在一层可读写层
4、从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去brcctl show
5、从地址池配置一个ip地址给容器
6、执行用户指定的应用程序
7、执行完毕后容器被终止 exit
################
$ docker run -dit --name first_image centos /bin/bash #-dit选项对应:后台,交互式,虚拟络端方式 --name是取的名称 centos是镜像名 /bin/bash启动镜像bash连接
33d75c630f22a6847ff960b659c7beca2dbc691684a0281dbc2cffb7ecd680f2
$ docker ps -a 查看所有container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33d75c630f22 centos "/bin/bash" 10 seconds ago Up 9 seconds first_image
e99ceea5c674 hello-world "/hello" 2 days ago Exited (0) 2 days ago cranky_euler
$docker attach 33d75c630f22
docker attach 33d75c630f22
[[email protected] /]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=127 time=40.0 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=127 time=41.1 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=127 time=40.3 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=127 time=41.7 ms
如果提示ipv4 forwarding错误需要修改配置文件
WARNING: IPv4 forwarding is disabled. Networking will not work.
error:xxxxxxx
...
解决办法:
在宿主机上面执行:
# net.ipv4.ip_forward=1 >> /usr/lib/sysctl.d/00-system.conf
重启network和docker服务
# systemctl restart network && systemctl restart docker
(四)在容器中安装一个NGINX应用:
1、更改yum源和epel源为aliyun
cp CentOS-Base.repo CentOS-Base.repo.bak
yum install -y wget
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
yum install -y nginx
退出container使用exit,如果想退出容器继续运行使用CTRL+P+Q键
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33d75c630f22 centos "/bin/bash" 20 minutes ago Exited (0) 10 seconds ago first_image
e99ceea5c674 hello-world "/hello" 2 days ago Exited (0) 2 days ago cranky_e
$docker save centos > /tmp/centos.tar.gz ##导出镜像到本地
$docker load < /tmp/centos.tar.gz ##导入镜像
$docker logs 33d75c630f22 获取容器输出信息
$docker rm 33d75c630f22 删除容器,如果容器正在运行需将容器停。-f选项是强制删除,可删除正在运行的容器。
使用nsenter方式连接
yum install -y util-linux
docker inspect --format "{{.State.Pid}}" 33d75c630f22
28994
nsenter -t 28994 -u -i -n -p
##PID=$(docker inspect --format "{{.State.Pid}}" $1)
##nsenter -t $PID -u -i -n -p
利用docker run来创建容器时,Docker在后台运行的标准操作包括:
2、docker目录方问
$docker run -dit --name volume_centos -v /volume_centos centos
$ docker inspect 32630009c6b5 |grep volume
"Name": "/volume_centos",
"Type": "volume",
"Source": "/var/lib/docker/volumes/cf07d4796477931afc32e041bf37ac65b75287642aa0a100d126a4e016afb68f/_data",
"Destination": "/volume_centos",
"/volume_centos": {}
在物理机上cd /var/lib/docker/volumes/cf07d4796477931afc32e041bf37ac65b75287642aa0a100d126a4e016afb68f/_data
touch helloworld.txt
在容器里:
ls -l /volume_centos
total 0
-rw-r--r--. 1 root root 0 May 11 11:47 123.txt
3、指定目录挂载
$docker run -dit --name volume_centos -v /volume_mount:/volume_mount centos ##指定目录挂载多用于开发环境
4、多容器共享目录:
$ docker run -it --name volume_share --volumes-from volume_mount centos
$ ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var volume_mount
$ ls -la volume_mount
total 0
drwxr-xr-x. 2 root root 37 May 11 12:15 .
drwxr-xr-x. 1 root root 26 May 11 12:13 ..
-rw-r--r--. 1 root root 0 May 11 12:15 123.txt
-rw-r--r--. 1 root root 0 May 11 12:06 haha.txt
原文地址:https://www.cnblogs.com/icerain0/p/9026470.html