该文的环境为win10子系统中使用docker。
使用前提
尚未安装docker的可以参考:win10子系统安装docker教程地址
安装完成之后应该能做到如下事情即为安装成功
docker基本概念
Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。
Docker通常用于如下场景:
- web应用的自动化打包和发布;
- 自动化测试和持续集成、发布;
- 在服务型环境中部署和调整数据库或其他的后台应用;
镜像images:即模版,可理解为java中的class
容器:即运行的一个docker实例,一个镜像可以启动多个容器,可理解为java中的对象、实例
docker基本命令(可按顺序测试)
//在docker Hub上搜索包含ubuntu的镜像
docker search ubuntu
//下载指定镜像
docker pull ubuntu
//查看本地的镜像
docker images
//启动一个容器
//-i 表示允许我们对容器内的 ( STDIN ) 进行交互
//-t 表示在新容器内指定一个伪终端或终端
// /bin/bash 启动容器后容器内执行的命令
docker run -it ubuntu /bin/bash
//退出容器 或者ctrl+D,该例子中退出后容器即终止了
exit
//启动一个容器,并后台执行循环输出命令,返回的是一个长串,代表容器ID(container ID).它对于每一个容器来说都是唯一的,所以我们可以使用它。
//-d 以后台进程模式运行
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 10; done"
//查看正在运行的容器,分别对应【容器短ID】【镜像名】【执行的命令】【启动时间】【状态修改时间】【端口映射】【容器名】
docker ps
//查看上述容器的输出标准日志,wonderful_hellman为容器名称
docker logs wonderful_hellman
//停止容器后查看 docker stop wonderful_hellman
docker ps
自制镜像(两种方式,一种是通过容器做修改再建立镜像,第二种是通过Dockerfile配置文件建立镜像,无论哪种都是在基础镜像上做增删改操作后建立镜像)
通过容器建立镜像
//查看本地镜像
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat/webapp latest d7bd49956a48 42 hours ago 514MB
tomcat latest 168588387c68 11 days ago 463MB
redis latest 0f55cf3661e9 13 days ago 95MB
ubuntu 18.04 47b19964fb50 13 days ago 88.1MB
ubuntu latest 47b19964fb50 13 days ago 88.1MB
docker4w/nsenter-dockerd latest 2f1c802f322f 4 months ago 187kB
training/webapp latest 6fae60ef3446 3 years ago 349MB
learn/tutorial latest a7876479f1aa 5 years ago 128MB
//建立一个ubuntu镜像的容器
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker run -it ubuntu /bin/bash
//已进入容器的控制台
//更新源
root@904ffcfe591a:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [3440 B]
Get:4 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [156 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [342 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [683 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [10.8 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6963 B]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [941 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3650 B]
Fetched 15.5 MB in 47s (327 kB/s)
Reading package lists... Done
//安装ping工具
root@904ffcfe591a:/# apt-get install -y inetutils-ping
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
netbase
The following NEW packages will be installed:
inetutils-ping netbase
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 12.7 kB/72.8 kB of archives.
After this operation, 389 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 netbase all 5.4 [12.7 kB]
Fetched 12.7 kB in 2s (6894 B/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package netbase.
(Reading database ... 4039 files and directories currently installed.)
Preparing to unpack .../archives/netbase_5.4_all.deb ...
Unpacking netbase (5.4) ...
Selecting previously unselected package inetutils-ping.
Preparing to unpack .../inetutils-ping_2%3a1.9.4-3_amd64.deb ...
Unpacking inetutils-ping (2:1.9.4-3) ...
Setting up netbase (5.4) ...
Setting up inetutils-ping (2:1.9.4-3) ...
//测试ping工具
root@904ffcfe591a:/# ping www.baidu.com
PING www.wshifen.com (104.193.88.123): 56 data bytes
64 bytes from 104.193.88.123: icmp_seq=0 ttl=37 time=163.289 ms
^C--- www.wshifen.com ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max/stddev = 163.084/163.579/164.088/0.354 ms
//退出容器的控制台
root@904ffcfe591a:/# exit
//基于短ID为904ffcfe591a的这个容器建立镜像,-m备注,-a指定作者
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker commit -m="add ping" -a="wish" 904ffcfe591a wish/ubuntu:v2
sha256:0b66b04e76c94bf00f1365840edb0289cd9016f2a63d9459d0c98c6d154a81ec
//查看本地镜像,可以发现多出了wish/ubuntu,即建立镜像成功,该镜像在原来ubuntu镜像的基础上多了ping工具
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wish/ubuntu v2 0b66b04e76c9 4 seconds ago 114MB
tomcat/webapp latest d7bd49956a48 42 hours ago 514MB
tomcat latest 168588387c68 11 days ago 463MB
redis latest 0f55cf3661e9 13 days ago 95MB
ubuntu 18.04 47b19964fb50 13 days ago 88.1MB
ubuntu latest 47b19964fb50 13 days ago 88.1MB
docker4w/nsenter-dockerd latest 2f1c802f322f 4 months ago 187kB
training/webapp latest 6fae60ef3446 3 years ago 349MB
learn/tutorial latest a7876479f1aa 5 years ago 128MB
通过Dokcerfile配置文件建立镜像
Dokcerfile文件内容如下:
#在哪个镜像基础上建立
FROM 47b19964fb50
#作者
MAINTAINER wish
#更新时间
ENV REFRESHED_AT 2019-02-18
#切换镜像目录
WORKDIR /usr
#建立目录
RUN mkdir jdk1.8.0_172
RUN mkdir apache-tomcat-8.5.32
#从宿主机复制目录至镜像
ADD jdk1.8.0_172/ /usr/jdk1.8.0_172/
ADD apache-tomcat-8.5.32/ /usr/apache-tomcat-8.5.32/
#设置环境变量
ENV JAVA_HOME=/usr/jdk1.8.0_172
ENV JRE_HOME=$JAVA_HOME/jre
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH=/sbin:$JAVA_HOME/bin:$PATH
#对宿主机暴露的端口
EXPOSE 8080
#容器启动时执行的命令
ENTRYPOINT ["/usr/apache-tomcat-8.5.32/bin/catalina.sh","run"]
操作步骤如下:
//预先准备好Dockerfile文件,apache-tomcat-8.5.32,jdk1.8.0_172
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# ls
Dockerfile apache-tomcat-8.5.32 apache-tomcat-8.5.32.zip auc.war jdk-8u172-linux-x64.tar.gz jdk1.8.0_172 test.html
//构建镜像 -t:指定仓库名及版本 .指当前目录
//每执行一步都会生成一个cache版本,若其中一步失败,下次构建时会调用cache版本,前提是未改动
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker build -t wish/tomcat .
Sending build context to Docker daemon 426.2MB
Step 1/14 : FROM 47b19964fb50
---> 47b19964fb50
Step 2/14 : MAINTAINER wish
---> Running in e8066d4d0cbf
Removing intermediate container e8066d4d0cbf
---> 93800715d1f9
Step 3/14 : ENV REFRESHED_AT 2019-02-18
---> Running in 158f05d70aa4
Removing intermediate container 158f05d70aa4
---> 6154de3221ce
Step 4/14 : WORKDIR /usr
---> Running in 1c147a751dd3
Removing intermediate container 1c147a751dd3
---> 0306302d873e
Step 5/14 : RUN mkdir jdk1.8.0_172
---> Running in da15de088955
Removing intermediate container da15de088955
---> 24beb15cea3e
Step 6/14 : RUN mkdir apache-tomcat-8.5.32
---> Running in 8131f2bdcb0a
Removing intermediate container 8131f2bdcb0a
---> bec27c7e5299
Step 7/14 : ADD jdk1.8.0_172/ /usr/jdk1.8.0_172/
---> ecd59e6f141a
Step 8/14 : ADD apache-tomcat-8.5.32/ /usr/apache-tomcat-8.5.32/
---> 51937f3a880a
Step 9/14 : ENV JAVA_HOME=/usr/jdk1.8.0_172
---> Running in 9e3bf28ae29b
Removing intermediate container 9e3bf28ae29b
---> bc1f500ec93d
Step 10/14 : ENV JRE_HOME=$JAVA_HOME/jre
---> Running in b8a0b1eb06c7
Removing intermediate container b8a0b1eb06c7
---> 6ecfd4597d7b
Step 11/14 : ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
---> Running in 9471c622a53e
Removing intermediate container 9471c622a53e
---> 520c84467a75
Step 12/14 : ENV PATH=/sbin:$JAVA_HOME/bin:$PATH
---> Running in aef84c7f2110
Removing intermediate container aef84c7f2110
---> cc1acc360ac5
Step 13/14 : EXPOSE 8080
---> Running in e0f73b72c24c
Removing intermediate container e0f73b72c24c
---> 7a05c2a9974b
Step 14/14 : ENTRYPOINT ["/usr/apache-tomcat-8.5.32/bin/catalina.sh","run"]
---> Running in 3412281265ad
Removing intermediate container 3412281265ad
---> 9ef9bd474c3e
Successfully built 9ef9bd474c3e
Successfully tagged wish/tomcat:latest
//查看镜像
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wish/tomcat latest 9ef9bd474c3e 5 minutes ago 489MB
wish/ubuntu v2 0b66b04e76c9 44 minutes ago 114MB
tomcat latest 168588387c68 11 days ago 463MB
redis latest 0f55cf3661e9 13 days ago 95MB
ubuntu latest 47b19964fb50 2 weeks ago 88.1MB
docker4w/nsenter-dockerd latest 2f1c802f322f 4 months ago 187kB
training/webapp latest 6fae60ef3446 3 years ago 349MB
learn/tutorial latest a7876479f1aa 5 years ago 128MB
//启动一个容器 -d 后台运行 -p 指定端口 将容器的8080映射到宿主机的8080端口
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker run -d -p 8080:8080 wish/tomcat
4e0f136a5ed07be3d0bff9c2b10d4d63c6dda58dd8e0c42a8abaa3aa394c1cfb
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0f136a5ed0 wish/tomcat "/usr/apache-tomcat-…" 22 minutes ago Up 22 minutes 0.0.0.0:8080->8080/tcp dazzling_sinoussi
在宿主机可以查看localhost:8080
将项目部署到容器中
尝试将一个test.html页面放入tomcat的webapps目录下
//准备测试页面
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# ls
test.html
//查看当前正在运行的容器
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0f136a5ed0 wish/tomcat "/usr/apache-tomcat-…" 39 minutes ago Up 39 minutes 0.0.0.0:8080->8080/tcp dazzling_sinoussi
//将测试页面复制进容器
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker cp /home/wish/testDockerfile/test 4e0f136a5ed0:/usr/apache-tomcat-8.5.32/webapps
//进入容器查看是否复制成功, exec 在容器中执行命令,-i 保持STDIN 打开 -t 分配一个伪终端
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker exec -it dazzling_sinoussi /bin/bash
//进入指定目录查看是否存在
root@4e0f136a5ed0:/usr# cd apache-tomcat-8.5.32/webapps/
root@4e0f136a5ed0:/usr/apache-tomcat-8.5.32/webapps# ls
ROOT docs examples host-manager manager test
//删除镜像 -f 强制删除, 可不加,不加则若该镜像有被使用的情况下无法删除
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker rmi -f wish/ubuntu:v2
Untagged: wish/ubuntu:v2
Deleted: sha256:0b66b04e76c94bf00f1365840edb0289cd9016f2a63d9459d0c98c6d154a81ec
Deleted: sha256:0f8db578e34437219f6a3a20558ff690928c866f1241ee99f8ba5cf65911de79
容器间通讯
测试redis
//搜索并下载镜像
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker search redis
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker pull redis
//查看本地镜像
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wish/tomcat latest 9ef9bd474c3e 8 hours ago 489MB
tomcat latest 168588387c68 11 days ago 463MB
redis latest 0f55cf3661e9 2 weeks ago 95MB
ubuntu latest 47b19964fb50 2 weeks ago 88.1MB
docker4w/nsenter-dockerd latest 2f1c802f322f 4 months ago 187kB
training/webapp latest 6fae60ef3446 3 years ago 349MB
learn/tutorial latest a7876479f1aa 5 years ago 128MB
//启动redis容器 -d后台运行 --name为容器指定名称,不能重复
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker run -d --name redisService redis
c46bda9a21831833092cf6314273d9f46a3c5e01f466743758870f0eb2f86d19
//查看运行中的容器
dockerroot@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c46bda9a2183 redis "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 6379/tcp redisService
//启动一个测试容器 --link redisService:db 连接其它容器 名称:在测试容器中的别名
root@DESKTOP-63Q0NFK:/home/wish/testDockerfile/test# docker run --link redisService:db -it ubuntu /bin/bash
//更新源
root@a46d506a7f68:/# apt-get install redis-server
//安装redis的redis-cli的二进制包来测试连接
root@a46d506a7f68:/# apt-get install -y redis-server
//查看测试容器的环境变量 DB开头的即为连接的容器的环境变量
root@a46d506a7f68:/# env
DB_PORT=tcp://172.17.0.2:6379
DB_PORT_6379_TCP_ADDR=172.17.0.2
DB_ENV_REDIS_DOWNLOAD_SHA=e290b4ddf817b26254a74d5d564095b11f9cd20d8f165459efa53eb63cd93e02
HOSTNAME=a46d506a7f68
DB_PORT_6379_TCP=tcp://172.17.0.2:6379
DB_ENV_REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-5.0.3.tar.gz
DB_PORT_6379_TCP_PROTO=tcp
DB_PORT_6379_TCP_PORT=6379
PWD=/
DB_ENV_REDIS_VERSION=5.0.3
HOME=/root
TERM=xterm
SHLVL=1
DB_NAME=/compassionate_noether/db
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DB_ENV_GOSU_VERSION=1.10
_=/usr/bin/env
//测试容器间的通讯
root@a46d506a7f68:/# redis-cli -h $DB_PORT_6379_TCP_ADDR
172.17.0.2:6379> set testKey testV
OK
172.17.0.2:6379> get testKey
"testV"
docker挂载
经测试,win10子系统貌似不好使,待摸索
进入docker可能为/bin/bash、/bin/sh (bash/sh都是shell,可能还存在其它)
docker
docker复制
docker save source -o target.tar
docker还原
sudo docker load < target.tar