1

I need have problem with instance the vendor in my project.

My php dockerfile

FROM php:7.2.1-fpm

RUN apt-get update

# Install tools
RUN apt-get install -y \
    python-setuptools \
    software-properties-common \
    wget \
    git \
    curl \
    zip \
    vim

RUN echo 'deb http://httpredir.debian.org/debian jessie contrib' >> /etc/apt/sources.list

RUN apt-get update
RUN export DEBIAN_FRONTEND=noninteractive && apt-get install -y --force-yes libssl-dev curl  libcurl4-gnutls-dev libxml2-dev libicu-dev libmcrypt4 libmemcached11 openssl

#CONFIGURAÇÕES DO OPCACHE
RUN docker-php-ext-install opcache

#CONFIGURAÇÕES DO APCU
RUN pecl install apcu-5.1.5 && docker-php-ext-enable apcu

#LIBS EXTRAS
RUN docker-php-ext-install bcmath
RUN apt-get install -y libbz2-dev
RUN docker-php-ext-install bz2
RUN docker-php-ext-install mbstring
RUN apt-get install -y libpq-dev
RUN apt-get install -y libicu-dev
RUN docker-php-ext-install intl

#GD
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng16-16
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd

#PDO
RUN docker-php-ext-install pdo_mysql

#sudo
RUN apt-get update && apt-get install -y sudo && rm -rf /var/lib/apt/lists/*    

VOLUME /var/www/html
WORKDIR /var/www/html
RUN pwd

# Allow Composer to be run as root
ENV COMPOSER_ALLOW_SUPERUSER 1

# Install composer
RUN wget https://getcomposer.org/download/1.5.5/composer.phar
RUN chmod +x composer.phar
RUN sudo mv composer.phar /usr/local/bin/composer

# Install libs/var_dumper
RUN mkdir -p /var/.composer
RUN composer global --no-interaction --working-dir=/var/.composer require slim/slim

EXPOSE 9000

CMD ["php-fpm"]

I just wanna move the vendor folder to var/www/html

Docker-compose

nginx:
  container_name: nginx
  build: ./docker/nginx
  ports:
    - "80:80"
  volumes:
    - ./public:/var/www/html
  links:
        - php
        - mysql

php:
  container_name: php
  build: ./docker/php7
  volumes:
    - ./public:/var/www/html
  ports:
    - "9000:9000"
  links:
    - mysql

mysql:
  image: mysql:5.7
  restart: always
  ports:
    - "3306:3306"
  environment:
    - MYSQL_ROOT_PASSWORD=root
    - MYSQL_DATABASE=testedb

the vendor files are in /var/.composer and i can't acess them. I wanna know how i build correctly the image to use the slim framework and the others images without errors.

2 Answers 2

1

Dockerfile

FROM php:7.2-fpm

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

RUN docker-php-ext-install mysqli   

# 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

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

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

# Set working directory
WORKDIR /var/www/html

RUN composer install

# Expose port 9000 and start php-fpm server
EXPOSE 9000

CMD ["php-fpm"]

Docker-compose.yml

version: '3'

services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    volumes:
      - ./:/var/www/html
      - php-data:/var/www/html/vendor/
      - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

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

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: testedb
      MYSQL_ROOT_PASSWORD: root
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./docker/mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge
#Volumes
volumes:
  dbdata:
    driver: local
  php-data:

I do that, creating 3 volumes and put vendor in php-data

3
  • interesting... but then you have to manually install the vendor dir yourself, dont you? Jul 25, 2019 at 21:07
  • No, its create automatically, check in my github, but is in portuguese. I can translate after. Jul 29, 2019 at 1:44
  • hi @Breno that looks good. you have composer installed in your container, which isnt the case in my solution. i thought mine could help you achieving more lightweight container. good luck anyway, Jul 29, 2019 at 6:16
1

i consider docker multistage build a powerful mechanism, which you can use to achieve a lightweight container.

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

composer is not slim, i cant see why it should reside at your final image. if i were you, i would create a composer stage, where i copy the project and run composer install. then i would copy the vendor directory as an artifact on to the final stage, which wont have composer installed at all. heres a basic example:

#create vendor:
FROM prooph/composer:7.3 as vendor-installer
WORKDIR /var/www/html
COPY . .
RUN composer install

#final stage
FROM php:7.2.1-fpm
WORKDIR /var/www/html
COPY --from vendor-installer /var/www/html/vendor vendor

EDIT

you will also have to exclude the vendor dir from the volume statement in the docker-compose.yml, so it wont get overwritten:

php:
  container_name: php
  build: ./docker/php7
  volumes:
    - ./public:/var/www/html
    - /var/www/html/vendor 
3
  • Thanks for the help, i understand and test your code. But do the same thing, in the finals don't copy the vendor folder to www/html. I guess my docker-compose.yml was wrong. Jul 25, 2019 at 16:17
  • @BrenoSobral thats probably because of the volumes. you have to exclude it out. i showed you how. Jul 25, 2019 at 19:04
  • I'll post the solution. Jul 25, 2019 at 19:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.