标签:现在 minutes env host -- 应该 centos7 display 记录
前面的操作我们一直下载下载官方已经构建好的镜像,直接下载后就可以run,如果我们想要在镜像中添加自己的应用,比如在tomcat中添加自己的app,构建一个自定义的镜像,那么我们应该怎么做,这个时候就用到了Dockerfile。Dockerfile是由一系列命令和参数构成的脚本,Docker可以根据这个脚本基于某个基础镜像创建一个新的定制化的镜像,大大提高了我们部署的效率,使用Dockfile最终的结果是帮助我们定制化自己的镜像。
下面就基于tomcat镜像简单部署一个属于我们自己的应用,选择一个合适的目录,这里我选择的目录是/usr/local/src,在/usr/local/src目录下创建一个文件夹docker-web,在里面创建一个index.html文件,写上下面内容
mkdir docker-web
vim index.html
<h1>I‘m Docker Test Page</h1>
然后创建一个没有扩展名的文件Dockerfile,开始编写相关命令
FROM tomcat
MAINTAINER www.conly.top
WORKDIR /usr/local/tomcat/webapps
ADD docker-web ./docker-web
一个简单的Dockerfile就编写完成,其中命令具体是什么意思,下面会做详细说明,接下来开始构建镜像
[root@node2 docker-test]# docker build -t conly/mywebapp:1.0 ./
Sending build context to Docker daemon 3.584kB
Step 1/4 : FROM tomcat
---> 6408fdc94212
Step 2/4 : MAINTAINER www.conly.top
---> Running in 53e29f832089
Removing intermediate container 53e29f832089
---> b285ad91c35b
Step 3/4 : WORKDIR /usr/local/tomcat/webapps/
---> Running in ac5f64ef5e3c
Removing intermediate container ac5f64ef5e3c
---> f3eeee360833
Step 4/4 : ADD dockerweb ./dockerweb
---> 51a4c7e6ed10
Successfully built 51a4c7e6ed10
Successfully tagged conly/mywebapp:1.0
在构建的过程中,Docker根据Dockerfile中的内容进行了四个步骤,分别对应四个命令,这里要提到一个镜像分层的概念,每执行一步都会创建一个临时容器,并且可以看到临时容器的ID,临时容器是不可使用的,执行完成后,会展示构建镜像的镜像ID,镜像名称,tags等信息。
这时可以看到我们的Docker image已经多了一个自己创建的镜像conly/tomcat 的1.0版本
[root@node2 docker-test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE conly/mywebapp 1.0 51a4c7e6ed10 3 minutes ago 507MB tomcat latest 6408fdc94212 2 weeks ago 507MB
如果此时基于原来的Dockerfile构建2.0版本
[root@node2 docker-test]# docker build -t conly/mywebapp:2.0 ./ Sending build context to Docker daemon 3.584kB Step 1/4 : FROM tomcat ---> 6408fdc94212 Step 2/4 : MAINTAINER www.conly.top ---> Using cache ---> b285ad91c35b Step 3/4 : WORKDIR /usr/local/tomcat/webapps/ ---> Using cache ---> f3eeee360833 Step 4/4 : ADD dockerweb ./dockerweb ---> Using cache ---> 51a4c7e6ed10 Successfully built 51a4c7e6ed10 Successfully tagged conly/mywebapp:2.0
Dcoker在根据Dockerfile构建镜像时没有变动的 部分会使用缓存Using cache不会重新构建,只会对我们变更的部分进行构建,由于镜像分层的和使用缓存的机制使得Docker在构建镜像的效率大大提升。
构建镜像后执行run来运行我们的镜像,这个时候就可以在外部浏览器访问自定义的app应用了。
docker run -p 80:8080 -d conly/mywebapp:3.0
访问页面
下面介绍Docker常用的指令:
LABEL multi.label1="value1" multi.label2="value2" other="value3"
或
LABEL multi.label1="value1" multi.label2="value2" other="value3"
HTTP_PROXY
http_proxy
HTTPS_PROXY
https_proxy
FTP_PROXY
ftp_proxy
NO_PROXY
no_proxy
[root@conly webapps]# docker build --build-arg xxccx=conly -t conly . Sending build context to Docker daemon 4.608kB Step 1/5 : from tomcat:latest ---> 6408fdc94212 Step 2/5 : MAINTAINER www.conly.top ---> Using cache ---> 79180381b68d Step 3/5 : WORKDIR /opt/tomcat/webapps ---> Using cache ---> 9735f116f248 Step 4/5 : EXPOSE 8080 8081 8082 ---> Using cache ---> 8b140733df33 Step 5/5 : ADD https://images.cnblogs.com/cnblogs_com/conly/1600508/o_191211120217min.png ./docker-web Downloading [==================================================>] 230.1kB/230.1kB ---> Using cache ---> 61e9f6346096 [Warning] One or more build-args [xxccx] were not consumed Successfully built 61e9f6346096 Successfully tagged conly:latest
关于ARG的更多使用可以参考https://www.centos.bz/2016/12/dockerfile-arg-instruction/
RUN groupadd -r postgres && useradd -r -g postgres postgres
ENTRYPINT ["ps"] CMD ["-ef"]
镜像构建完成,如果使用docker run image -aux,则原有CMD命令在执行时会被替换为ps -aux,这种方式实现了动态传参,不用修改Dockerfile重新构建镜像就可以在创建容器上执行自定义命令。
# use the value to set the ENV var default
ARG A_VARIABLE
ENV an_env_var=$A_VARIABLE
[root@conly webapps]# docker build -t tomcat:0.1 . Sending build context to Docker daemon 4.608kB Step 1/7 : ARG user=test_user Step 2/7 : ARG password=test_password Step 3/7 : from tomcat:latest ---> 6408fdc94212 Step 4/7 : MAINTAINER www.conly.top ---> Using cache ---> 79180381b68d Step 5/7 : WORKDIR /opt/tomcat/webapps ---> Using cache ---> 9735f116f248 Step 6/7 : EXPOSE 8080 8081 8082 ---> Using cache ---> 8b140733df33 Step 7/7 : ADD https://images.cnblogs.com/cnblogs_com/conly/1600508/o_191211120217min.png ./docker-web Downloading [==================================================>] 230.1kB/230.1kB ---> Using cache ---> 61e9f6346096 Successfully built 61e9f6346096 Successfully tagged tomcat:0.1 [root@conly webapps]# docker history tomcat:0.1 IMAGE CREATED CREATED BY SIZE COMMENT 61e9f6346096 5 hours ago /bin/sh -c #(nop) ADD 394a0073236e50f2867f4a… 230kB 8b140733df33 5 hours ago /bin/sh -c #(nop) EXPOSE 8080 8081 8082 0B 9735f116f248 2 days ago /bin/sh -c #(nop) WORKDIR /opt/tomcat/webapps 0B 79180381b68d 2 days ago /bin/sh -c #(nop) MAINTAINER www.conly.top 0B 6408fdc94212 2 weeks ago /bin/sh -c #(nop) CMD ["catalina.sh" "run"] 0B <missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 8080 0B <missing> 2 weeks ago /bin/sh -c set -e && nativeLines="$(catalin… 0B <missing> 2 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 18.6MB <missing> 2 weeks ago /bin/sh -c #(nop) ENV TOMCAT_SHA512=263480a… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV TOMCAT_VERSION=8.5.49 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV TOMCAT_MAJOR=8 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV GPG_KEYS=05AB33110949… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV LD_LIBRARY_PATH=/usr/… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV TOMCAT_NATIVE_LIBDIR=… 0B <missing> 2 weeks ago /bin/sh -c #(nop) WORKDIR /usr/local/tomcat 0B <missing> 2 weeks ago /bin/sh -c mkdir -p "$CATALINA_HOME" 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/tomca… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV CATALINA_HOME=/usr/lo… 0B <missing> 2 weeks ago /bin/sh -c set -eux; dpkgArch="$(dpkg --pr… 205MB <missing> 2 weeks ago /bin/sh -c #(nop) ENV JAVA_URL_VERSION=8u23… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV JAVA_BASE_URL=https:/… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV JAVA_VERSION=8u232 0B <missing> 2 weeks ago /bin/sh -c { echo ‘#/bin/sh‘; echo ‘echo "$J… 27B <missing> 2 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/openj… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV JAVA_HOME=/usr/local/… 0B <missing> 2 weeks ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0B <missing> 2 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 9.68MB <missing> 2 weeks ago /bin/sh -c apt-get update && apt-get install… 142MB <missing> 2 weeks ago /bin/sh -c set -ex; if ! command -v gpg > /… 7.81MB <missing> 2 weeks ago /bin/sh -c apt-get update && apt-get install… 23.2MB <missing> 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 2 weeks ago /bin/sh -c #(nop) ADD file:152359c10cf61d800… 101MB
完整信息命令
docker history --format "table {{.ID}}\t{{.CreatedBy}}" --no-trunc image:<tag>
首次使用 COPY 和 ADD 命令时也没有考虑过太多二者的区别,随着对Docker的使用会发现 COPY 命令的设计是简单,概念清晰的。而 ADD 命令是在COPY命令上的扩展,提高了使用的复杂度,尤其在使用ADD添加源为URL的情况,要注意dest路径的结尾有没有/,但这些设计在我们熟悉Docker后也会方便操作。
在Docker中推荐使用exec方式执行命令,方便于我们的操作,这也是官方推荐的方式。
在了解完Dockerfile中的一些常见命令后,接下来就进行一个简单的应用吧,创建一个Dockerfile基于CentOS来构建自定义Redis镜像。这里切换到/usr/local/redis_dockertest目录来进行操作,Redis作为一个优秀的NoSQL数据库,Docker Hub自然有官方镜像,这里用来练习Dockerfile的常用指令。
基于CentOS7的镜像,添加Redis的安装包进去,添加配置文件来制作镜像,帮助我们熟悉Dockerfile的指令。
下载redis-5.0.5安装包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
下载redis.conf配置文件
wget http://download.redis.io/redis-stable/redis.conf
修改redis.conf
bind 127.0.0.1 --> #bind 127.0.0.1 daemonize no --> daemonize yes protected-mode yes --> protected-mode no
创建Dockerfile
vim Dockerfile
编写Dockerfile
ARG desc FROM centos LABEL maintainer=www.cnblogs.com/conly/ RUN ["yum","-y","install","gcc","gcc-c++","net-tools","make"] WORKDIR /usr/local ADD redis-5.0.5.tar.gz . WORKDIR /usr/local/redis-5.0.5/src/ RUN make && make install WORKDIR /usr/local/redis-5.0.5 ADD redis.conf . EXPOSE 6379 CMD ["redis-server","redis.conf"]
构建redis镜像
docker build --build-arg desc="this is a docker image build test" -t conly/redis:1.0 .
截取部分执行过程,看到Dockerfile的执行步骤
[root@conly redis_dockertest]# docker build --build-arg desc="this is a docker image build test" -t conly/redis:1.0 . ... ... ... Removing intermediate container 528d15f50a03 ---> 809289100143 Step 9/14 : WORKDIR /usr/local/redis-5.0.5 ---> Running in 566b63100414 Removing intermediate container 566b63100414 ---> 9cfce44318ee Step 10/14 : ADD redis.conf . ---> dc8127dafb54 Step 11/14 : EXPOSE 6379 ---> Running in eda8f77e9c83 Removing intermediate container eda8f77e9c83 ---> 08f348a33ff6 Step 12/14 : WORKDIR /usr/local/redis-5.0.5/utils ---> Running in c7cd15701c85 Removing intermediate container c7cd15701c85 ---> 1dcde04ee437 Step 13/14 : ENTRYPOINT ["./"] ---> Running in 152277e9979e Removing intermediate container 152277e9979e ---> 1c9a7b9c6bae Step 14/14 : CMD ["install_server.sh"] ---> Running in 065dc4a5ba65 Removing intermediate container 065dc4a5ba65 ---> 8b1990aa9224 Successfully built 8b1990aa9224 Successfully tagged conly/redis:1.0
查看docker的镜像,可以看到除了我们构建的conly/redis:1.0版本意外,此外Dockerfile中的FROM centos也pull下来了
[root@conly redis_dockertest]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE conly/redis 1.0 8b1990aa9224 3 minutes ago 587MB centos latest 0f3e07c0138f 2 months ago 220MB
使用docker run运行redis镜像
[root@conly redis_dockertest]# docker run -p 6379:6379 conly/redis:1.0 1:C 12 Dec 2019 13:27:00.599 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 12 Dec 2019 13:27:00.599 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 12 Dec 2019 13:27:00.599 # Configuration loaded _._ _.-``__ ‘‘-._ _.-`` `. `_. ‘‘-._ Redis 5.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ‘‘-._ ( ‘ , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379 | `-._ `._ / _.-‘ | PID: 1 `-._ `-._ `-./ _.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | http://redis.io `-._ `-._`-.__.-‘_.-‘ _.-‘ |`-._`-._ `-.__.-‘ _.-‘_.-‘| | `-._`-._ _.-‘_.-‘ | `-._ `-._`-.__.-‘_.-‘ _.-‘ `-._ `-.__.-‘ _.-‘ `-._ _.-‘ `-.__.-‘ 1:M 12 Dec 2019 13:27:00.601 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 12 Dec 2019 13:27:00.601 # Server initialized 1:M 12 Dec 2019 13:27:00.601 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect. 1:M 12 Dec 2019 13:27:00.601 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 1:M 12 Dec 2019 13:27:00.603 * Ready to accept connections
此时redis是阻塞状态运行,当然我们也可以重新编写Dockerfile来构建镜像让redis容器后台运行
ARG desc FROM centos LABEL maintainer=www.cnblogs.com/conly/ RUN ["yum","-y","install","gcc","gcc-c++","net-tools","make"] WORKDIR /usr/local ADD redis-5.0.5.tar.gz . WORKDIR /usr/local/redis-5.0.5/src/ RUN make && make install WORKDIR /usr/local/redis-5.0.5 ADD redis.conf . RUN cd /usr/local/redis-5.0.5/utils && echo | /bin/bash install_server.sh ENTRYPOINT /usr/local/bin/redis-server /etc/redis/6379.conf && tail -f /var/log/redis_6379.log
运行redis,可以看到我们自定义的redis已经运行了。
[root@conly redis_dockertest]# dockerun -p 6379:6379 -d conly/redis:1.0 . d3b1191ae2408d5025ba55e3b893300ca9438246a10feb2d7e02fa0f29801740 [root@conly redis_dockertest]# docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE d3b1191ae240 conly/redis:1.0 "/bin/sh -c ‘/usr/lo…" 8 seconds ago Up 7 seconds 0.0.0.0:6379->6379/tcp fervent_babbage 2.58kB (virtual 587MB)
Dockerfile中常用的指令总结完毕,并且进行了简单的练习,接下来开始学习Docker容器间的通信,Docker Compose的实战应用。
标签:现在 minutes env host -- 应该 centos7 display 记录
原文地址:https://www.cnblogs.com/conly/p/12009076.html