无法使用IP访问Docker数据库(仅本地主机)

我正在尝试设置Docker以允许来自我networking上其他设备的连接。 目前访问Docker我访问我的电脑上的localhost 。 我试图连接使用我的电脑的本地IP( 192.168.0.140 ),这让我看到我的文件,但没有连接到我的数据库。

我认为这是我的configuration有问题,但我不知道Docker排除故障。

 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' 

任何帮助将不胜感激。

您能够连接到其他容器的原因是因为您已映射容器和主机之间的端口。 对于使用短端口语法的数据库:

 ports: - 3306 

这将在主机上select一个随机的端口。 为了能够连接到数据库,使用长格式的语法:

 ports: - '3306:3306' 

在这种情况下,您将能够连接到localhost:3306上的数据库

也许你应该这样做:

 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.