0

I have tested different things like psql container ip, 127.0.0.1, different docker-compose.yml configurations from sites where solutions are suggested, but none works. :( docker-compose.yml -


services:
  web:
    build: .
    ports:
      - "4200:4200"
    stdin_open: true
    tty: true
    environment:
      - DATABASE_URL=postgresql://postgres:12345@db/vex
    depends_on:
      - database
  database:
    image: postgres
    container_name: database
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: 12345
      POSTGRES_DB: axle
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
    ports:
      - '5432:5432'
  apache:
    image: "httpd:latest"
    volumes:
      - ./httpd.conf:/etc/httpd/conf
volumes:
  db:
    driver: local

Dockerfile -


RUN apt-get update && apt-get upgrade -y && apt-get install -y curl && apt-get install -y git && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql


# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY /vex /var/www/html/vex
COPY /ember-cli-inject-live-reload /var/www/html/vex/frontend/node_modules

WORKDIR "/var/www/html/vex/backend"

RUN composer install -n
RUN php artisan session:table
RUN php artisan queue:table
RUN php artisan migrate

laravel db config -

                'host'     => 'database',
                'database' => 'vex',
                'username' => 'postgres',
                'password' => '12345',
                'charset'  => 'utf8',
                'port'     => '5432'

And I am getting this error - SQLSTATE[08006] [7] could not translate host name "database" to address: Name or service not known

Phil
  • 157,677
  • 23
  • 242
  • 245
Ramix
  • 41
  • 8
  • What command are you using to start your app? – Phil May 17 '22 at 01:30
  • I start postgres with - **docker-compose up -d database** I try to run web service with - **docker-compose up --build** – Ramix May 17 '22 at 01:32
  • Why start just the database first? Why not just `docker compose up` to start the whole stack? – Phil May 17 '22 at 01:33
  • When I use that, it just pulls the apache, database and starts to build web service. And the apache and database has not even started. – Ramix May 17 '22 at 01:39
  • That's right. It needs to build your `web` image before it can run it. The dependency on `database` means `web` won't start until the `database` service is up – Phil May 17 '22 at 01:41
  • So for that now I use - **docker-compose up -d database** to start database. And **docker-compose up -d web** to run web service and dockerfile. – Ramix May 17 '22 at 01:41
  • You mean the - depends_on ? – Ramix May 17 '22 at 01:42
  • Ah, you need to move the `artisan` commands out of your build file. Building the image should definitely not require a database connection – Phil May 17 '22 at 01:43
  • Ugh, it something new for me. What else should I do? – Ramix May 17 '22 at 01:44
  • I found a duplicate link that should help – Phil May 17 '22 at 01:45
  • Oh one more thing! When I entered the web container, I was able to run migrations just fine! So maybe this bash thing would do the trick, that you suggested? – Ramix May 17 '22 at 01:47
  • I'd actually go with [this answer](https://stackoverflow.com/a/66516411/283366) since you probably don't want or need to run migrations every time you start the app up, especially with a persistent data volume – Phil May 17 '22 at 01:54
  • When I use this - ** docker exec docker_project-web-1 "cd /var/www/html/vex/backend && php artisan migrate"** It gives me this error - **OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "cd /var/www/html/vex/backend && php artisan migrate": stat cd /var/www/html/vex/backend && php artisan migrate: no such file or directory: unknown** – Ramix May 17 '22 at 02:30
  • You should be able to use `docker compose exec web php artisan migrate`. There's no need to `cd` since you've already set the `WORKDIR` for your container – Phil May 17 '22 at 04:50

0 Answers0