我想天真地使用这个dockerfile。
FROM test/tomcat ENV CATALINA_HOME /tomcat RUN ${CATALINA_HOME}/bin/catalina.sh start RUN wget localhost:8080/Writer/WriterServlet
结果如下
docker build --no-cache=true - < Dockerfile Sending build context to Docker daemon 3.072 kB Sending build context to Docker daemon Step 0 : FROM test/tomcat ---> 2a85ccb3dd07 Step 1 : ENV CATALINA_HOME /tomcat ---> Running in 76872d1acdd5 ---> 30ad1fe832d6 Removing intermediate container 76872d1acdd5 Step 2 : RUN ${CATALINA_HOME}/bin/catalina.sh start ---> Running in 5947560a3428 Tomcat started. ---> ea670dd1d3ff Removing intermediate container 5947560a3428 Step 3 : RUN wget localhost:8080/Writer/WriterServlet ---> Running in 6aa5c9be8569 --2014-12-19 03:08:16-- http://localhost:8080/Writer/WriterServlet Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:8080... failed: Connection refused. Connecting to localhost (localhost)|127.0.0.1|:8080... failed: Connection refused. 2014/12/19 11:08:16 The command [/bin/sh -c wget localhost:8080/Writer/WriterServlet] returned a non-zero code: 4
在这种情况下,tomcat提供了一些编写文件的接口,所以我需要通过tomcat服务器构建。
也许是因为wget无法等待tomcat启动。
或者因为docker的分层容器结构导致在步骤3(wget)中,tomcat再次失效。
总而言之,我的问题是,我可以编写一个docker构建来保持一些进程一起运行以满足交互式需求吗?
答案 0 :(得分:2)
Docker文件构建步骤不维护正在运行的容器或系统。每行都对文件系统进行了更改,docker仅将更改存储到文件系统。任何已启动的进程都将被丢弃。
Docker只能在运行容器时启动长时间运行的进程(而不是在构建容器时)。您需要使用ENTRYPOINT或CMD在容器启动时调用自定义脚本。此脚本将启动tomcat并运行wget并在启动长时间运行的进程(例如tomcat日志中的尾部)之前进行所需的任何更改。