赞
踩
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口,更重要的是容器性能开销极低。
Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。官网
Web 应用的自动化打包和发布。
自动化测试和持续集成、发布。
在服务型环境中部署和调整数据库或其他的后台应用。
从头编译或者扩展现有的 Openshellift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。
Docker 是一个用于开发,交付和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分开,从而可以快速交付软件。借助 Docker,您可以与管理应用程序相同的方式来管理基础架构。通过利用 Docker 的方法来快速交付,测试和部署代码,您可以大大减少编写代码和在生产环境中运行代码之间的延迟。
容器非常适合持续集成和持续交付(CI / CD)工作流程,请考虑以下示例方案:
您的开发人员在本地编写代码,并使用 Docker 容器与同事共享他们的工作。
他们使用 Docker 将其应用程序推送到测试环境中,并执行自动或手动测试。
当开发人员发现错误时,他们可以在开发环境中对其进行修复,然后将其重新部署到测试环境中,以进行测试和验证。
测试完成后,将修补程序推送给生产环境,就像将更新的镜像推送到生产环境一样简单。
Docker 的可移植性和轻量级的特性,还可以使您轻松地完成动态管理的工作负担,并根据业务需求指示,实时扩展或拆除应用程序和服务。
虚拟化技术特点:
容器化技术:容器化技术不是模拟的一个完整的操作系统
比较Docker和虚拟机的不同:
Docker的基本组成图如下:
说明:
查看系统的内核:
uname -r
系统内核版本为3.10.0
[root@iZwz99sm8v95sckz8bd2c4Z ~]# uname -r
3.10.0-957.21.3.el7.x86_64
查看系统配置
cat /etc/os-release
[root@iZwz99sm8v95sckz8bd2c4Z ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"
Docker的安装步骤:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo #国外的地址
# 设置阿里云的Docker镜像仓库
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #国内的地址
yum makecache fast
docker-ce 是社区版,docker-ee 企业版
yum install docker-ce docker-ce-cli containerd.io
出现了completed即安装成功。
systemctl start docker
# 查看当前版本号,是否启动成功
docker version
# 设置开机自启动
systemctl enable docker
结果:
下载hello-world镜像进行测试
查看下载的hello world镜像
[root@iZwz99sm8v95sckz8bd2c4Z lib]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 11 months ago 13.3kB
# 1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2. 删除资源 . /var/lib/docker是docker的默认工作路径
rm -rf /var/lib/docker
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://axvfsf7e.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
启动一个容器,Docker的运行流程如下图:
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socker从客户端访问!Docker Server接收到Docker-Client的指令,就会执行这个指令!
Docker为什么比VM Ware快?
1.Docker比虚拟机更少的抽象层
2.docker利用宿主机的内核,VM需要的是Guest OS
Docker新建一个容器的时候,不需要像虚拟机一样重新加载一个操作系统内核,直接利用宿主机的操作系统,而虚拟机是需要加载Guest OS。Docker和VM的对比如下:
docker version #查看docker的版本信息
docker info #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令(可查看可选的参数)
docker COMMAND --help
命令的帮助文档地址
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 11 months ago 13.3kB #解释: 1.REPOSITORY 镜像的仓库源 2.TAG 镜像的标签 3.IMAGE ID 镜像的id 4.CREATED 镜像的创建时间 5.SIZE 镜像的大小 # 可选参数 -a/--all 列出所有镜像 -q/--quiet 只显示镜像的id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10308 [OK] mariadb MariaDB is a community-developed fork of MyS… 3819 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 754 [OK] percona Percona Server is a fork of the MySQL relati… 517 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 86 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79 centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK] #可选参数 Search the Docker Hub for images Options: -f, --filter filter Filter output based on conditions provided --format string Pretty-print search using a Go template --limit int Max number of search results (default 25) --no-trunc Don't truncate output #搜索收藏数大于3000的镜像 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10308 [OK] mariadb MariaDB is a community-developed fordockerk of MyS… 3819 [OK]
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql Using default tag: latest #如果不写tag默认就是latest latest: Pulling from library/mysql 6ec7b7d162b2: Pull complete #分层下载,docker image的核心-联合文件系统 fedd960d3481: Pull complete 7ab947313861: Pull complete 64f92f19e638: Pull complete 3e80b17bff96: Pull complete 014e976799f9: Pull complete 59ae84fee1b3: Pull complete ffe10de703ea: Pull complete 657af6d90c83: Pull complete 98bfb480322c: Pull complete 6aa3859c4789: Pull complete 1ed875d851ef: Pull complete Digest: shella256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 #签名 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #下载来源的真实地址 #docker pull mysql等价于docker pull docker.io/library/mysql:latest
指定版本下载
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 6ec7b7d162b2: Already exists fedd960d3481: Already exists 7ab947313861: Already exists 64f92f19e638: Already exists 3e80b17bff96: Already exists 014e976799f9: Already exists 59ae84fee1b3: Already exists 7d1da2a18e2e: Pull complete 301a28b700b9: Pull complete 529dc8dbeaf3: Pull complete bc9d021dc13f: Pull complete Digest: shella256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7
#1.删除指定的镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id
#2.删除多个镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id 镜像id 镜像id
#3.删除全部的镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f $(docker images -aq)
docker pull centos
docker run [可选参数] image
#参数说明
--name="名字" 指定容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
(
-p ip:主机端口:容器端口 配置主机端口映射到容器端口
-p 主机端口:容器端口
-p 容器端口
)
-P 随机指定端口(大写的P)
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it centos /bin/bashell
[root@bd1b8900c547 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#exit 停止并退出容器(后台方式运行则仅退出)
#Ctrl+P+Q 不停止容器退出
[root@bd1b8900c547 /]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]#
#docker ps
# 列出当前正在运行的容器
-a # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bca129320bb5 centos "/bin/bashell" 4 minutes ago Exited (0) 3 minutes ago optimistic_shelltern
bd1b8900c547 centos "/bin/bashell" 6 minutes ago Exited (0) 5 minutes ago cool_tesla
cf6adbf1b506 bf756fb1ae65 "/hello" 5 hours ago Exited (0) 5 hours ago optimistic_darwin
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f
docker rm -f $(docker ps -aq) #删除所有的容器(慎用)
docker ps -a -q|xargs docker rm #删除所有的容器(慎用)
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止当前容器
#exit 停止并退出容器(后台方式运行则仅退出)
#Ctrl+P+Q 不停止容器退出
[root@bd1b8900c547 /]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]#
#docker ps
# 列出当前正在运行的容器
-a # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bca129320bb5 centos "/bin/bashell" 4 minutes ago Exited (0) 3 minutes ago optimistic_shelltern
bd1b8900c547 centos "/bin/bashell" 6 minutes ago Exited (0) 5 minutes ago cool_tesla
cf6adbf1b506 bf756fb1ae65 "/hello" 5 hours ago Exited (0) 5 hours ago optimistic_darwin
#docker ps
# 列出当前正在运行的容器
-a # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bca129320bb5 centos "/bin/bashell" 4 minutes ago Exited (0) 3 minutes ago optimistic_shelltern
bd1b8900c547 centos "/bin/bashell" 6 minutes ago Exited (0) 5 minutes ago cool_tesla
cf6adbf1b506 bf756fb1ae65 "/hello" 5 hours ago Exited (0) 5 hours ago optimistic_darwin
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止当前容器
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs --help Usage: docker logs [OPTIONS] CONTAINER Fetch the logs of a container Options: --details shellow extra details provided to logs -f, --follow Follow log output --since string shellow logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) -n, --tail string Number of lines to shellow from the end of the logs (default "all") -t, --timestamps shellow timestamps --until string shellow logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) 常用: docker logs -tf 容器id docker logs --tail number 容器id #num为要显示的日志条数 #docker容器后台运行,必须要有一个前台的进程,否则会自动停止 #编写shellell脚本循环执行,使得centos容器保持运行状态 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d centos /bin/shell -c "while true;do echo hi;sleep 5;done" c703b5b1911ff84d584390263a35707b6024816e1f46542b61918a6327a570dc [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c703b5b1911f centos "/bin/shell -c 'while t…" 13 seconds ago Up 10 seconds pedantic_banach [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs -tf --tail 10 c703b5b1911f 2020-12-27T03:34:07.255599560Z hi 2020-12-27T03:34:12.257641517Z hi 2020-12-27T03:34:17.259706294Z hi 2020-12-27T03:34:22.261693707Z hi 2020-12-27T03:34:27.262609289Z hi 2020-12-27T03:34:32.267862677Z hi 2020-12-27T03:34:37.270382873Z hi 2020-12-27T03:34:42.272414182Z hi 2020-12-27T03:34:47.274823243Z hi 2020-12-27T03:34:52.277419274Z hi
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker top 容器ID
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker inspect 容器ID
因为通常我们的容器都是使用后台方式来运行的,有时需要进入容器修改配置
方式一:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it 容器ID /bin/bashell
[root@c703b5b1911f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@c703b5b1911f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:31 ? 00:00:00 /bin/shell -c while true;do echo hi;sleep 5;done
root 279 0 0 03:54 pts/0 00:00:00 /bin/bashell
root 315 1 0 03:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shellebang=sleep /usr/bin/sleep 5
root 316 279 0 03:56 pts/0 00:00:00 ps -ef
方式二:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker attach 容器ID
#拷贝容器的文件到主机中
docker cp 容器id:容器内路径 目的主机路径
#拷贝宿主机的文件到容器中
docker cp 目的主机路径 容器id:容器内路径
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bashell
[root@c703b5b1911f /]# cd home
[root@c703b5b1911f home]# ls
#touch 新建文件
[root@c703b5b1911f home]# touch test.java
[root@c703b5b1911f home]# ls
test.java
[root@c703b5b1911f home]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c703b5b1911f centos "/bin/shell -c 'while t…" 35 minutes ago Up 35 minutes pedantic_banach
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker cp c703b5b1911f:/home/test.java /home
[root@iZwz99sm8v95sckz8bd2c4Z ~]# ls /home
hai pan test.java
命令小节的图解如下:
Portaniner是Docker的图形化管理工具,类似的工具还有Rancher(CI/CD再用)
下载运行Portaniner镜像并运行,设置本机映射端口为8088
[root@localhost conf]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
94cfa856b2b1: Pull complete
49d59ee0881a: Pull complete
a2300fd28637: Pull complete
Digest: shella256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
8c525a0137be22965bd1e3944da622a2c4248f8ad20883f4b3ea4f8a6b11e163
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7789d4505a00 portainer/portainer "/portainer" 6 seconds ago Up 5 seconds 0.0.0.0:8088->9000/tcp quirky_sinoussi
第一次登录设置admin用户的密码
如果是阿里云服务器记得设置安全组,选择连接本地的Docker,整体界面预览如下图:
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需要的所有内容,包括代码,运行时(一个程序在运行或者在被执行的依赖)、库,环境变量和配置文件。
Docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统是UnionFS联合文件系统。
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker image inspect nginx:latest [ { "Id": "shella256:ae2feff98a0cc5095d97c6c283dcd33090770c76d63877caa99aefbbe4343bdd", "RepoTags": [ "nginx:latest" ], "RepoDigests": [ "nginx@shella256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9" ], "Parent": "", "Comment": "", "Created": "2020-12-15T20:21:00.007674532Z", "Container": "4cc5da85f27ca0d200407f0593422676a3bab482227daee044d797d1798c96c9", "ContainerConfig": { "Hostname": "4cc5da85f27c", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.19.6", "NJS_VERSION=0.5.0", "PKG_RELEASE=1~buster" ], "Cmd": [ "/bin/shell", "-c", "#(nop) ", "CMD [\"nginx\" \"-g\" \"daemon off;\"]" ], "Image": "shella256:13bffe371b56f4aeed88218ec17d0c6f653a83b49bd3e211fc8cfa2ca5d7a3d3", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.shell" ], "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGQUIT" }, "DockerVersion": "19.03.12", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.19.6", "NJS_VERSION=0.5.0", "PKG_RELEASE=1~buster" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "shella256:13bffe371b56f4aeed88218ec17d0c6f653a83b49bd3e211fc8cfa2ca5d7a3d3", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.shell" ], "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGQUIT" }, "Architecture": "amd64", "Os": "linux", "Size": 132935043, "VirtualSize": 132935043, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/cb791e78a08db7091bf2ce1d78603f1758f52199e57f1805156fe30e39067aae/diff:/var/lib/docker/overlay2/1e73a72b25af68ee9abf4eb443f778d31226e12e9af428fcc14c7b044c83b258/diff:/var/lib/docker/overlay2/88c9c01762f2af8327db65d0b0d4a64785e87c9c2ab76c62e7d03619db03a985/diff:/var/lib/docker/overlay2/7304ab112ac4a9cb91fc6f74730be28fecbe19f042e92d321aa9181424cc4b2e/diff", "MergedDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/merged", "UpperDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/diff", "WorkDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "shella256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9", "shella256:5c4e5adc71a82a96f02632433de31c998c5a9e2fccdcbaee780ae83158fac4fa", "shella256:7d2b207c26790f693ab1942bbe26af8e2b6a14248969e542416155a912fec30d", "shella256:2c7498eef94aef8c40d106f3e42f7da62b3eee8fd36012bf7379becc4cd639a2", "shella256:4eaf0ea085df254fd5d2beba4e2c11db70a620dfa411a8ad44149e26428caee4" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ]
这里指示了分层信息:
"RootFS": {
"Type": "layers",
"Layers": [
"shella256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9",
"shella256:5c4e5adc71a82a96f02632433de31c998c5a9e2fccdcbaee780ae83158fac4fa",
"shella256:7d2b207c26790f693ab1942bbe26af8e2b6a14248969e542416155a912fec30d",
"shella256:2c7498eef94aef8c40d106f3e42f7da62b3eee8fd36012bf7379becc4cd639a2",
"shella256:4eaf0ea085df254fd5d2beba4e2c11db70a620dfa411a8ad44149e26428caee4"
]
},
使用docker commit 命令提交容器成为一个新的版本
docker commit -m=“提交的描述信息” -a="作者" 容器id 目标镜像名:[TAG]
由于默认的Tomcat镜像的webapps文件夹中没有任何内容,需要从webapps.dist中拷贝文件到webapps文件夹。下面自行制作镜像:就是从webapps.dist中拷贝文件到webapps文件夹下,并提交该镜像作为一个新的镜像。使得该镜像默认的webapps文件夹下就有文件。具体命令如下:
#1.复制文件夹 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it tomcat /bin/bashell root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../ root@2a3bf3eaa2e4:/usr/local/tomcat# cp -r webapps.dist/* webapps root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a3bf3eaa2e4 tomcat "/bin/bashell" 4 minutes ago Up 4 minutes 8080/tcp competent_torvalds 7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it 2a3bf3eaa2e4 /bin/bashell root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../ root@2a3bf3eaa2e4:/usr/local/tomcat# read escape sequence [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a3bf3eaa2e4 tomcat "/bin/bashell" 8 minutes ago Up 8 minutes 8080/tcp competent_torvalds 7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi #2.提交镜像作为一个新的镜像 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker commit -m="add webapps" -a="Ethan" 2a3bf3eaa2e4 mytomcat:1.0 shella256:f189aac861de51087af5bc88a5f1de02d9574e7ee2d163c647dd7503a2d3982b [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mytomcat 1.0 f189aac861de 7 seconds ago 653MB mysql 5.7 f07dfa83b528 6 days ago 448MB tomcat latest feba8d001e3f 10 days ago 649MB nginx latest ae2feff98a0c 12 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB #3.运行容器 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it mytomcat:1.0 /bin/bashell root@1645774d4605:/usr/local/tomcat# cd webapps root@1645774d4605:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager wz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mytomcat 1.0 f189aac861de 7 seconds ago 653MB mysql 5.7 f07dfa83b528 6 days ago 448MB tomcat latest feba8d001e3f 10 days ago 649MB nginx latest ae2feff98a0c 12 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 14207 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1932 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 797 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 137 jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 123 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 107 [OK] [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 6ec7b7d162b2: Already exists cb420a90068e: Pull complete 2766c0bf2b07: Pull complete e05167b6a99d: Pull complete 70ac9d795e79: Pull complete Digest: shella256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images; REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 f07dfa83b528 5 days ago 448MB nginx latest ae2feff98a0c 11 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB
可以到dockerhub官网查看Nginx的详细版本信息
docker run -d --name nginx01 -p 3334:80 nginx
-d 后台运行
--name 给容器命名
-p 3334:80 将宿主机的端口3334映射到该容器的80端口
运行结果:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bashell
Error: No such container: nginx01
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d --name nginx01 -p 3334:80 nginx
20c896637ff5de8be835797109d62ee2465e28d9d716be5a8d550ef7d547fcf5
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20c896637ff5 nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:3334->80/tcp nginx01
端口暴露的概念
配置文件
进入容器,自定义配置文件
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bashell
root@20c896637ff5:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/shellare/nginx
root@20c896637ff5:/# cd /etc/nginx
root@20c896637ff5:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@20c896637ff5:/# cd /etc/nginx
root@20c896637ff5:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
访问测试
本地主机访问测试,curl命令发起请求,如果使用阿里云服务器需要设置安全组。
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20c896637ff5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:3334->80/tcp nginx01 [root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3334 <!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> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
安装vim
我们使用Nginx往往需要编写配置文件,但是Nginx官方镜像没有安装vim,需要我们手动进行安装。使用以下命令进行安装:
#检查是否已安装 rpm -qa|grep vim #如果已安装,会显示: [root@localhost usr]# rpm -qa|grep vim vim-minimal-7.4.629-6.el7.x86_64 vim-filesystem-7.4.629-6.el7.x86_64 vim-enhanced-7.4.629-6.el7.x86_64 vim-common-7.4.629-6.el7.x86_64 vim-X11-7.4.629-6.el7.x86_64 #安装 #如果缺少了其中某个,比如说: vim-enhanced这个包少了,则执行: yum -y install vim-enhanced #它会自动下载安装。如果上面三个包一个都没有显示,则直接输入命令: yum -y install vim* #配置 vim /etc/vimrc #打开文件后,按 i 进入编辑模式,然后找一个位置添加如下代码 set nu " 设置显示行号 set shellowmode " 设置在命令行界面最下面显示当前模式等 set ruler " 在右下角显示光标所在的行数等信息 set autoindent " 设置每次单击Enter键后,光标移动到下一行时与上一行的起始字符对齐 syntax on " 即设置语法检测,当编辑C或者shellell脚本时,关键字会用特殊颜色显示
ps: 由于到目前为止还没学到数据卷,具体的设置以及编写配置文件,你可以先到后面了解一下数据卷,再来编写配置文件
下载并运行
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 6c33745f49b4: Pull complete ef072fc32a84: Pull complete c0afb8e68e0b: Pull complete d599c07d28e6: Pull complete e8a829023b97: Pull complete d04be46a31d1: Pull complete db6007c69c35: Pull complete e4ad4c894bce: Pull complete 248895fda357: Pull complete 277059b4cba2: Pull complete Digest: shella256:57dae7dfb9b62a413cde65334c8a18893795cac70afc3be589c8336d8244655d Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d -p 3335:8080 --name tomcat01 tomcat 7136295a6082cb0f805b025a1471bde02ead4864be3e2c9dcd337b1dde0a3113
进入容器
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it tomcat01 /bin/bashell root@7136295a6082:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work root@7136295a6082:/usr/local/tomcat# cd webapps.dist root@7136295a6082:/usr/local/tomcat/webapps.dist# ls ROOT docs examples host-manager manager root@7136295a6082:/usr/local/tomcat/webapps.dist# cd ROOT root@7136295a6082:/usr/local/tomcat/webapps.dist/ROOT# ls RELEASE-NOTES.txt WEB-INF asf-logo-wide.svg bg-button.png bg-middle.png bg-nav.png bg-upper.png favicon.ico index.jsp tomcat.css tomcat.svg root@7136295a6082:/usr/local/tomcat/webapps.dist/ROOT# cd ../../ root@7136295a6082:/usr/local/tomcat# cd webapps root@7136295a6082:/usr/local/tomcat/webapps# ls root@7136295a6082:/usr/local/tomcat/webapps# cp -r /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps/ root@7136295a6082:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@7136295a6082:/usr/local/tomcat/webapps# exit exit
访问测试
[root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3335 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Apache Tomcat/9.0.41</title> <link href="favicon.ico" rel="icon" type="image/x-icon" /> <link href="tomcat.css" rel="styleshelleet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="navigation" class="curved container"> <span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span> <span id="nav-hosts"><a href="/docs/">Documentation</a></span> <span id="nav-config"><a href="/docs/config/">Configuration</a></span> <span id="nav-examples"><a href="/examples/">Examples</a></span> <span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span> <span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span> <span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span> <br class="separator" /> </div>
拉取Elasticsearch镜像
docker pull elasticsearch:7.7.0
查看镜像命令:docker images
删除镜像命令:docker rmi <镜像名:版本号>
or docker rmi <镜像id>
启动前准备
创建存放数据及配置文件的文件夹,启动时挂载。
mkdir -p /home/elasticsearch/data/
mkdir -p /home/elasticsearch/config/
解释:
编写配置文件
echo 'http.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*" '>>/home/elasticsearch/config/elasticsearch.yml
解释:
echo >> :是利用了shellell编程语法,使用echo >>指令向文件追加内容,原内容将保存
cat:展示文件内容。
http.cors.enabled: true
和http.cors.allow-origin: "*"
配置是为了解决跨域问题,因为等下还要下载可视化工具查看。
修改文件夹权限
chmod -R 777 /home/elasticsearch/
ls -l # 查看文件权限
启动elasticseach镜像
docker run --name elasticsearch -p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
-v /home/elasticsearch/config/elasticsearch.yml:/usr/shellare/elasticsearch/config/elasticsearch.yml \
-v /home/elasticsearch/data:/usr/shellare/elasticsearch/data \
-v /home/elasticsearch/plugins:/usr/shellare/elasticsearch/plugins \
-d elasticsearch:7.7.0
参数解释:
--name elasticsearch:将容器命名为 elasticsearch
-p 9200:9200:将容器的9200端口映射到宿主机9200端口
-p 9300:9300:将容器的9300端口映射到宿主机9300端口,目的是集群互相通信
-e "discovery.type=single-node":单例模式
-e ES_JAVA_OPTS="-Xms64m -Xmx128m":配置内存大小
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/shellare/elasticsearch/config/elasticsearch.yml:将配置文件挂载到宿主机
-v /mydata/elasticsearch/data:/usr/shellare/elasticsearch/data:将数据文件夹挂载到宿主机
-v /mydata/elasticsearch/plugins:/usr/shellare/elasticsearch/plugins:将插件目录挂载到宿主机(需重启)
-d elasticsearch:7.7.0:后台运行容器,并返回容器ID
执行结果:
接下来我们就是拿浏览器访问啦。
在浏览器上输入:服务器IP地址:9200
可能出现的问题
启动成功,但是访问失败的话,查看一下安全组中映射端口是否已经打开。如果是本地虚拟机,可能与防火墙相关(我用的云服务器,没有测试,如果有问题,还是需要大家自行去百度||谷歌一下)。
-e ES_JAVA_OPTS=“-Xms64m -Xmx128m”:配置内存大小出现了问题。(可试着调一下,docker玩不坏的,莫慌)
最后就还有可能是配置文件中出现了问题。
echo 'http.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*" '>>/home/elasticsearch/config/elasticsearch.yml
http.host: 0.0.0.0
在冒号后面有一个空格的,这里的配置文件是yml
格式,所以一定要严格按照yml
格式来书写。
为Elasticsearch安装ik分词器
分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词器是将每个字看成一个词,比如"我爱技术"会被分为"我",“爱”,“技”,“术”。
结果:
{ "tokens": [ { "token": "我", "start_offset": 0, "end_offset": 1, "type": "<IDEOGRAPHIC>", "position": 0 }, { "token": "爱", "start_offset": 1, "end_offset": 2, "type": "<IDEOGRAPHIC>", "position": 1 }, { "token": "技", "start_offset": 2, "end_offset": 3, "type": "<IDEOGRAPHIC>", "position": 2 }, { "token": "术", "start_offset": 3, "end_offset": 4, "type": "<IDEOGRAPHIC>", "position": 3 } ] }
这显然不符合要求,所以我们需要安装中文分词器IK来解决这个问题
IK提供了两个分词算法:ik_smart
和ik_max_word
其中ik_smart
为最少切分,ik_max_word
为最细粒度划分
进入已经启动成功的elasticsearch容器。
docker exec -it elasticsearch /bin/bashell
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.0/elasticsearch-analysis-ik-7.7.0.zip
ps: 安装的ik分词器的版本一定要与elasticsearch的版本保持一致
下载过程中会出现这个问题,直接输入 y回车就可以了。
下载完成之后,退出去,重新启动elasticsearch容器。
docker restart elasticsearch # <容器名o容器id> 重启容器命令
测试ik分词器是否安装成功
安装完成后,我们再来进行测试遍,看看有什么样的变化。
postman post请求分词测试:http://服务器IP地址:9200/_analyze
{
"tokenizer":"ik_smart",// 分词器不同,这个地方也会有变化 具体的可能还需要去学习。
"text":"我爱技术"
}
{ "tokens": [ { "token": "我", "start_offset": 0, "end_offset": 1, "type": "CN_CHAR", "position": 0 }, { "token": "爱", "start_offset": 1, "end_offset": 2, "type": "CN_CHAR", "position": 1 }, { "token": "技术", "start_offset": 2, "end_offset": 4, "type": "CN_WORD", "position": 2 } ] }
Elasticsearch
可视化工具有多种,可以根据自己的喜好进行选择,我这里使用的是mobz/elasticsearch-head:5
而已。
docker pull mobz/elasticsearch-head:5
启动镜像
docker run -d --name elasticsearch-head -p 9100:9100 mobz/elasticsearch-head:5
测试
我们在浏览器访问:http://服务器IP:9100/
可能出现的问题
elasticsearch配置文件elasticsearch.yml中 缺少了下列跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
下载并运行
#拉取并运行容器
docker run -d --name mysql-5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
参数说明:
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 默认账号root 用户的密码。
进入容器查看mysql服务
进入容器,并通过账号root查看MySQL服务能否正常连接
docker exec -it 9b3aad6819ff /bin/bashell
mysql -h localhost -u root -p
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。