简体   繁体   English

docker 从 docker-compose.yml 构建自定义镜像

[英]docker build a custom image from docker-compose.yml

I have a setup where I have a Dockerfile and a docker-compose.yml.我有一个设置,我有一个 Dockerfile 和一个 docker-compose.yml。

Dockerfile: Dockerfile:

# syntax=docker/dockerfile:1
FROM php:7.4
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN apt-get -y update
RUN apt-get -y install git
COPY . .
RUN composer install

YML file: YML 文件:

version: '3.8'
services:
  foo_db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=foo
      - MYSQL_DATABASE=foo
  foo_app:
    image: foo_php
    platform: linux/x86_64
    restart: unless-stopped
    ports:
      - 8000:8000
    links:
      - foo_db
    environment:
      - DB_CONNECTION=mysql
      - DB_HOST=foo_db
      - DB_PORT=3306
      - DB_PASSWORD=foo
    command: sh -c "php artisan serve --host=0.0.0.0 --port=8000"
  foo_phpmyadmin:
    image: phpmyadmin
    links:
      - foo_db
    environment:
      PMA_HOST: foo_db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
      PMA_USER: root
      PMA_PASSWORD: foo
    restart: always
    ports:
      - 8081:80

In order to set this up on a new workstation the steps I am taking are first running:为了在新工作站上进行设置,我首先运行的步骤是:

docker build -t foo_php .

As I understand it this runs the commands in the Dockerfile and creates a new image called foo_php.据我了解,这将运行 Dockerfile 中的命令并创建一个名为 foo_php 的新图像。

Once that is done I am running docker compose up .一旦完成,我将运行docker compose up

Question:问题:

How can I tell docker that I would like my foo_app image to be automatically built, so that I can skip the step of first building the image.如何告诉 docker 我希望自动构建我的 foo_app 映像,这样我就可以跳过首先构建映像的步骤。 Ideally I would have one command similar to docker compose up that I could call each time I want to launch my containers.理想情况下,我将有一个类似于docker compose up ,每次我想启动容器时都可以调用它。 The first time it would build the images it needs including this custom image of mine described in the Dockerfile, and subsequent times calling it would just run these images.第一次它会构建它需要的图像,包括我在 Dockerfile 中描述的这个自定义图像,随后调用它只会运行这些图像。 Does a method to achieve this exist?是否存在实现这一目标的方法?

You can ask docker compose to build the image every time:您可以要求 docker 撰写每次构建图像:

docker compose up --build

But you need to also instruct docker compose on what to build:但是您还需要指示 docker 撰写要构建的内容:

foo_app:
    image: foo_php
    build:
      context: .

where context points to the folder containing your Dockerfile其中context指向包含您的 Dockerfile 的文件夹

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

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