我在一个Docker容器中使用PHP/Symfony构建了一个api。我想测试一下。
要做到这一点:
在我的测试中,我有一个请求:
$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:连接到本地主机端口8080失败:连接被拒绝(参见https://curl.haxx.se/libcurl/c/libcurl-errors.html)
那么如何在容器内进行这个测试呢?
编辑
我的船坞-写作
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"]发布于 2020-01-08 17:12:47
您不能在PHP容器中直接使用本地主机--它不提供HTTP,它是php。
您必须调用nginx容器da-invoicing api。
$result = $this->client->request(
'POST',
'da-invoicing-api/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,
]),
]
);Docker-为撰写文件中的每个容器编写创建主机别名 (同一网络上的其他容器可以使用服务名称)。
https://stackoverflow.com/questions/59649452
复制相似问题