I created a dockerFile and docker-compose.yml:
Dockerfile
FROM php:7
RUN apt-get update -y && apt-get install -y libmcrypt-dev openssl
RUN docker-php-ext-install pdo mcrypt mbstring
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
docker-compose.yml
version: '3'
services:
web:
build:
context: .
dockerfile: ./Dockerfile
ports:
- 3021:8000
volumes:
- ./laravel-app:/app
When I executedocker-compose up –force-recreate -dAfter that, laravel app will127.0.0.1:3021On the correct implementation.
I’ve seen many docker-composer articles on the Internet that encourage the minimization of container, so I want to ask.Is there any way to rewrite this docker-compose.yml using php and composer’s official image?
The following are my attempts:
version: '3'
services:
php:
image: php:7-fpm
ports:
- "3021:8000"
volumes:
- ./laravel-app:/app
composer:
image: composer:latest
volumes:
- ./laravel-app:/app
working_dir: /app
command: ["install","php artisan serve --host=0.0.0.0"]
depends_on:
- php
After completing this docker-compose.yml, I will execute itdocker-compose up –force-recreate -d, however127.0.0.1:3021However, no information was captured on the website.
I’ll follow updocker-compose psAnddocker-compose log, found that composer’s container was not started with the following error information
Invalid argument php artisan serve --host=0.0.0.0. Use "composer require php artisan serve --host=0.0.0.0" instead to add packages to your composer.json.
May I know how to amend it?
When Dockerfile was built, the basic image was the alpine image version, and then the composer construction was separated into a single stage (multi-stage construction).
What I mean is that it is meaningless to simply tear down the container to a minimum. On the contrary, it may cause bugs or bottlenecks when network data frequently enter and leave the container. Moreover, the way of building+docker-compose.yml to pull up services through Dockerfile was quite good before, and it was not necessary to write all of them into dockercomposition.yml. In addition, some things, such as mbstring, exist in the basic image itself, so there is no need to install them again. It is recommended that you goDocker StoreSearch the underlying image (point Tag to see the build script), and then select the appropriate Tag.