Home > database >  docker build a custom image from docker-compose.yml
docker build a custom image from docker-compose.yml

Time:09-21

I have a setup where I have a Dockerfile and a docker-compose.yml.

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:

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.

Once that is done I am running 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. Ideally I would have one command similar to docker compose up that I could call each time I want to launch my containers. 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. Does a method to achieve this exist?

CodePudding user response:

You can ask docker compose to build the image every time:

docker compose up --build

But you need to also instruct docker compose on what to build:

foo_app:
    image: foo_php
    build:
      context: .

where context points to the folder containing your Dockerfile

  • Related