最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

docker - Dockerizing laravel application - Stack Overflow

programmeradmin4浏览0评论

I am using Ubuntu using WSL in windows, and I do not want to install anything on Windows itself. Everything is offloaded to WSL. I have an up and running laravel application and I am trying to dockerize it without installing php, composer, mysql, or node js on my host machine or on WSL. I thought of keeping everything on the docker image without installing docker desktop for windows; everything is on WSL, but without any success. It stops at composer install step; it does not building the image.

Any help would be appreciated.

Here is my docker file

FROM php:8.2-cli

# Install dependencies
RUN apt-get update && apt-get install -y \
    unzip \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    zip \
    curl \
    && docker-php-ext-install pdo pdo_mysql gd zip

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Copy existing Laravel project
COPY . .

# Install Laravel dependencies
RUN composer install --no-interaction --optimize-autoloader

CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]

In addition, my docker-compose.yml file

version: "3.8"

services:
  app:
    build: .
    container_name: laravel_app
    working_dir: /var/www
    volumes:
      - .:/var/www
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      - APP_ENV=local
      - APP_DEBUG=true
      - APP_KEY=
    command: php artisan serve --host=0.0.0.0 --port=8000

  db:
    image: mysql:8
    container_name: laravel_db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: laraveluser
      MYSQL_PASSWORD: secret
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: laravel_phpmyadmin
    restart: unless-stopped
    depends_on:
      - db
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "8080:80"

volumes:
  dbdata:

I am using Ubuntu using WSL in windows, and I do not want to install anything on Windows itself. Everything is offloaded to WSL. I have an up and running laravel application and I am trying to dockerize it without installing php, composer, mysql, or node js on my host machine or on WSL. I thought of keeping everything on the docker image without installing docker desktop for windows; everything is on WSL, but without any success. It stops at composer install step; it does not building the image.

Any help would be appreciated.

Here is my docker file

FROM php:8.2-cli

# Install dependencies
RUN apt-get update && apt-get install -y \
    unzip \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    zip \
    curl \
    && docker-php-ext-install pdo pdo_mysql gd zip

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Copy existing Laravel project
COPY . .

# Install Laravel dependencies
RUN composer install --no-interaction --optimize-autoloader

CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]

In addition, my docker-compose.yml file

version: "3.8"

services:
  app:
    build: .
    container_name: laravel_app
    working_dir: /var/www
    volumes:
      - .:/var/www
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      - APP_ENV=local
      - APP_DEBUG=true
      - APP_KEY=
    command: php artisan serve --host=0.0.0.0 --port=8000

  db:
    image: mysql:8
    container_name: laravel_db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: laraveluser
      MYSQL_PASSWORD: secret
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: laravel_phpmyadmin
    restart: unless-stopped
    depends_on:
      - db
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "8080:80"

volumes:
  dbdata:
Share Improve this question asked Feb 16 at 12:41 islambassiemislambassiem 94 bronze badges 2
  • well laravel sail might help but you still need to have php and composer running on host machine laravel/docs/11.x/sail – Soneye Oluwasina Commented Feb 16 at 13:00
  • What is the exact command you're running, from what context, and what is the exact error you get? The volumes: mount in particular mounts content from whatever context the Docker daemon is running in, so depending on what exactly is in a container and what isn't, you might be overwriting your application code with an empty directory. – David Maze Commented Feb 16 at 14:44
Add a comment  | 

2 Answers 2

Reset to default 0

let me try to help.

You can try the following improvements:

  1. Use proper web server instead of laravel built-in dev server
  2. Specify composer version instead of composer:latest

Here is the Dockerfile and compose.yml setup I have. This assumes that the Dockerfile & compose.yml files are at the same level with your root laravel project. (I used from this gist to setup the Dockerfile)

Dockerfile

FROM php:8.2-apache

