Docker: 无法连接到其他容器
Docker: Cannot connect to other container
docker-compose.yml
version: '3'
volumes:
wp-assets:
services:
mariadb:
build: ./requirements/mariadb
environment:
- MYSQL_ROOT_HOST=${MYSQL_ROOT_HOST}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "127.0.0.1:3306:3306"
- "127.0.0.1:9999:9999" # test
wordpress:
environment:
- WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_TABLE_PREFIX=${WORDPRESS_TABLE_PREFIX}
- WORDPRESS_AUTH_KEY=${WORDPRESS_AUTH_KEY}
- WORDPRESS_SECURE_AUTH_KEY=${WORDPRESS_SECURE_AUTH_KEY}
- WORDPRESS_LOGGED_IN_KEY=${WORDPRESS_LOGGED_IN_KEY}
- WORDPRESS_NONCE_KEY=${WORDPRESS_NONCE_KEY}
- WORDPRESS_AUTH_SALT=${WORDPRESS_AUTH_SALT}
- WORDPRESS_SECURE_AUTH_SALT=${WORDPRESS_SECURE_AUTH_SALT}
- WORDPRESS_LOGGED_IN_SALT=${WORDPRESS_LOGGED_IN_SALT}
- WORDPRESS_NONCE_SALT=${WORDPRESS_NONCE_SALT}
volumes:
- wp-assets:/var/wp-assets
build: ./requirements/wordpress
ports:
# host_port == 127.0.0.1:9000, allow only localhost
- "127.0.0.1:9000:9000"
nginx:
# image: nginx:latest
depends_on:
- wordpress
volumes:
- wp-assets:/var/wp-assets
build: ./requirements/nginx
ports:
# host_port == 0.0.0.0:8080, allow all interfaces
- "8080:80"
mariadb/Dockerfile
FROM debian:buster
# install mariadb-server
RUN apt update && apt install -y mariadb-server
# allow connection from wordpress (host name)
RUN sed -e 's/127.0.0.1/wordpress/' \
-i '/etc/mysql/mariadb.conf.d/50-server.cnf'
# used for socket
RUN mkdir -p /var/run/mysqld && \
chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && \
chmod 777 /var/run/mysqld && \
touch /var/run/mysqld/mysqld.sock
# init db here
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
#ENTRYPOINT ["/docker-entrypoint.sh"]
ENTRYPOINT ["tail", "-f"]
我试图将 mariadb
连接到 wordpress (mariadb-client)
,但出现错误:
Can't connect to MySQL server on 'mariadb'
所以我测试端口是好的。但是,虽然 nginx:80
或 wordpress:9000
等其他端口可以被其他容器访问,但 mariadb
的端口拒绝连接。
我无法弄清楚 mariadb
容器与其他容器之间的区别。有什么问题吗?
您的 MariaDB Dockerfile 的入口点是
ENTRYPOINT ["tail", "-f"]
所以它实际上并不 运行 MariaDB。
您可能需要将其注释掉并在
中回复
ENTRYPOINT ["/docker-entrypoint.sh"]
我对端口感到困惑。我认为每个容器只能通过 opening
个端口进行通信,但没有 LISTEN
ing 个端口。 Docker-compose ports:
只是绑定端口,与LISTEN
.
无关
docker-compose.yml
version: '3'
volumes:
wp-assets:
services:
mariadb:
build: ./requirements/mariadb
environment:
- MYSQL_ROOT_HOST=${MYSQL_ROOT_HOST}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "127.0.0.1:3306:3306"
- "127.0.0.1:9999:9999" # test
wordpress:
environment:
- WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_TABLE_PREFIX=${WORDPRESS_TABLE_PREFIX}
- WORDPRESS_AUTH_KEY=${WORDPRESS_AUTH_KEY}
- WORDPRESS_SECURE_AUTH_KEY=${WORDPRESS_SECURE_AUTH_KEY}
- WORDPRESS_LOGGED_IN_KEY=${WORDPRESS_LOGGED_IN_KEY}
- WORDPRESS_NONCE_KEY=${WORDPRESS_NONCE_KEY}
- WORDPRESS_AUTH_SALT=${WORDPRESS_AUTH_SALT}
- WORDPRESS_SECURE_AUTH_SALT=${WORDPRESS_SECURE_AUTH_SALT}
- WORDPRESS_LOGGED_IN_SALT=${WORDPRESS_LOGGED_IN_SALT}
- WORDPRESS_NONCE_SALT=${WORDPRESS_NONCE_SALT}
volumes:
- wp-assets:/var/wp-assets
build: ./requirements/wordpress
ports:
# host_port == 127.0.0.1:9000, allow only localhost
- "127.0.0.1:9000:9000"
nginx:
# image: nginx:latest
depends_on:
- wordpress
volumes:
- wp-assets:/var/wp-assets
build: ./requirements/nginx
ports:
# host_port == 0.0.0.0:8080, allow all interfaces
- "8080:80"
mariadb/Dockerfile
FROM debian:buster
# install mariadb-server
RUN apt update && apt install -y mariadb-server
# allow connection from wordpress (host name)
RUN sed -e 's/127.0.0.1/wordpress/' \
-i '/etc/mysql/mariadb.conf.d/50-server.cnf'
# used for socket
RUN mkdir -p /var/run/mysqld && \
chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && \
chmod 777 /var/run/mysqld && \
touch /var/run/mysqld/mysqld.sock
# init db here
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
#ENTRYPOINT ["/docker-entrypoint.sh"]
ENTRYPOINT ["tail", "-f"]
我试图将 mariadb
连接到 wordpress (mariadb-client)
,但出现错误:
Can't connect to MySQL server on 'mariadb'
所以我测试端口是好的。但是,虽然 nginx:80
或 wordpress:9000
等其他端口可以被其他容器访问,但 mariadb
的端口拒绝连接。
我无法弄清楚 mariadb
容器与其他容器之间的区别。有什么问题吗?
您的 MariaDB Dockerfile 的入口点是
ENTRYPOINT ["tail", "-f"]
所以它实际上并不 运行 MariaDB。
您可能需要将其注释掉并在
中回复ENTRYPOINT ["/docker-entrypoint.sh"]
我对端口感到困惑。我认为每个容器只能通过 opening
个端口进行通信,但没有 LISTEN
ing 个端口。 Docker-compose ports:
只是绑定端口,与LISTEN
.