docker自定义镜像:应用程序打包
1, cmd和entrypointentrypointcmd相同点可以有多个cmd或entrypoint, 但只执行最后一个不同点1指定的命令不能覆盖可以在docker run 镜像 …时覆盖启动命令不同点2参数能在docker run时动态传入参数不能动态传入vim entrypoint.conf==&amp
·
文章目录
说明:由于docker build 命令默认寻找Dockerfile文件,以下为了实验,应此使用了不同名的文件,在构建镜像时需要指定配置文件: docker build -f -f 文件名
1, cmd和entrypoint
entrypoint | cmd | |
---|---|---|
相同点 | 可以有多个cmd或entrypoint, 但只执行最后一个 | |
不同点1 | 启动命令在docker run 镜像 …时不能覆盖 | 启动命令可以覆盖 |
不同点2 | 参数可以在docker run时动态传入 | 参数不能动态传入 |
a, entrypoint
[root@www dockerfile]# ls
a.sh entrypoint.dockerfile
[root@www dockerfile]# cat entrypoint.dockerfile
# ENTRYPOINT ["executable", "param1", "param2"]
# ENTRYPOINT command param1 param2(shell中执行)。
FROM centos:6
COPY a.sh /
RUN chmod +x /a.sh
ENTRYPOINT ["/a.sh"]
[root@www dockerfile]# cat a.sh
#!/bin/sh
echo $@
[root@www dockerfile]# docker build . -t entrypoint -f entrypoint.dockerfile
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM centos:6
---> d0957ffdf8a2
Step 2/4 : COPY a.sh /
---> Using cache
---> 94df98711317
Step 3/4 : RUN chmod +x /a.sh
---> Using cache
---> 8634a9ec16dc
Step 4/4 : ENTRYPOINT ["/a.sh"]
---> Using cache
---> 80e97687cc1d
Successfully built 80e97687cc1d
Successfully tagged entrypoint:latest
[root@www dockerfile]# docker run -it entrypoint 1 2 3
1 2 3
[root@www dockerfile]# docker run -it entrypoint ls /
ls /
b, cmd
[root@www dockerfile]# cat cmd.dockerfile
FROM centos:6
CMD cat /etc/hosts
[root@www dockerfile]# docker build . -t cmd -f cmd.dockerfile
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos:6
---> d0957ffdf8a2
Step 2/2 : CMD cat /etc/hosts
---> Running in c9360cadf640
Removing intermediate container c9360cadf640
---> 9bd857c744a4
Successfully built 9bd857c744a4
Successfully tagged cmd:latest
[root@www dockerfile]# docker run -it cmd
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
172.17.0.3 30943241d769
[root@www dockerfile]# docker run -it cmd ls /
bin etc lib lost+found mnt proc sbin srv tmp var
dev home lib64 media opt root selinux sys usr
c, cmd-entrypoint.conf
docker run的参数,覆盖了内置的cmd参数,最后传给了entrypoint执行
[root@www dockerfile]# cat cmd-entrypoint.dockerfile
FROM centos:6
COPY a.sh /
RUN chmod +x /a.sh
CMD echo 1.1.1.1 test.com >> /etc/hosts
ENTRYPOINT ["/a.sh"]
[root@www dockerfile]# cat a.sh
#!/bin/sh
echo "hello world..."
cat /etc/hosts
[root@www dockerfile]# docker build . -t cmd-entrypoint -f cmd-entrypoint.dockerfile
Sending build context to Docker daemon 7.168kB
Step 1/5 : FROM centos:6
---> d0957ffdf8a2
Step 2/5 : COPY a.sh /
---> 4a702c3e7a12
Step 3/5 : RUN chmod +x /a.sh
---> Running in b36869a9a9d0
Removing intermediate container b36869a9a9d0
---> 1df09b4ad929
Step 4/5 : CMD echo 1.1.1.1 test.com >> /etc/hosts
---> Running in 585f7d73931b
Removing intermediate container 585f7d73931b
---> 2663920a77a3
Step 5/5 : ENTRYPOINT ["/a.sh"]
---> Running in d9762bc68c91
Removing intermediate container d9762bc68c91
---> 333acc2cb292
Successfully built 333acc2cb292
Successfully tagged cmd-entrypoint:latest
[root@www dockerfile]# docker run -it cmd-entrypoint
hello world...
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
172.17.0.3 0fdd4bb4e72f
d, entrypoint-cmd
(mysql5.6 的dockerfile就用到这个配置)
注意:ENTRYPOINT执行完的代码,最后要添加exec “$@” ,否则后面的CMD就无法执行
[root@www dockerfile]# cat entrypoint-cmd.dockerfile
FROM centos:6
COPY a.sh /
RUN chmod +x /a.sh
ENTRYPOINT ["/a.sh"]
CMD cat /etc/hosts
[root@www dockerfile]# cat a.sh
#!/bin/sh
echo "hello world..."
echo 1.1.1.1 test.com >> /etc/hosts
exec "$@"
[root@www dockerfile]# docker build . -t entrypoint-cmd -f entrypoint-cmd.dockerfile
Sending build context to Docker daemon 7.168kB
Step 1/5 : FROM centos:6
---> d0957ffdf8a2
Step 2/5 : COPY a.sh /
---> ca6b7465b5a7
Step 3/5 : RUN chmod +x /a.sh
---> Running in 52ba469a5ee7
Removing intermediate container 52ba469a5ee7
---> 168285a1e37c
Step 4/5 : ENTRYPOINT ["/a.sh"]
---> Running in 7f61ca29e996
Removing intermediate container 7f61ca29e996
---> e1441211b25c
Step 5/5 : CMD cat /etc/hosts
---> Running in 80d63299537f
Removing intermediate container 80d63299537f
---> a8d057b00f16
Successfully built a8d057b00f16
Successfully tagged entrypoint-cmd:latest
[root@www dockerfile]# docker run -it entrypoint-cmd
hello world...
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 3d7b7b365271
1.1.1.1 test.com
e, 传递-e参数
[root@www dockerfile]# cat e_param.dockerfile
FROM centos:6
COPY a.sh /
RUN chmod +x /a.sh
CMD ["/a.sh", "$@"]
[root@www dockerfile]# cat a.sh
#!/bin/sh
echo $@
echo $name
echo $age
[root@www dockerfile]# docker build . -t e_param -f e_param.dockerfile
Sending build context to Docker daemon 6.144kB
Step 1/4 : FROM centos:6
---> d0957ffdf8a2
Step 2/4 : COPY a.sh /
---> Using cache
---> 3ccd29cebca3
Step 3/4 : RUN chmod +x /a.sh
---> Using cache
---> 9957f7fad3c6
Step 4/4 : CMD ["/a.sh", "$@"]
---> Using cache
---> e1c5420485f2
Successfully built e1c5420485f2
Successfully tagged e_param:latest
[root@www dockerfile]# docker run -it -e name=lisi -e age=28 e_param
$@
lisi
28
2, 应用程序jar包:打包为镜像
a, 打包应用程序为jar包
打包下面的文件为可执行的jar包: job.jar
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
if (args.length ==0) {
System.out.println("please input youar name ");
System.exit(1);
}else {
System.out.println("hello "+ args[0]);
}
Scanner scanner = new Scanner(System.in);
while (true){
//输入
System.out.println("input num1: ");
String num1 = scanner.next();
System.out.println("input num2: ");
String num2 = scanner.next();
System.out.println("sum (num1,num2) = "+ (Integer.parseInt(num1)+ Integer.parseInt(num2)));
}
}
}
b, 编写Dockerfile, 编译镜像,运行容器
准备的文件如下
wang@wang-pc:~/txt/docker-test/business-docker$ ls
Dockerfile jdk.tar.gz job.jar
wang@wang-pc:~/txt/docker-test/business-docker$ cat Dockerfile
FROM centos
ADD jdk.tar.gz /root
COPY job.jar /root
WORKDIR /root
CMD jdk1.8/bin/java -jar job.jar test
c, 定制jdk镜像( 修改时区,字符集)
[root@docker3 docker-self]# ll
total 175240
-rw-r--r--. 1 root root 279 Jul 23 17:09 jdk8.dockerfile
-rw-r--r--. 1 root root 179439263 Jul 21 12:26 jdk-8u211-linux-x64.rpm
[root@docker3 docker-self]# cat jdk8.dockerfile
FROM centos:7
WORKDIR /
ADD jdk-8u211-linux-x64.rpm .
RUN rpm -Uvh jdk*.rpm && rm -f jdk*.rpm && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US.UTF-8
CMD ["top","-b"]
- 编译镜像
wang@wang-pc:~/txt/docker-test/business-docker$ docker build .
Sending build context to Docker daemon 190.9MB
Step 1/5 : FROM centos
---> 1e1148e4cc2c
Step 2/5 : ADD jdk.tar.gz /root
---> Using cache
---> efd1776422e1
Step 3/5 : COPY job.jar /root
---> Using cache
---> ce65c63bb86c
Step 4/5 : WORKDIR /root
---> Using cache
---> 1d7da03e91de
Step 5/5 : CMD jdk1.8/bin/java -jar job.jar test
---> Using cache
---> c5ff2589c724
Successfully built c5ff2589c724
- 运行容器
wang@wang-pc:~/txt/docker-test/business-docker$ docker run -it c5ff2589c724
hello test
input num1:
1
input num2:
3
sum (num1,num2) = 4
input num1:
3, 应用程序war包:打包为镜像
a, 打包web应用程序为war包
根据上篇博客 https://blog.csdn.net/eyeofeagle/article/details/86597649, 编写springboot 的helloworld案例,重写Controller类,如下
package a;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class Controller1 {
@RequestMapping(value = "/a", method = {RequestMethod.GET})
@ResponseBody
public String successModel(HttpServletRequest request, HttpServletResponse reponse)
{
return "hello "+ request.getParameter("name" )+" !";
}
}
打包为web.war
b, 编写Dockerfile,编译镜像,运行容器
wang@wang-pc:~/txt/docker-test/web-docker$ ls
centos tomcat
# 以tomcat为基础镜像: 只要拷贝war包到webapps目录即可运行
wang@wang-pc:~/txt/docker-test/web-docker$ ls tomcat/
Dockerfile web.war
# 以centos为基础镜像: 需要安装jdk, tomcat
wang@wang-pc:~/txt/docker-test/web-docker$ ls centos/
apache-tomcat-7.0.92.tar.gz Dockerfile entrypoint.sh jdk1.8.tar.gz web.war
1, tomcat镜像版
- Dockerfile
wang@wang-pc:~/txt/docker-test/web-docker$ cat tomcat/Dockerfile
FROM tomcat
COPY web.war /usr/local/tomcat/webapps
- 编译镜像
wang@wang-pc:~/txt/docker-test/web-docker$ docker build -f tomcat/Dockerfile tomcat/
Sending build context to Docker daemon 8.188MB
Step 1/2 : FROM tomcat
---> 168588387c68
Step 2/2 : COPY web.war /usr/local/tomcat/webapps
---> 2c93f355880e
Successfully built 2c93f355880e
- 运行容器
wang@wang-pc:~/txt/docker-test/web-docker$ docker run -d -P 2c93f355880e
c71c16f561a2bb1c96cb60e053e87891f2e97db5b91721876c8519ddbaad368e
dwang@wang-pc:~/txt/docker-test/web-docker$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c71c16f561a2 2c93f355880e "catalina.sh run" 3 seconds ago Up 1 second 0.0.0.0:32768->8080/tcp priceless_wright
2, centos镜像版
- Dockerfile
wang@wang-pc:~/txt/docker-test/web-docker$ ls centos/
apache-tomcat-7.0.92.tar.gz Dockerfile entrypoint.sh jdk1.8.tar.gz web.war
wang@wang-pc:~/txt/docker-test/web-docker$ cat centos/Dockerfile
FROM centos
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN tar -zxvf apache-tomcat-7.0.92.tar.gz && tar -zxvf jdk1.8.tar.gz
RUN echo 'export PATH=$PATH:/app/jdk1.8/bin' >>/etc/profile
RUN echo 'export JAVA_HOME=/app/jdk1.8' >>/etc/profile
RUN ln -sf apache-tomcat-7.0.92 tomcat
RUN chmod +x entrypoint.sh
COPY web.war tomcat/webapps
EXPOSE 8080
CMD ["/app/entrypoint.sh"]
wang@wang-pc:~/txt/docker-test/web-docker$ cat centos/entrypoint.sh
#!/bin/sh
source /etc/profile
/app/tomcat/bin/catalina.sh run
- 编译镜像
wang@wang-pc:~/txt/docker-test/web-docker$ docker build -f centos/Dockerfile centos/
Sending build context to Docker daemon 208.2MB
Step 1/12 : FROM centos
---> 1e1148e4cc2c
Step 2/12 : RUN mkdir /app
---> Using cache
---> 046cbd73b61b
Step 3/12 : COPY . /app
---> Using cache
---> 0decb22ed490
Step 4/12 : WORKDIR /app
---> Using cache
---> ecf1ad73c5a6
Step 5/12 : RUN tar -zxvf apache-tomcat-7.0.92.tar.gz && tar -zxvf jdk1.8.tar.gz
---> Using cache
---> 49bd733b326f
Step 6/12 : RUN echo 'export PATH=$PATH:/app/jdk1.8/bin' >>/etc/profile
---> Using cache
---> 9979258f6c4a
Step 7/12 : RUN echo 'export JAVA_HOME=/app/jdk1.8' >>/etc/profile
---> Using cache
---> 3dea4e296cf2
Step 8/12 : RUN ln -sf apache-tomcat-7.0.92 tomcat
---> Using cache
---> 76f4f56a7064
Step 9/12 : RUN chmod +x entrypoint.sh
---> Using cache
---> 8e62226d5c0f
Step 10/12 : COPY web.war tomcat/webapps
---> Using cache
---> c97dd34ddbb0
Step 11/12 : EXPOSE 8080
---> Using cache
---> 33c02e8e5e53
Step 12/12 : CMD ["/app/entrypoint.sh"]
---> Using cache
---> d38dac2ca09c
Successfully built d38dac2ca09c
- 运行容器
wang@wang-pc:~/txt/docker-test/web-docker$ docker run -d -P d38dac2ca09c
ca88879c88ab6040efe3eb9d0f09463b88133905067a585f4651ec12ba092e94
wang@wang-pc:~/txt/docker-test/web-docker$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca88879c88ab d38dac2ca09c "/app/entrypoint.sh" 4 seconds ago Up 1 second 0.0.0.0:32769->8080/tcp priceless_engelbart
c, 访问web页面,测试结果
更多推荐
已为社区贡献6条内容
所有评论(0)