说明:由于docker build 命令默认寻找Dockerfile文件,以下为了实验,应此使用了不同名的文件,在构建镜像时需要指定配置文件: docker build -f -f 文件名
entrypoint | cmd | |
---|---|---|
相同点 | 可以有多个cmd或entrypoint, 但只执行最后一个 | |
不同点1 | 启动命令在docker run 镜像 …时不能覆盖 | 启动命令可以覆盖 |
不同点2 | 参数可以在docker run时动态传入 | 参数不能动态传入 |
原始命令 ==> docker build -t test1 -f entrypoint.conf .
==> docker run -it test1 bash a b c
==>结果: 打印出 a b c
==> docker run -it test1 ls /
==>结果: 打印出 ls /
#ENTRYPOINT ["executable", "param1", "param2"]
#ENTRYPOINT command param1 param2(shell中执行)。
FROM centos
###a.sh: echo $@
#COPY a.sh /
#ENTRYPOINT ["/a.sh"]
#等价于:上面的/a.sh
ENTRYPOINT echo $@
原始命令 ==> docker build -t test2 -f cmd.conf .
==> docker run -it test2
==>结果: 打印出 hello world , good
覆盖启动命令==> docker run -it test1 ls /
==>结果: 打印出 bin etc lib media opt root sbin sys usr
FROM centos
#CMD cat /etc/hosts
CMD echo "hello world" && echo "good...."
原始命令 ==> docker build -t test3 -f cmd-entrypoint.conf .
==> docker run -it test3
==>结果: 打印出 /bin/sh -c ls /
==>总结: entrypoint,cmd都存在时(与先后顺序无关),cmd的参数传给了entrypoint
覆盖启动命令_参数==> docker run -it test1 cat /etc/hosts
==>结果: 打印出 cat /etc/hosts
==>总结: docker run的参数,覆盖了内置的cmd参数,最后传给了entrypoint执行
FROM centos
###a.sh: echo $@
COPY a.sh /
RUN chmod +x /a.sh
CMD ls /
ENTRYPOINT ["/a.sh"]
打包下面的文件为可执行的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)));
}
}
}
准备的文件如下
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
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:
根据上篇博客 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
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
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
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