RUN apt-get update && apt-get install -y  \
    git \
    curl \
    zip \
    unzip \
    libfreetype6-dev \
    libjpeg-dev \
    libpng-dev \
    libwebp-dev \
    --no-install-recommends \
    && docker-php-ext-enable opcache \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_mysql -j$(nproc) gd \
    && apt-get autoclean -y \
    && rm -rf /var/lib/apt/lists/*

ENV APACHE_DOCUMENT_ROOT=/var/www/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

RUN echo "file_uploads = On\n" \
         "memory_limit = 1024M\n" \
         "upload_max_filesize = 512M\n" \
         "post_max_size = 512M\n" \
         "max_execution_time = 1200\n" \
         > /usr/local/etc/php/conf.d/uploads.ini

# add composer
COPY --from=composer:2.7.6 /usr/bin/composer /usr/local/bin/composer

RUN a2enmod rewrite headers

compose.yml

networks:
  backend:

services:

  # Application & web server
  app:
    build:
      context: .
    working_dir: /var/www
    volumes:
      - ./:/var/www
    depends_on:
      - "db"
    ports:
      - 8070:80
    networks:
      - backend
  db:
    image: mysql:8.0
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      MYSQL_DATABASE: "app"
      MYSQL_ROOT_PASSWORD: "abcd12345"
      MYSQL_PASSWORD: "secret"
      MYSQL_USER: "user"
    ports:
      - "33061:3306"
    networks:
      - backend

volumes:
  dbdata:

command: docker compose up -d at project root.

This should be able to install the composer dependencies, connect to your docker container database, and server your laravel project.

Hope this helps.

I recommend you to use serversideup/php image, it is a lightweight production-ready php image optimized for performance, specifically designed for Laravel development.

They have different versions (cli, fpm, fpm-apache, fpm-nginx), for local development I only use fpm-alpine and run the php server.

Here is my docker-compose for local development :

services:
  api:
  image: serversideup/php:8.4-fpm-alpine
  user: "${UID-1000}:${GID-1000}"
  environment:
    APP_NAME: ${APP_NAME}
    DB_CONNECTION: ${DB_CONNECTION}
    DB_HOST: ${DB_HOST}
    DB_DATABASE: ${DB_DATABASE}
    DB_USERNAME: ${DB_USERNAME}
    DB_PASSWORD: ${DB_PASSWORD}
    MAIL_MAILER: ${MAIL_MAILER}
    MAIL_HOST: ${MAIL_HOST}
    MAIL_PORT: ${MAIL_PORT}
  volumes:
    - ./src:/var/www/html
  depends_on:
    mysql:
      condition: service_healthy
  ports:
    - "8000:8000"
  command: [ "php", "/var/www/html/artisan", "serve", "--host=0.0.0.0" ]

mysql:
  image: mysql:8.0
  environment:
    MYSQL_RANDOM_ROOT_PASSWORD: 1
    MYSQL_DATABASE: ${DB_DATABASE}
    MYSQL_USER: ${DB_USERNAME}
    MYSQL_PASSWORD: ${DB_PASSWORD}
  volumes:
    - mysqldata:/var/lib/mysql
  healthcheck:
    test: [ "CMD", "mysqladmin", "ping", "--silent" ]
    interval: 5s
    start_period: 30s
    retries: 3
    timeout: 5s

phpmyadmin:
  image: phpmyadmin
  environment:
    PMA_HOST: ${DB_HOST}
    PMA_USER: ${DB_USERNAME}
    PMA_PASSWORD: ${DB_PASSWORD}
  volumes:
    - phpmyadmindata:/var/www/html
  ports:
    - "8080:80"
  depends_on:
    mysql:
      condition: service_healthy

mailpit:
  image: 'axllent/mailpit:latest'
  ports:
    - '1025:1025'
    - '8025:8025'

volumes:
  mysqldata:
  phpmyadmindata:
发布评论

评论列表(0)

  1. 暂无评论