title: How to test a PHP API in a Docker container
body: <p>I have an api built with PHP/Symfony, inside a Docker container. I would like to test it.</p>
  <p>To do that :</p>
  <ul>
  <li><p>first : I enter into my container : docker-compose exec da-invoicing-php sh</p>
  </li>
  <li><p>second : I run the test : vendor/bin/simple-phpunit</p>
  </li>
  </ul>
  <p>In my test I have this request :</p>
  <pre><code>$result = $this-&gt;client-&gt;request(
          'POST',
          '10.110.167.124:8080/api/v1/course_invoices',
          [
              RequestOptions::HEADERS =&gt; [
                  'Accept' =&gt; 'application/ld+json',
                  'Content-Type' =&gt; 'application/json',
                  'Authorization' =&gt; &quot;Bearer {$this-&gt;token}&quot;,
              ],
              RequestOptions::BODY =&gt; json_encode([
                  'courseInstanceId' =&gt; self::COURSE_INSTANCE,
              ]),
          ]
      );
  </code></pre>
  <p>As you can see, I request to the endpoint &quot;'10.110.167.124:8080/api/v1.....&quot;. It works but I know that I cannot continue like that.</p>
  <p>I tried with 'localhost', 'localhost:8080', 'http://localhost', etc... without success. I always have this error :</p>
  <blockquote>
  <p>GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to localhost port 8080: Connection refused (see <a href="https://curl.haxx.se/libcurl/c/libcurl-errors.html" rel="nofollow noreferrer">https://curl.haxx.se/libcurl/c/libcurl-errors.html</a>)</p>
  </blockquote>
  <p>So how to do this test inside a container?</p>
  <h2>Edit</h2>
  <p>My docker-compose</p>
  <pre><code>services:
    da-invoicing-php:
      build:
          context: .
          args:
              GITHUB_TOKEN: 060......2e8b
      container_name: da-invoicing-php
      depends_on:
        - da-invoicing-db
      env_file:
        - .env.local
      # Comment out this volume in production
      volumes:
        - .:/srv/api:rw,cached
  
    da-invoicing-api:
      build:
        context: .
        dockerfile: Dockerfile.nginx
      depends_on:
        - da-invoicing-php
      volumes:
        - ./public:/srv/api/public:ro
      environment:
        - INTERNAL_DNS_RESOLVER=127.0.0.11
      ports:
        - &quot;8080:80&quot;
  
    da-invoicing-db:
      image: postgres:9.6-alpine
      environment:
        - POSTGRES_DB=api
        - POSTGRES_USER=api-platform
        # You should definitely change the password in production
        - POSTGRES_PASSWORD=!ChangeMe!
      ports:
        - &quot;1234:5432&quot;
  </code></pre>
  <p>My Dockerfile :</p>
  <pre><code>    FROM php:7.3-fpm-alpine
  
  RUN apk add --no-cache  \
          git
  
  ARG http_proxy
  ARG APCU_VERSION=5.1.17
  RUN if [ ! -z $http_proxy ] ; then pear config-set http_proxy $http_proxy; fi \
    &amp;&amp; set -xe \
      &amp;&amp; apk add --no-cache --virtual .build-deps \
          $PHPIZE_DEPS \
          icu-dev \
          postgresql-dev \
          libzip-dev \
      &amp;&amp; docker-php-ext-install -j$(nproc) \
          intl \
          pdo_pgsql \
          zip \
      &amp;&amp; pecl install \
          apcu-${APCU_VERSION} \
      &amp;&amp; pecl clear-cache \
      &amp;&amp; docker-php-ext-enable --ini-name 20-apcu.ini apcu \
      &amp;&amp; docker-php-ext-enable --ini-name 05-opcache.ini opcache \
      &amp;&amp; runDeps=&quot;$( \
          scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
              | tr ',' '\n' \
              | sort -u \
              | awk 'system(&quot;[ -e /usr/local/lib/&quot; $1 &quot; ]&quot;) == 0 { next } { print &quot;so:&quot; $1 }' \
      )&quot; \
      &amp;&amp; apk add --no-cache --virtual .api-phpexts-rundeps $runDeps \
      &amp;&amp; apk del .build-deps
  
  COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
  
  # Supervisor
  RUN apk add --no-cache supervisor
  COPY docker/php/supervisor/*.ini /etc/supervisor.d/
  
  ARG XDEBUG=false
  ARG XDEBUG_VERSION=2.6.0
  RUN if [ &quot;$XDEBUG&quot; != &quot;false&quot; ] ;then \
      set -eux; \
      apk add --no-cache --virtual .build-deps $PHPIZE_DEPS; \
      pecl install xdebug-$XDEBUG_VERSION; \
      docker-php-ext-enable xdebug; \
      apk del .build-deps; \
  fi
  
  # https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
  ENV COMPOSER_ALLOW_SUPERUSER=1
  ENV COMPOSER_NO_INTERACTION=1
  ARG GITHUB_TOKEN
  RUN composer config -g github-oauth.github.com ${GITHUB_TOKEN}
  RUN composer global require &quot;symfony/flex&quot; --prefer-dist --no-progress --no-suggest --classmap-authoritative \
      &amp;&amp; composer clear-cache
  ENV PATH=&quot;${PATH}:/root/.composer/vendor/bin&quot;
  
  WORKDIR /srv/api
  
  # Build for production
  ARG APP_ENV=prod
  
  # Prevent the reinstallation of vendors at every changes in the source code
  COPY composer.json composer.lock ./
  RUN echo '&lt;?php return [];' &gt; .env.local.php
  
  RUN composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress --no-suggest \
      &amp;&amp; composer clear-cache
  
  # copy only specifically what we need
  COPY bin bin/
  COPY config config/
  COPY public public/
  COPY src src/
  COPY .env ./
  
  RUN set -eux; \
      mkdir -p var/cache var/log var/sessions; \
      composer dump-autoload --classmap-authoritative --no-dev; \
      composer run-script --no-dev post-install-cmd; \
      chmod +x bin/console; sync; \
      chown -R www-data var
      
  VOLUME /srv/api/var
  
  COPY docker/php/php.ini /usr/local/etc/php/php.ini
  COPY docker/php/fpm.d/zz-academy.conf /usr/local/etc/php-fpm.d/
  COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
  COPY docker/php/fpm.d/zz-academy.conf /usr/local/etc/php-fpm.d/
  RUN chmod +x /usr/local/bin/docker-entrypoint
  
  EXPOSE 9000
  
  ENTRYPOINT [&quot;docker-entrypoint&quot;]
  CMD [&quot;php-fpm&quot;]
  </code></pre>