简体   繁体   English

Docker自动构建的多映像

[英]Multi image with Docker automated build

I link my hub.docker.com account with bitbucket.org for automated build. 我将hub.docker.com帐户与bitbucket.org关联以进行自动构建。 In core folder of my repository exist Dockerfile, which is inside 2 image building steps. 在我存储库的核心文件夹中存在Dockerfile,它位于2个映像构建步骤中。 If I build images based same Dockerfile in local (i mean in Windows), I get 2 different images. 如果我在本地(我的意思是在Windows中)基于相同的Dockerfile构建映像,则会得到2个不同的映像。 But if I will use hub.docker.com for building, only last image is saved and tagged as "latest". 但是,如果我将使用hub.docker.com进行构建,则只会保存最后一张图像并将其标记为“最新”。

Dockerfile: Dockerfile:

#-------------First image ----------
FROM nginx 

#-------Adding html files
RUN mkdir /usr/share/nginx/s1
COPY content/s1 /usr/share/nginx/s1
RUN mkdir /usr/share/nginx/s2
COPY content/s2 /usr/share/nginx/s2

# ----------Adding conf file
RUN rm -v /etc/nginx/nginx.conf
COPY conf/nginx.conf /etc/nginx/nginx.conf

RUN service nginx start


# ------Second image -----------------
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8080 available to the world outside this container
EXPOSE 8080

# The application's jar file
ARG JAR_FILE=jar/testbootstap-0.0.1-SNAPSHOT.jar

# Add the application's jar to the container
ADD ${JAR_FILE} test.jar

# Run the jar file 
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/test.jar"]

Anybody did this before or its not possible? 有人以前这样做或不可能吗?

PS: There only one private repository for free use, may be this is main reason. PS:只有一个私有存储库可供免费使用,这可能是主要原因。

Whenever you specify a second FROM in your Dockerfile, you start creating a new image. 每当您在Dockerfile中指定第二个FROM时,就开始创建新映像。 That's the reason why you see only the last image being saved and tagged. 这就是为什么您只看到最后一张被保存和标记的图像的原因。

You can accomplish what you want by creating multiple Dockerfiles, ie by creating the first image in its Dockerfile and then using that to create the second image - all of it using docker-compose to co-ordinate between the containers. 您可以通过创建多个Dockerfile,即在其Dockerfile中创建第一个映像,然后使用该映像创建第二个映像来完成所需的全部操作,所有这些都使用docker-compose在容器之间进行协调。

I found some walk-around for this problem. 我发现了一些解决此问题的方法。

  1. I separate docker file to two file. 我将docker文件分为两个文件。

      1.docker for ngix 2.docker for java app 
  2. In build settings set this two files as dockerfile and tag with different tags. 在构建设置中,将这两个文件设置为dockerfile并使用不同的标签进行标记。 在此处输入图片说明

  3. After building you have one image but versions is define as image name. 构建后,您只有一个映像,但版本定义为映像名称。 For example you can use 例如,您可以使用

      for nginx server youraccount/test:nginx for app image youraccount/test:java 

在此处输入图片说明

在此处输入图片说明 I hope this will be no problem in future processes. 我希望这在将来的过程中不会有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM