0

Following is what I tried, is there something I doing wrong?

step1. create a simple laravel app at localhost.

composer create-project --prefer-dist laravel/laravel laravel-app 5.6

step2. create docker-compose.yml

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 that, I run docker-compose up --force-recreate -d and access 127.0.0.1:3021 at browser, but I get nothing.

Then I run docker-composer log, it shows me this error message:

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.

How to fix this issue?

2 Answers 2

3

in the main folder of your Laravel app, create a file named Dockerfile and insert this code:

FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
WORKDIR /app
COPY . /app
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181

In the same main folder of Dockerfile, create a file named docker-compose.yml and insert this code:

version: '2'
services:
  app:
    build: .
    ports:
      - "8009:8000"
    volumes:
      - .:/app
    env_file: .env
    working_dir: /app
    command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0'
    depends_on:
      - db
    links:
      - db
  db:
    image: "mysql:5.7"
    environment:
      - MYSQL_ROOT_PASSWORD=yourpassword
      - MYSQL_DATABASE=yourdbname
      - MYSQL_USER=root
      - MYSQL_PASSWORD=yourpassword
    volumes:
      - ./data/:/var/lib/mysql
    ports:
      - "3306:3306"
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - 8090:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: yourpassword

Open the terminal command line and go inside the laravel folder, and launch this commands:

docker.compose build
docker-compose up -d

if have need to create and migrate the db, or use other commands, launch the Laravel commands in this way:

docker-compose run app php artisan The app will available at the address http://0.0.0.0:8009

Source: https://medium.com/@pierangelo1982/dockerize-an-existing-laravel-application-with-docker-compose-a45eb7956cbd

Sign up to request clarification or add additional context in comments.

Comments

1

You are mixing commands. Composer does not "serve". Php has a build in dev server to "serve".

You can read more about it here: https://laravel.com/docs/4.2/quick

To actually get Laravel up and running please do the following:

1 - Run this in the laravel-app folder: composer install

2 - Create a Dockerfile with the following contents:

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

3 - Build your docker image: docker build -t my-laravel-image .

4 - Finally replace the content of your docker-compose:

version: '3'
services:
  web:
    image: my-laravel-image
    ports:
        - 3021:8000
    volumes:
        - ./laravel-app:/app

A more complete tutorial can be found here (not mine): https://www.techiediaries.com/docker-compose-laravel/

EDIT:

in order to use the official compose image you could simply do this:

version: '3'

services:
    composer:
        image: composer:latest
        working_dir: /app
        entrypoint: php artisan serve --host=0.0.0.0
        depends_on:
            - php
        volumes:
            - ./laravel-app:/app
        ports:
          - "3021:8000"

Make sure ./laravel-app contains a laravel project. Otherwise this won't work!

1 Comment

Thanks man, but can I using php and composer offical image to do the same thing? Is that possible to use links、networks or depends_on to make this work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.