1

I'm trying to setup Docker to allow connections from other devices on my network. Currently to access Docker I visit localhost on my computer. I'm trying to connect using my computer's local IP (192.168.0.140), which lets me see my files but not connect to my database.

I assume this is a problem with my configuration but I don't know enough about Docker to troubleshoot it.

version: '2'

services:
  webserver:
    build: ./docker/webserver
    image: localdev
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - ./www:/var/www/html
    links:
      - db

  db:
    image: mysql:5.6
    ports:
      - 3306
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secret

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: secret
    ports:
      - '8080:80'

Any help would be greatly appreciated.

2 Answers 2

1

Maybe you should try like this :

version: '2'

services:
  webserver:
    build: ./docker/webserver
    image: localdev
    ports:
      - '0.0.0.0:80:80'   # Map container port 80 on your "public" ip on port 80, it will be available on the network 
      - '0.0.0.0:443:443' # Same for port 443 
    volumes:
      - ./www:/var/www/html
    links:
      - db

  db:
    image: mysql:5.6
    # ports:
    #  - 3306    # You don't need to map port 3306 because mysql already expose this port for others containers, unless you wan't to access to your mysql directly
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secret

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    #links:
    #  - db # Not necessary, with docker-compose v2 every containers are in the same network and see each other
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_USER: root
      MYSQL_ROOT_PASS
      WORD: secret
    ports:
      - 'localhost:8080:80' # to keep your phpmyadmin only available from localhost.
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure why this was downvoted as there are some valid points. However, I changed the settings and still can't access the database on the IP — only on localhost, which suggests something is still not setup right...
1

The reason you are able to connect to the other containers is because you have mapped the ports between the container and the host. For the database you are using the short port syntax:

ports:
  - 3306

This will choose a random port on the host. To be able to connect to the database, use the long form syntax:

ports:
  - '3306:3306'

In that case, you will be able to connect to the database on localhost:3306

1 Comment

I hoped this might of fixed it but it hasn't made a difference. I still can't access the port from an internal IP. I can access phpMyAdmin from http://192.168.0.104:8080/, and can access the files, but the CMS won't connect to the database unless viewed from localhost.

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.