尝试启动 docker 个容器时出错

Error when trying to up docker containers

我已经解决这个问题 2 天了,尝试了很多在互联网上找到的解决方案,但没有人解决我的问题...我很沮丧,我希望有人能帮助我。

我在 Linux,但我对服务器端口、主机等一无所知

这个项目好像使用了API平台,不知道有没有用。

我不知道如何启动我刚收到的这个项目。我在启动容器时遇到问题 (docker-compose up) :

Error thrown while running command "doctrine:schema:update -f". Message: "An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused

Is the server running on host "localhost" (127.0.0.1) and accepting
php_1          |    TCP/IP connections on port 5432?
php_1          | could not connect to server: Address not available
php_1          |    Is the server running on host "localhost" (::1) and accepting
php_1          |    TCP/IP connections on port 5432?"

好像是容器无法和DB容器通信。但是我不知道该怎么做才能解决这个问题。

docker-composer.yml中有几个容器:

    version: '3'

services:
    php:
        build:
            context: ./api
        depends_on:
            - db
        env_file:
            - ./api/.env
        # Comment out these volumes in production
        volumes:
            - ./api:/srv/api:rw,cached
            # If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
            - /srv/api/var/
            - /srv/api/var/cache/
            - /srv/api/var/logs/
            - /srv/api/var/sessions/

    api:
        build:
            context: ./api
            dockerfile: ./Dockerfile.nginx
        depends_on:
            - php
        ports:
            - "8080:80"
        volumes:
            - ./api/public:/srv/api/public:ro

    cache-proxy:
        build:
            context: ./api
            dockerfile: ./Dockerfile.varnish
        depends_on:
            - api
        # Comment out this volume in production
        volumes:
            - ./api/docker/varnish/conf:/etc/varnish:ro
        ports:
            - "8081:80"

    db:
        # In production, you may want to use a managed database service
        image: postgres:9.6-alpine
        environment:
            - POSTGRES_DB=api
            - POSTGRES_USER=api-platform
            # You should definitely change the password in production
            - POSTGRES_PASSWORD=!ChangeMe!
        volumes:
            - db-data:/var/lib/postgresql/data:rw
            # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
            # - ./docker/db/data:/var/lib/postgresql/data:rw
        ports:
            - "5432:5432"

    client:
        # Use a static website hosting service in production
        # See https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#deployment
        build:
            context: ./client
            dockerfile: ./Dockerfile
        env_file:
            - ./client/.env
        volumes:
            - ./client:/usr/src/client:rw,cached
            - /usr/src/client/node_modules
        ports:
            - "80:3000"

    h2-proxy:
        # Don't use this proxy in prod
        build:
            context: ./h2-proxy
            dockerfile: ./Dockerfile
        depends_on:
            - client
            - api
            - cache-proxy
        ports:
            - "443:443"
            - "444:444"
            - "8443:8443"
            - "8444:8444"

volumes:
    db-data: {}

Dockerfile:

    ARG PHP_VERSION=7.2
ARG ALPINE_VERSION=3.7
FROM php:${PHP_VERSION}-fpm-alpine${ALPINE_VERSION}

RUN apk add --no-cache \
        git

ARG APCU_VERSION=5.1.11
RUN set -xe \
    && apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        icu-dev \
        postgresql-dev \
        zlib-dev \
    && docker-php-ext-install -j$(nproc) \
        intl \
        pdo_pgsql \
        zip \
    && pecl install \
        apcu-${APCU_VERSION} \
    && 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/"  " ]") == 0 { next } { print "so:"  }' \
    )" \
    && apk add --no-cache --virtual .api-phpexts-rundeps $runDeps \
    && apk del .build-deps

RUN apk add --no-cache freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev && \
  docker-php-ext-configure gd \
    --with-gd \
    --with-freetype-dir=/usr/include/ \
    --with-png-dir=/usr/include/ \
    --with-jpeg-dir=/usr/include/ && \
  NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \
  docker-php-ext-install -j${NPROC} gd exif && \
  apk del --no-cache freetype-dev libpng-dev libjpeg-turbo-dev

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY docker/php/php.ini /usr/local/etc/php/php.ini

# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative

RUN apk add  --no-cache ffmpeg

WORKDIR /srv/api

# Prevent Symfony Flex from generating a project ID at build time
ARG SYMFONY_SKIP_REGISTRATION=1

# Prevent the reinstallation of vendors at every changes in the source code
COPY composer.json composer.lock ./
RUN composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress --no-suggest \
    && composer clear-cache

COPY . ./

RUN mkdir -p var/cache var/logs var/sessions \
    && composer dump-autoload --classmap-authoritative --no-dev \
    && chown -R www-data var

COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]

.env 文件 :

APP_ENV=dev
APP_SECRET=!ChangeMe!
TRUSTED_PROXIES=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
TRUSTED_HOSTS=localhost,api
DATABASE_URL=pgsql://api-platform:!ChangeMe!@localhost/api
CORS_ALLOW_ORIGIN=^https?://localhost:?[0-9]*$
VARNISH_URL=http://cache-proxy
JWT_PRIVATE_KEY_PATH=config/jwt/private.pem
JWT_PUBLIC_KEY_PATH=config/jwt/public.pem
JWT_PASSPHRASE=symfony
JWT_TOKEN_TTL=null
MAILER_URL=gmail://email:password@localhost?
MAILER_FROM=user@example.com
CLIENT_MAGIC_LINK_URL=https://localhost/login
FACEBOOK_CLIENT_ID=433898047056774
FACEBOOK_CLIENT_SECRET=90ac80e435405a2ffeaab8a242981152
AUTH_API_KEY=123456789
ADMIN_PWD=y$vqd7AMd0A5eetDT00qLd2OkGG8T9UJ1gLsD2huOhk3iRwGCBqR3iu

我不知道你们是否需要更多东西。告诉我你需要什么。

非常感谢...

您必须使用 db 作为数据库主机,而不是 DATABASE_URL 应用程序环境中的 localhost