如何合并两个或多个Docker映像 [英] How to combine two or more Docker images

查看:96
本文介绍了如何合并两个或多个Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手.我想用我的Web应用程序创建一个图像.我需要一些应用服务器,例如wlp,那么我需要一些数据库,例如Postgres.

I'm a newbie to docker. I want to create an image with my web application. I need some application server, e.g. wlp, then I need some database, e.g. postgres.

有一个用于wlp的Docker映像,还有一个用于postgres的Docker映像.

There is a Docker image for wlp and there is a Docker image for postgres.

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

So I created following simple Dockerfile.

FROM websphere-liberty:javaee7
FROM postgres:latest

现在,也许是个me脚,但是当我建立这张图片时

Now, maybe it's a lame, but when I build this image

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 为空.

only postgres is running and wlp is not even there. Directory /opt is empty.

我想念什么?

推荐答案

您需要使用docker-compose文件.这使您可以绑定运行两个不同映像的两个不同容器.一个拿着您的服务器,另一个拿着您的数据库服务.

You need to use docker-compose file. This makes you bind two different containers that are running two different images. One holding your server and the other the database services.

这是一个nodejs服务器容器与mongodb容器一起使用的示例

Here is the Example of a nodejs server container working with a mongodb container

首先,我编写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"

我希望这会有所帮助.

这篇关于如何合并两个或多个Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