1

I'm on a Mac OS X, and have a docker-compose.yml

version: '3'
services:
  portalmodules:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8010:8000
    links:
      - database
  database:
    image: postgres:11.2
    expose:
      - "5432"
    environment:
      - "POSTGRES_PASSWORD=12345"
      - "POSTGRES_USER=john"
      - "POSTGRES_DB=api"

I also have a Dockerfile

FROM composer:1.8.5 as build_stage

COPY . /src
WORKDIR /src
RUN composer install


FROM alpine:3.8
RUN apk --no-cache add \
php7 \
php7-mbstring \
php7-session \
php7-openssl \
php7-tokenizer \
php7-json \
php7-pdo \
php7-pdo_pgsql \
php7-pgsql
COPY --from=build_stage /src  /src
RUN ls -al
RUN set -x \
addgroup -g 82 -S www-data \
adduser -u 82 -D -S -G www-data www-data
WORKDIR /src
RUN ls -al
RUN chmod -R 777 storage
CMD php artisan serve --host=0.0.0.0

The container seems to build and start successfully when I ran docker-compose up.

But when I tried to connect to the database:

  • IP: localhost
  • Port: 5432
  • UN : john
  • PW : 12345

I kept getting

How would one go about and debug this further?

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604

1 Answers1

4

By specifying a port in the expose configuration, you're only opening that port to other Docker containers in the Docker network. It will not be open to connect to from your host machine (OS X).

You want to add a ports configuration, which allows you to map host machine ports to the container.

database:
  image: postgres:11.2
  expose:
   - "5432"
  ports:
    - "5432:5432"
  environment:
    - "POSTGRES_PASSWORD=12345"
    - "POSTGRES_USER=john"
    - "POSTGRES_DB=api"

More information in this helpful StackOverflow Q&A.

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40