构建基本映像的Docker-compose.yml文件,然后基于该映像的子级? [英] Docker-compose.yml file that builds a base image, then children based on it?

查看:64
本文介绍了构建基本映像的Docker-compose.yml文件,然后基于该映像的子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为澄清起见,当我说基本映像时,我指的是具有所有通用配置的父映像,因此基于它的子级不需要单独下载依赖项.

For clarification, when I say base image, I mean the parent image that has all the common configurations, so that the children based on it don't need to download the dependencies individually.

据我了解,docker-compose.yml文件是运行时配置,而Dockerfiles是构建时配置.但是,有一个使用docker-compose的 build 选项,我想知道如何使用它来构建基础映像.

From my understanding, docker-compose.yml files are the run-time configurations, while Dockerfiles are the build-time configurations. However, there is a build option using docker-compose, and I was wondering how I could use this to build a base image.

截至目前,我使用的是运行其他shellscript的shellscript.一个人也从它创建的基础图像中构建了我所有的图像.另一个将它们作为具有必要配置的容器运行.但是,基本映像永远不会作为容器运行.

As of right now, I use a shellscript that runs other shellscripts. One builds all my images, from a base image that it also creates. The other runs them as containers with the necessary configurations. However, the base image is never ran as a container.

目前,我希望将shellscript更改为docker-compose文件,如下所示:

Currently, the shellscript I hope to change into a docker-compose file, looks like so:

echo "Creating docker network net1"
docker network create net1

echo "Running api as a container with port 5000 exposed on net1"
docker run --name api_cntr --net net1 -d -p 5000:5000 api_img

echo "Running redis service with port 6379 exposed on net1"
docker run --name message_service --net net1 -p 6379:6379 -d redis

echo "Running celery worker on net1"
docker run --name celery_worker1 --net net1 -d celery_worker_img

echo "Running flower HUD on net1 with port 5555 exposed"
docker run --name flower_hud --net net1 -d -p 5555:5555 flower_hud_img

制作图像的shellscript如下:

The shellscript that makes the images, is as follows:

echo "Building Base Image"
docker build -t base ../base-image

echo "Building api image from Dockerfile"
docker build -t api_img  ../api

echo "Building celery worker image"
docker build -t celery_worker_img ../celery-worker

echo "Building celery worker HUD"
docker build -t flower_hud_img ../flower-hud

我的问题归结为一件事,我是否可以创建此基本映像而无需在带有docker-compose的容器中运行它?(除了基本本身以外,所有Dockerfile均以 FROM base:latest 开头).我希望对其他人尽可能地简化此操作,以便他们只需要运行一个命令即可.

My questions comes down to one thing, can I create this Base image without ever running it in a container with docker-compose. (All the Dockerfiles start with FROM base:latest other than the base itself). I'm looking to make it as easy as possible for other people, so that they only have to run a single command.

我正在使用版本3,并且根据文档, build:被忽略,而docker-compose只接受预构建的图像.

I am using version 3, and acording to the docs, build: is ignored, and docker-compose only accepts pre-built images.

推荐答案

根据文档,服务的 build 选项将目录作为参数,其中包含著名的 Dockerfile .无法建立基础映像,然后再建立服务的实际映像.

As per the documentation the build option of a service takes a directory as an argument which contains the famous Dockerfile. There is no way to build a base image and then the actual image of the service.

Docker是您的应用程序在其中运行的环境.在创建基本映像时,它应该包含不会经常更改的内容.然后,您需要构建一次baseiamge,然后上传到您的存储库,并在Dockerfile中使用 FROM baseimage:latest .

Docker is a environment in which your application runs. When you are creating a base image, it should have things which are not going to change often. Then you need to build baseiamge once and upload to your repository and use FROM baseimage:latest in the Dockerfile.

例如,如果您要构建python应用程序,则可以从python创建它并安装要求:

For example, if you are building a python application you can create it from python and install requirements:

FROM python:3.6
COPY requirements.txt .
RUN pip install -r requirements.txt

此处, python:3.6 基本映像,该映像不会经常更改,因此您不必在每次运行docker compose命令时都构建它..

这篇关于构建基本映像的Docker-compose.yml文件,然后基于该映像的子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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