3

I have a similar issue to Run MySQL on Port 3307 Using Docker Compose but either I can't see the wood for the trees or the solution here is not working.

I have the following docker-compose.yml file:

version: '3'
services:
    db:
        image: mysql:5.7
        container_name: squirrels_db
        volumes:
          - db_data:/var/lib/docker/volumes/squirrels_db_data/_data
        restart: always
        ports:
          # <Port exposed> : <MySQL Port running inside container>
          - 3310:3306
        environment:
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: wordpress
            MYSQL_USER: mydb_user
            MYSQL_PASSWORD: password
        volumes:
          - ./var/lib/docker/volumes/squirrels_db_data/_data
        networks:
          internal-net:
            ipv4_address: 172.29.0.11

    wordpress:
        image: wordpress:latest
        container_name: squirrels_web
        depends_on:
            - db
        ports:
           - 8000:80
        restart: always
        environment:
            WORDPRESS_DB_HOST: db:3306
            WORDPRESS_DB_USER: mydb_user
            WORDPRESS_DB_NAME: mydb_name
            WORDPRESS_DB_PASSWORD: password

        volumes:
          - ./data/wp_content:/var/www/html/wp-content
          - ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
        networks: 
          nginx-proxy:
          internal-net:
            ipv4_address: 172.29.0.12      

# Names our volume
volumes:
  db:

networks:
  nginx-proxy:
    external:
      name: nginx-proxy
  internal-net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.29.0.0/16

Note: I have changed usernames and passwords here and will eventually have them in a .env file

When I hit http://localhost:8000 I am seeing a WordPress delivered "Error establishing a database connection" message and the following in the log (from docker-compose logs):

PHP Warning: mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22 MySQL Connection Error: (2002) Connection refused

This now officially driving me nuts, not helped at all by just knowing I am missing something obvious! So, any observations or suggestions gratefully received

Thanks

ROOT
  • 11,363
  • 5
  • 30
  • 45
ssherlock
  • 89
  • 1
  • 8
  • I should add that I can manually connect to mysql from the command line using: mysql -u mydb_user -p -h 172.29.0.11 – ssherlock Feb 23 '20 at 11:19
  • I'd suspect `WORDPRESS_DB_HOST: db:3306` line to be an issue as it includes `:` twice. Would you be able to state the port in a different line? – Nae Feb 23 '20 at 12:03
  • 1
    I got that line from this article https://docs.docker.com/compose/wordpress/ – ssherlock Feb 23 '20 at 12:31

1 Answers1

1

I managed to get it working. Here's the working file – I'll explain my changes below:

version: "3"
services:
  db:
    image: mysql:5.7
    container_name: squirrels_db
    volumes:
      # You prepended with db_data: but volume was called db – scroll down to volumes to see my fix
      - db_data:/var/lib/docker/volumes/squirrels_db_data/_data
    restart: always
    ports:
      # <Port exposed> : <MySQL Port running inside container>
      - 3310:3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      # This is likely the isse - you called it wordpress but tried to connect to mydb_name in the wordpress container
      MYSQL_DATABASE: mydb_name
      MYSQL_USER: mydb_user
      MYSQL_PASSWORD: password
    # REMOVE BELOW 2 LINES - you declared above
    # volumes:
    #   - ./var/lib/docker/volumes/squirrels_db_data/_data
    networks:
      internal-net:
        ipv4_address: 172.29.0.11

  wordpress:
    image: wordpress:latest
    container_name: squirrels_web
    depends_on:
      - db
    ports:
      - 8000:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: db # db:3306 is fine but that's default so removed
      WORDPRESS_DB_USER: mydb_user
      WORDPRESS_DB_NAME: mydb_name
      WORDPRESS_DB_PASSWORD: password
    volumes:
      - ./data/wp_content:/var/www/html/wp-content
      - ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    networks:
      nginx-proxy:
      internal-net:
        ipv4_address: 172.29.0.12

# Names our volume
volumes:
  # FIX: renamed from db to db_data. Added {} to declare an empty volume
  db_data: {}

networks:
  nginx-proxy:
    external:
      name: nginx-proxy
  internal-net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.29.0.0/16

Changes

  1. I think your indentation was fine - but I changed this as a first test

  2. You had 2 volumes keys declared in your db container. the first was seemingly correct, the second was missing the db_data: volume prepending the path.

  3. Your volume was named db but was being used (as in point 2) as db_data

  4. Your database names didn't match. You called the db wordpress on setup at MYSQL_DATABASE: wordpress then tried to connect to mydb_name at WORDPRESS_DB_NAME: mydb_name

Simpler solution

Although the above works, do you definitely need the networking? The below will also work and is much simpler:

version: "3"
services:
  db:
    image: mysql:5.7
    container_name: squirrels_db
    volumes:
      - db_data:/var/lib/docker/volumes/squirrels_db_data/_data
    restart: always
    ports:
      - 3310:3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mydb_name
      MYSQL_USER: mydb_user
      MYSQL_PASSWORD: password

  wordpress:
    image: wordpress:latest
    container_name: squirrels_web
    depends_on:
      - db
    ports:
      - 8000:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: mydb_user
      WORDPRESS_DB_NAME: mydb_name
      WORDPRESS_DB_PASSWORD: password
    volumes:
      - ./data/wp_content:/var/www/html/wp-content
      - ./config/wordpress/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini

volumes:
  db_data: {}
Andy Mardell
  • 1,132
  • 8
  • 13
  • Brilliant thanks. That now works locally (as long as I remove my nginx-proxy network) but then fails when I try and run it on my server (that uses nginx-proxy). I haven't time now but will do some more playing before reporting back. The reason I had the internal-net network was so I could have a static ip which makes it easier for hitting mysql manually/command line etc. That's not really necessary here so have removed it and works a treat locally. Still has the aforementioned issue with nginx-proxy on the main server though. – ssherlock Feb 23 '20 at 17:52
  • Cool - I guess the nginx-proxy issue could be something else entirely. Do you know you can CLI into the instance by running `docker-compose exec -it [container] [command]`? So in your case, `docker-compose exec -it db bash` would open a bash shell. Don't forget to mark as solved if possible so I get the sweet green tick ;) – Andy Mardell Feb 23 '20 at 18:21
  • 1
    Thanks Andy, marked as solved as I can now run it locally. Still got to figure out the server setup but that is separate to this. Thanks again for your help - it was driving me nuts! :) – ssherlock Feb 24 '20 at 08:46