Docker

Asked

Viewed 241 times

1

I am setting up a development environment with Docker. In it I need

  • NGINX (webservice)
  • PHP 7.2 (app)
  • MARIADB 10.3 (database)

In the container app I must install Composer, I will work using Windows.

When using RUN composer install and COPY .env.example .env in the dockerfile to install the vendor and configure the .env, got no error message but also no file was created.

Docker-Compose.yml

version: '3'
services:
  app:
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    build:
      context: .
      dockerfile: Dockerfile
    image: php:7.2
    container_name: app
    restart: unless-stopped
    tty: true
    working_dir: /var/www
    networks:
      - app-network


  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    networks:
      - app-network
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/

  db:
    image: mariadb:10.3
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: db
      MYSQL_ROOT_PASSWORD: pass
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - app-network
    volumes:
      - dbdata:/var/lib/mysql

networks:
  app-network:
    driver: bridge

volumes:
  dbdata:
    driver: local

dockerfile

FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    mariadb-client-10.3 \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Run composer install
RUN composer install

# Create the .env from the .env.example
COPY .env.example .env

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

1 answer

0

We can divide your process into 2.

  1. the build that is running Composer installs and is not giving error, as it is working properly and creating the vendor folder.

  2. After the Docker-Compose build is executed and it mirrors its folder without the vendor inside the container by subscribing to what is inside the container, so you get a directory without the installed Composer

You can solve it in two ways.

  1. run the command Composer install after the application has climbed into the correct container, in your case have the webserver and the app, I will try with app.

    Docker-Compose exec app sh -c "Composer install"

  2. Run the command inside the Docker-Compose, so every time the application starts it will run the composer install

your Docker-Compose:

version: '3'
services:
  app:
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    command:  /bin/bash -c "composer install && php server"
    build:
      context: .
      dockerfile: Dockerfile
    image: php:7.2
    container_name: app
    restart: unless-stopped
    tty: true
    working_dir: /var/www
    networks:
      - app-network

OBS: remember that when you run the command always the last command is what will let the container run, so it has to be a Demon application like php server, it can be any other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.