之前有试验过用WIN10版的docker来学习和调试,但是实在是很卡,无法使用,因为windows平台下的基本可以看成是三层,所以最终还是选择Linux环境下的docker,因为ubuntu不是很熟悉,故采用Centos7发行版
Centos7版本:CentOS Linux release 7.6.1810 (Core)
内核版本:Linux localhost.localdomain 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
需要注意的是docker对于linux的内核是由版本要求的
docker版本:
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
docker库:https://hub.docker.com/
如果没有科学上网的话可能会比较卡,笔者这里网络无问题,就不做更换了,国内由很多网站,比如网易,可以自行查找
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
# 国内源
#$ sudo yum-config-manager \
# --add-repo \
# https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 官方源
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum makecache fast
$ sudo yum install docker-ce
$ sudo systemctl enable docker
$ sudo systemctl start docker
默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
建立 docker 组:
$ sudo groupadd docker
将当前用户加入 docker 组:
$ sudo usermod -aG docker $USER
重新登录后
执行
$ docker run hello-world
显示如下内容,或者类似,表示安装成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
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:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
打开docker库的地址,可以看到有很多库,在搜索框中输入tomcat,查找可以找到tomcat的页面https://hub.docker.com/_/tomcat
右侧可以找到镜像的拉取方式
docker pull tomcat
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
shisedo_server latest 7e31b3cef9a0 25 hours ago 547MB
tomcat latest ef802ca71927 7 days ago 475MB
redis latest ce25c7293564 2 weeks ago 95MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
# 这里后面跟随的是镜像ID,可以不用写全,但是必须唯一
[root@localhost ~]# docker rmi ce
Untagged: redis:latest
Untagged: redis@sha256:bf65ecee69c43e52d0e065d094fbdfe4df6e408d47a96e56c7a29caaf31d3c35
Deleted: sha256:ce25c7293564bac1a996496c75f49c58b840075ac6e8cb08d347fce5be90e212
Deleted: sha256:e9b4608f41cb6636feaf05e738ffd73507c475701850e7e82a1e3d5126e30929
Deleted: sha256:ff552fff5028a7b61934ae35f87eea48297eed091389763eb8fc16270e2a4c50
Deleted: sha256:f8a74967de91e643100b27cfc7f06fdd45bcfeb8f8ff1b12c116c3be1bbfde2e
Deleted: sha256:1568b09301049abf7ed4b38406ce96465f2145af91428d9efa8c8c0dc53297fa
Deleted: sha256:42bd21f043c373312ccf3f31fcfeabf596497421e9ff0103b6fb7dc764de631e
Deleted: sha256:ef68f6734aa485edf13a8509fe60e4272428deaf63f446a441b79d47fc5d17d3
[root@localhost ~]# docker run tomcat
27-Dec-2018 18:48:19.050 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.37
27-Dec-2018 18:48:19.053 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Dec 12 2018 12:07:02 UTC
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.37.0
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-957.1.3.el7.x86_64
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-8-openjdk-amd64/jre
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_181-8u181-b13-2~deb9u1-b13
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
27-Dec-2018 18:48:19.058 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
27-Dec-2018 18:48:19.059 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
27-Dec-2018 18:48:19.060 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
27-Dec-2018 18:48:19.060 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
27-Dec-2018 18:48:19.060 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
27-Dec-2018 18:48:19.060 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.19] using APR version [1.5.2].
27-Dec-2018 18:48:19.060 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
27-Dec-2018 18:48:19.060 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
27-Dec-2018 18:48:19.072 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.0j 20 Nov 2018]
27-Dec-2018 18:48:19.370 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
27-Dec-2018 18:48:19.399 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
27-Dec-2018 18:48:19.442 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
27-Dec-2018 18:48:19.447 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
27-Dec-2018 18:48:19.448 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 2357 ms
27-Dec-2018 18:48:19.529 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
27-Dec-2018 18:48:19.529 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.37
27-Dec-2018 18:48:19.556 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
27-Dec-2018 18:48:20.591 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [1,035] ms
27-Dec-2018 18:48:20.591 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
27-Dec-2018 18:48:20.640 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [49] ms
27-Dec-2018 18:48:20.640 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
27-Dec-2018 18:48:21.424 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [784] ms
27-Dec-2018 18:48:21.424 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
27-Dec-2018 18:48:21.494 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [70] ms
27-Dec-2018 18:48:21.494 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
27-Dec-2018 18:48:21.541 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [47] ms
27-Dec-2018 18:48:21.557 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
27-Dec-2018 18:48:21.585 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
27-Dec-2018 18:48:21.596 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 2148 ms
这属于最简单的前台执行,按Ctrl+C可以取消
docker run -d tomcat
f9df88fc4635083b95e5d1bfcc4cd55a993e84745cdf7270e580f62ec92be13b
后台运行会返回容器的ID,64位长
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9df88fc4635 tomcat "catalina.sh run" About a minute ago Up About a minute 8080/tcp eloquent_ellis
如果此时不关闭之前的容器,再次打开一次tomcat,会新增一个容器,且容器ID不同!所以可以将镜像与容器的关系看作是Java中的类和对象。
[root@localhost ~]# docker ps -a
9e为容器ID
docker stop 9e
终止状态的容器可以用 docker container ls -a
注意,必须先关闭容器再删除,否则只能强制删除,可能会发生部分问题
f9为容器ID
docker rm f9
这里的区别在于如果你继续使用docker run XXX,会新增一个容器!
1d为容器ID
docker start 1d
这里再次执行就无法看到日志了,如果想要看到日志,那么需要使用
docker logs 1d
有attach和exec两种方式,推荐使用exec,因为attach中使用exit退出会导出容器停止!
docker attach 243c
docker exec -it 69d1 bash
# run的形式下使用-d
docker run -d tomcat
# start形式下默认后台
docker start 1d
# 前台启动已经存在的容器
docker start -a 1d
# run形式下 使用-p来绑定端口,可以绑定多个;前面是主机端口号,后面是
docker run -d -p 80:8080 hello-world
docker run -d -p 80:8080 -p 8443:443 hello-world
# start形式下分为三种情况
1.run时已经指定无需修改,直接照常start就可以了
docker start 1d
2.run时已经指定需要修改
如果没有做一些修改,那么直接启动一个新的容器或许是一个好方法【官方貌似也是推荐此方法,可以看参考中的github的issue回复】,具体的修改方法,可以参考提交新镜像来解决
如果一定要修改呢
docker stop b8
vi /var/lib/docker/containers/{containerid}/hostconfig.json
vi /var/lib/docker/containers/{containerid}/config.v2.json
在其中修改对应的端口,保存
之后仍然需要
systemctl restart docker
systemctl start 1d
3.run时没有指定需要添加
这个可以采用添加配置文件的方式也可以用iptables转发
iptables -t nat -A DOCKER -p tcp --dport 37221 -j DNAT --to-destination 192.168.0.2:3306
后面的192.168.0.2:3306为容器的ip与需要映射的端口,37221为主机端口
也可以通过新镜像实现,所以其实还是使用新镜像来的好,要用容器的思想来思考问题
注意,如果在新建或者启动时出现提示“WARNING: IPv4 forwarding is disabled. Networking will not work. ”
需要打开
vi /usr/lib/sysctl.d/00-system.conf
添加
net.ipv4.ip_forward=1
保存
重启网络即可
systemctl restart network
这部分分为两个部分,tomcat部署于jar直接部署部分
docker pull tomcat
docker images
# 制作描述文件,新建Dockfile文件,名字必须固定!
vi Dockfile
内容如下:
from tomcat
// 表示持有人,和联系方式
MAINTAINER XXX [email protected]
// 拷贝文件,后面是位置,前面为文件,需要在当前目录下
COPY shiseido_server.war /usr/local/tomcat/webapps/
保存
docker build -t test .
注意后面的"."表示当前目录,-t表示指定名称
Sending build context to Docker daemon 71.15MB
Step 1/3 : from tomcat
---> ef802ca71927
Step 2/3 : MAINTAINER test [email protected]
---> Running in 86a30ad2bfde
Removing intermediate container 86a30ad2bfde
---> 814718afb521
Step 3/3 : COPY test .war /usr/local/tomcat/webapps/
---> 2867923b442d
Successfully built 2867923b442d
Successfully tagged test:latest
日志中会显示过程
直接启动,访问http://ip:port/test
成功!
docker pull java
vi Dockerfile
from java
MAINTAINER xxx [email protected]
COPY shiseido.war /usr/local/
CMD java -jar /usr/local/shiseido.war
:wq
# 确保war存在并可以执行
docker build -t shiseido .
docker run -d -p 8080:8080 shiseido
curl http://localhost:8080/
# 成功返回网页
还有很多命令可以在Dockerfile中使用,有兴趣的可以了解一下
Dockerfile 最佳实践
这里列出一些简单的解释:
WORKDIR 指定工作目录,可以指定多个,后续的可以是相对目录,第一个必须是绝对目录
COPY --chown=55:mygroup files* /mydir/ COPY命令可以指定用户和用数组,可以复制多个,支持匹配符
ADD 更高级的COPY 源路径可以是连接,ADD会自动下载;如果 <源路径> 为一个 tar 压缩文件的话,压缩格式为 gzip, bzip2 以及 xz 的情况下,ADD 指令将会自动解压缩这个压缩文件到 <目标路径> 去。所以如果是压缩包,谨慎使用。另外需要注意的是,ADD 指令会令镜像构建缓存失效,从而可能会令镜像构建变得比较缓慢。
因此在 COPY 和 ADD 指令中选择的时候,可以遵循这样的原则,所有的文件复制均使用 COPY 指令,仅在需要自动解压缩的场合使用 ADD。
CMD 容器启动命令;与RUN命令不同,RUN在创建镜像时就会执行,CMD实在运行容器时执行,表示“指定进程执行的程序”
ENV命令 设置环境变量 ENV = =...
导出分为两部分,导出镜像与导出容器
区别
export用来导出容器,save用来导出镜像
导出镜像,更加全面,因为会包含所有的历史文件,同时可以进行回滚操作;而容器则不能。
导出容器
docker export container_id > /home/export.tar
导入容器,注意,这里导入之后为一个镜像,而非容器,冒号后会版本号,如果特殊指定,那么在启动的时候需要加上
cat /home/export.tar | sudo docker import - test/containerExport:latest
导出镜像
docker save image_id > /home/save.tar
导入镜像
docker load < /home/save.tar
这里还是推荐把容器提交成新镜像后导出再导入的方式是比较好的,同时也比较方面,也能够保留历史文件