3

My application is set up using Docker Compose. My docker-compose.yml contents;

version: "3.1"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
  db:
    image: mariadb:10.4
    restart: always
    ports:
      - "3307:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    container_name: mariadb
    environment:
      - MYSQL_DATABASE=dbname
      - MYSQL_PASSWORD=dbpass
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_USER=dbuser
  phpapp:
    depends_on:
      - nginx-proxy
      - db
    image: myhub/myrepo
    restart: always
    container_name: phpapp
    expose:
      - 80
      - 443
    environment:
      - APP_ENV=prod
      - APP_SECRET="secret"
      - CORS_ALLOW_ORIGIN="^.*?$$"
      - DATABASE_URL="mysql://dbuser:dbpass@db/dbname"
      - VIRTUAL_HOST=phpapp.com
networks:
  default:
    external:
      name: nginx-proxy

However, when I try to connect to the database in the application I get an error message; SQLSTATE[HY000] [2002] No such file or directory.

I can, however, connect on port 3307 on my localhost with the correct username and password. Also, when I access the container using docker exec -ti phpapp /bin/bash and run env I get all the environment variables listed and they are correct.

When I execute docker network inspect nginx-proxy I get;

[
  {
    "Name": "nginx-proxy",
    "Id": "2529933d7e38cfba34e0e876e43a96fc4c8d7321c1e89048319ba804b00e1026",
    "Created": "2018-12-12T05:04:01.9044879Z",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
      "Driver": "default",
      "Options": {},
      "Config": [
      {
        "Subnet": "172.18.0.0/16",
        "Gateway": "172.18.0.1"
      }
      ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
      "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {},
    "Options": {},
    "Labels": {}
  }
]

The containers array is empty, however a default (external) network is set in the docker-compose.yml.

2
  • What port does your php app use to connect to the db? 3306? Make sure that your containers are really in the same docker network: "docker network inspect nginx-proxy" Commented Dec 12, 2018 at 8:06
  • I've updated my question with the result for that command, thank you. Commented Dec 12, 2018 at 9:41

2 Answers 2

2

Welcome to stackoverflow :)

I think maybe it is not working for db container not linked from phpapp container. Can you try like this?

version: "3.1"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
  db:
    image: mariadb:10.4
    restart: always
    ports:
      - "3307:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    container_name: mariadb
    environment:
      - MYSQL_DATABASE=dbname
      - MYSQL_PASSWORD=dbpass
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_USER=dbuser
  phpapp:
    depends_on:
      - nginx-proxy
    links:
      - db
    image: myhub/myrepo
    restart: always
    container_name: phpapp
    expose:
      - 80
      - 443
    environment:
      - APP_ENV=prod
      - APP_SECRET="secret"
      - CORS_ALLOW_ORIGIN="^.*?$$"
      - DATABASE_URL="mysql://dbuser:dbpass@db/dbname"
      - VIRTUAL_HOST=phpapp.com
networks:
  default:
    external:
      name: nginx-proxy

I moved db from depends_on to links. You must use db for hostname in database connection now.

I hope i could help.

Sign up to request clarification or add additional context in comments.

3 Comments

Please note that links does not work in version 3, therefore it will not work in the case of the OP and will eventually be deprecated, see: docs.docker.com/network/links
Also, I guess in compose services can reach out to each other by using their names itself. No need to provide links option.
Thank you and thanks for your suggested solution. However, like Sven and vivekyad4v pointed out; version 3 supports using the name of a container to link services.
0

Well, I've found the solution to this problem. However, it's not the one I expected. The database URL is defined as:

DATABASE_URL="mysql://dbuser:dbpass@db/dbname"

It should be without quotes, like this:

DATABASE_URL=mysql://dbuser:dbpass@db/dbname

Now everything seems to work, does anyone know when and why you can and can't use quotes?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.