1

I'm struggling with the docker setup on localhost. When calling a PHP function file_get_contents() with a local domain, I'm getting three warnings:

1. SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
2. Failed to enable crypto 
3. failed to open stream: Cannot assign requested address

Originally I have a relatively complex setup with nginx as reverse proxy, selfsigned SSL certs, mariadb, wordpress, wpcli, phpmyadmin, mailhog and redis. For simplicity, I'm pasting a simple docker-compose file. On this setup, I'm getting: file_get_contents() failed to open stream: Cannot assign requested address

version: '3.6'
services:

  mysql:
    container_name: docker-test-mysql
    image: mariadb:latest   
    volumes:
      - ./db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_DATABASE: docker-test
    restart: always
    ports:
      - 3306:3306
    networks:
      - webnet    

  wordpress:
    container_name: docker-test-wordpress
    image: wordpress:latest  
    ports:
      - 8000:80
    volumes:
      - wp_data:/var/www/html:rw,cached
      - ./wordpress:/var/www/html:rw,cached
    depends_on:
      - mysql
    restart: always
    environment:
      WORDPRESS_DB_NAME: docker-test
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: root
      WORDPRESS_DEBUG: 1
    networks:
      - webnet

networks:
  webnet:
    external: true
    driver: bridge

volumes:
  db_data: {}
  wp_data: {}

The function works when calling with an external domain, but fails only with local domains (https://my-local-domain.local or http://localhost:8000).

What am I missing? Any help is appreciated!

Best regards

Marin
  • 11
  • 3
  • 1
    Because docker has it's own _localhost_ defined than what _you_ know as localhost. E.g. on Mac it would be `http://docker.for.mac.localhost:8000` – Justinas Apr 08 '22 at 09:52
  • Thanks Justinas! It's working but I would also like to make this work with the actual domain name. What would be a correct approach when using nginx as a reverse proxy with local domain (ex. https://mydomain.local)? https://docker.for.mac.mydomain.local doesn't work... – Marin Apr 08 '22 at 11:45
  • `docker.for.mac.mydomain.local` does not work, because there is no such domain registered. It's literally `docker.for.mac.localhost`. You should check how to add domain aliases to nginx – Justinas Apr 08 '22 at 13:12

1 Answers1

1

That's how docker works. Inside a container localhost is itself. You're mapping port 80 of the container on port 8000 of your local machine.

But inside the same container the correct url is simply localhost or the name of the container inside your docker-compose file (in this case wordpress) because docker compose create also a network and uses the alias of your service inside the network to find them.

So you can use file_get_contents('http://localhost/...') or file_get_contents('http://wordpress/...')

You can also add extra host in the docker compose file if you want to use another alias

R4ncid
  • 6,944
  • 1
  • 4
  • 18