如何组合两个或多个Docker镜像

时间:2018-01-16 14:52:27

标签: docker

我是码头工人的新手。 我想用我的Web应用程序创建一个图像。我需要一些应用服务器,例如wlp,然后我需要一些数据库,例如postgres的。

wlp有一个Docker镜像,而postgres有一个Docker镜像。

所以我创建了以下简单的Dockerfile。

FROM websphere-liberty:javaee7
FROM postgres:latest

现在,也许它是一个跛脚,但是当我构建这个图像时

docker build -t wlp-db .

运行容器

docker run -it --name wlp-db-test wlp-db

并检查

docker exec -it wlp-db-test /bin/bash

只有postgres正在运行,wlp甚至没有。目录 / opt 为空。

我错过了什么?

3 个答案:

答案 0 :(得分:3)

每个服务都应该有自己的image / dockerfile。您启动多个容器并通过网络连接它们以便能够进行通信 如果您希望在一个文件中组合多个容器,请查看docker-compose,这是为此而制作的!

答案 1 :(得分:2)

您不能在一个文件中多次FROM并期望两个进程都运行

这是从图像创建每个图层,但只有一个入口点,即Postgres,因为它是第二个

此模式通常仅在您拥有一些“设置”泊坞窗图像,然后在其上面设置“运行时”图像时才会执行。

https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds

你正在尝试做的事情并不是非常依赖“微服务”。从应用程序中单独运行数据库。 Docker Compose可以为您提供帮助,几乎所有Dockers网站上的示例都使用Postgres和一些Web应用程序

另外,您正在启动一个空的数据库和服务器。例如,您需要复制至少一个WAR来运行服务器代码

答案 2 :(得分:1)

您需要使用docker-compose文件。这使您可以绑定两个运行两个不同图像的不同容器。一个持有你的服务器,另一个持有数据库服务。

以下是使用mongodb容器的nod​​ejs服务器容器的示例

首先,我编写了docker文件来配置主容器

FROM node:latest

RUN mkdir /src

RUN npm install nodemon -g

WORKDIR /src
ADD app/package.json package.json
RUN npm install

EXPOSE 3000

CMD npm start

然后我创建docker-compose文件来配置两个容器并链接它们

version: '3' #docker-compose version
services:  #Services are your different containers
  node_server: #First Container, containing nodejs serveer
    build: . #Saying that all of my source files are at the root path
    volumes: #volume are for hot reload for exemple
      - "./app:/src/app"
    ports:   #binding the host port with the machine
      - "3030:3000"
    links:   #Linking the first service with the named mongo service (see below)
      - "mongo:mongo" 
  mongo: #declaration of the mongodb container
    image: mongo #using mongo image
    ports:  #port binding for mongodb is required
      - "27017:27017"

我希望这有帮助。