【发布时间】:2020-01-08 15:52:47
【问题描述】:
我在 Docker 容器中使用 PHP/Symfony 构建了一个 api。我想测试一下。
要做到这一点:
-
首先:我进入我的容器:docker-compose exec da-invoicing-php sh
-
第二个:我运行测试:vendor/bin/simple-phpunit
在我的测试中我有这个要求:
$result = $this->client->request(
'POST',
'10.110.167.124:8080/api/v1/course_invoices',
[
RequestOptions::HEADERS => [
'Accept' => 'application/ld+json',
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$this->token}",
],
RequestOptions::BODY => json_encode([
'courseInstanceId' => self::COURSE_INSTANCE,
]),
]
);
如您所见,我向端点“'10.110.167.124:8080/api/v1.....”请求。它有效,但我知道我不能这样继续下去。
我尝试使用“localhost”、“localhost:8080”、“http://localhost”等...但没有成功。我总是有这个错误:
GuzzleHttp\Exception\ConnectException:cURL 错误 7:无法连接到 localhost 端口 8080:连接被拒绝(请参阅https://curl.haxx.se/libcurl/c/libcurl-errors.html)
那么如何在容器内进行这个测试呢?
编辑
我的 docker-compose
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:
- "8080:80"
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:
- "1234:5432"
我的 Dockerfile :
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 \
&& set -xe \
&& apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
icu-dev \
postgresql-dev \
libzip-dev \
&& docker-php-ext-install -j$(nproc) \
intl \
pdo_pgsql \
zip \
&& pecl install \
apcu-${APCU_VERSION} \
&& pecl clear-cache \
&& docker-php-ext-enable --ini-name 20-apcu.ini apcu \
&& docker-php-ext-enable --ini-name 05-opcache.ini opcache \
&& runDeps="$( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)" \
&& apk add --no-cache --virtual .api-phpexts-rundeps $runDeps \
&& 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 [ "$XDEBUG" != "false" ] ;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 "symfony/flex" --prefer-dist --no-progress --no-suggest --classmap-authoritative \
&& composer clear-cache
ENV PATH="${PATH}:/root/.composer/vendor/bin"
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 '<?php return [];' > .env.local.php
RUN composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress --no-suggest \
&& 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 ["docker-entrypoint"]
CMD ["php-fpm"]
【问题讨论】:
-
你能分享一下 Dockerfile 吗?你暴露了什么端口?同时分享你的运行命令。
-
看起来你的 docker 没有相互链接。如果是,您可以使用 API 容器的名称作为 url。
localhost:8080无法工作,因为 api 没有在当前容器中运行。 -
我看到你正在使用
docker-compose。除了Dockerfile之外,您能否添加您的docker-compose.yml? -
在编辑中添加了 2 个文件。谢谢。
-
您在 Dockerfile 中公开了端口 9000,但尝试在 Compose 中映射到端口 80。