title: Why are my composer dependencies not found inside a docker container but are natively on my mac?
body: <p>Within my php application, I have several dependencies which are installed via composer. When I build this application natively on my mac machine all the php dependencies are installed correctly. When the application is run the: namespaces, files and classes can all be found by composer's autoloader.</p>
  
  <p>However, I wish to run this application inside a docker container, so that I can push the container up to ECS. When I run the same application (which works natively on my mac) inside a container. Composer's autoloader fails to find some class, and instead throws exceptions like this: <code>PHP Fatal error:  Uncaught Error: Class ... not found in ...</code>. </p>
  
  <p>The really weird part is that the correct namespaces show up in <code>vendor/composer/autoload_psr4.php</code> (none missing) and the class files do exist inside the vendor directory, in the docker container. </p>
  
  <p>The only solution I could find (to get composer to find the classes) is to delete the vendor's directory and re-run <code>composer install</code> all inside the docker container. But I don't want to have todo this. I should just be able to publish and deploy the container to ECS without having to install my composer dependencies again, after building. I have also tried running <code>composer dump-autoloader</code> in the entry point, but this did not resolve my issue. </p>
  
  <p>Has anyone else come across an issue like this before with: php, composer and docker?</p>
  
  <p>Here are some useful files:</p>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM ubuntu:xenial
  
  # install dependencies
  RUN apt-get update
  RUN apt-get install -y software-properties-common python-software-properties
  RUN apt-get install -y language-pack-en-base
  RUN LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
  
  # setup nginx and PHP
  RUN apt-get update &amp;&amp; \
      apt-get install -y nginx \
      php7.1 \
      php7.1-fpm \
      php7.1-cli \
      php7.1-common \
      php7.1-json \
      php7.1-opcache \
      php7.1-mysql \
      php7.1-mbstring \
      php7.1-gd \
      php7.1-imap \
      php7.1-ldap \
      php7.1-dev \
      php7.1-intl \
      php7.1-gd \
      php7.1-curl \
      php7.1-zip \
      php7.1-xml \
      php-redis
  
  # change the configuration to disable clear_env
  RUN sed -e 's/;clear_env = no/clear_env = no/' -i /etc/php/7.1/fpm/pool.d/www.conf
  
  # set up composer (we don't checksum the setup script)
  ENV COMPOSER_HOME /opt/composer
  RUN mkdir $COMPOSER_HOME &amp;&amp; \
      cd $COMPOSER_HOME &amp;&amp; \
      php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" &amp;&amp; \
      php composer-setup.php &amp;&amp; \
      php -r "unlink('composer-setup.php');" &amp;&amp; \
      rm keys.dev.pub &amp;&amp; \
      rm keys.tags.pub
  COPY composer.sh /usr/bin/composer
  RUN chmod 744 /usr/bin/composer
  
  # download and install Symfony
  RUN mkdir -p /usr/local/bin &amp;&amp; \
  curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony &amp;&amp; \
  chmod a+x /usr/local/bin/symfony
  
  ARG VERSION
  ENV VERSION $VERSION
  
  ENV WORK_DIR /opt/api
  
  # copy everything to /opt/api
  COPY . $WORK_DIR
  RUN chmod a+x /opt/api/entrypoint.sh
  
  # move the configuration file to the right place
  RUN mv $WORK_DIR/nginx.conf /etc/nginx/sites-available/default
  
  # fix the permissions for nginx and PHP
  RUN chown -R www-data:www-data $WORK_DIR/var &amp;&amp; \
      chown -R www-data:www-data $WORK_DIR/app
  
  # expose port 8000
  EXPOSE 8000
  
  # make it a working directory
  WORKDIR $WORK_DIR
  
  ENTRYPOINT ["/bin/sh", "/opt/api/entrypoint.sh"]
  </code></pre>
  
  <p>docker-compose.yml</p>
  
  <pre><code>app_api:
  image: app/api:latest
    command: run
    links:
      - redis
      - mysql
    ports:
      - 8000:8000
    env_file:
      - ./app/config/parameters.env
    environment:
      DB_HOST: mysql
      DB_PORT: 3306
      DB_NAME: xxx
      DB_USER: xxx
      DB_PASSWORD: xxx
      REDIS_HOST: redis
      SECRET: xxx
      ENVIRONMENT: dev
      SUBENVIRONMENT: default
  
  mysql:
    image: mysql:5.7
    expose:
    - 3306
    environment:
      MYSQL_ROOT_PASSWORD: xxx
      MYSQL_USER: xxx
      MYSQL_PASSWORD: xxx
      MYSQL_DATABASE: xxx
  
  redis:
    image: redis:alpine
    expose:
      - 6379
  </code></pre>
  
  <p>entrypoint.sh</p>
  
  <pre><code>#!/usr/bin/env bash
  
  case $1 in
  
  run)
      echo "---&gt; RUN app/api"
      echo "clear_env = no" &gt;&gt; /etc/php/7.1/fpm/pool.d/www.conf
  
      # start the PHP thing
      /etc/init.d/php7.1-fpm start &amp;&amp; \
  
      # clear cache
      composer dump-autoload &amp;&amp; \
      php bin/console cache:clear --env=prod &amp;&amp; \
      chown -R www-data:www-data var/cache/prod
  
      # start the nginx in the foreground
      nginx -g "daemon off;"
  
      shift 1
  ;;
  </code></pre>
  
  <p>composer.json</p>
  
  <pre><code>{
    "name": "app/api",
    "license": "MIT",
    "type": "project",
    "autoload": {
      "psr-4": { "": "src/" },
      "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
    },
    "autoload-dev": {
      "psr-4": {
        "Tests\\": "tests/"
      }
    },
    "require": {
      "php": "&gt;=5.5.9",
      "symfony/symfony": "3.1.*",
      "doctrine/orm": "^2.5",
      "doctrine/doctrine-bundle": "^1.6",
      "doctrine/doctrine-cache-bundle": "^1.2",
      "symfony/swiftmailer-bundle": "^2.3",
      "symfony/monolog-bundle": "^2.8",
      "symfony/polyfill-apcu": "^1.0",
      "sensio/distribution-bundle": "^5.0",
      "sensio/framework-extra-bundle": "^3.0.2",
      "incenteev/composer-parameter-handler": "^2.0",
      "crell/api-problem": "^2.0",
      "prooph/service-bus-symfony-bundle": "^0.2.0",
      "prooph/event-store-symfony-bundle": "^0.2.2",
      "prooph/event-sourcing": "^4.0",
      "prooph/event-store-doctrine-adapter": "^3.2",
      "prooph/event-store-bus-bridge": "^2.0",
      "doctrine/doctrine-migrations-bundle": "^1.2",
      "nelmio/api-doc-bundle": "^2.13",
      "prooph/snapshot-doctrine-adapter": "^1.1",
      "mediamath/t1-php" : "^1.0.3",
      "guzzlehttp/guzzle": "^5.3.1|^6.2.1",
      "league/statsd": "^1.4",
      "prooph/snapshotter": "^1.1",
      "phpunit/phpunit": "^6.2.1"
    },
    "require-dev": {
      "sensio/generator-bundle": "^3.0",
      "symfony/phpunit-bridge": "^3.0"
    },
    "scripts": {
      "symfony-scripts": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
      ],
      "post-install-cmd": [
        "@symfony-scripts"
      ],
      "post-update-cmd": [
        "@symfony-scripts"
      ]
    },
    "config": {
      "platform": {
        "php": "7.0.11"
      }
    },
    "extra": {
      "symfony-app-dir": "app",
      "symfony-bin-dir": "bin",
      "symfony-var-dir": "var",
      "symfony-web-dir": "web",
      "symfony-tests-dir": "tests",
      "symfony-assets-install": "relative",
      "incenteev-parameters": {
        "file": "app/config/parameters.yml"
      },
      "branch-alias": {
        "dev-master": "3.1-dev"
      }
    }
  }
  </code></pre>