在 docker 中连接 mongo 快递服务时出现问题
Problem with connecting mongo express service in docker
我有一个启动 nginx 服务器的 docker 配置。现在我必须 运行 mongo 和 mongo-express 才能连接到我的数据库。当我将这两个的配置放在 docker-compose.yml 中并尝试 docker 组合时,mongo 服务启动但 mongo express 显示此错误
Could not connect to database using connectionString: mongodb://root:pass@mongodb:27017/" mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect
感谢任何帮助,这是我的配置。
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: Dockerfile
image: cryptoterminal
container_name: app
restart: unless-stopped
volumes:
- ./:/var/www
webserver:
build:
context: .
dockerfile: Dockerfile_Nginx
image: nginx
container_name: webserver
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./config/nginx/:/etc/nginx/conf.d/
depends_on:
- app
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=admin123
depends_on:
- mongo
ports:
- "8888:8081"
Docker 文件
FROM php:7.4-fpm-alpine
WORKDIR /var/www
RUN apk update && apk add \
build-base \
vim
RUN addgroup -g 1000 -S www && \
adduser -u 1000 -S www -G www
USER www
COPY --chown=www:www . /var/www
EXPOSE 9000
Dockerfile_Nginx
FROM nginx:alpine
COPY ./config/nginx/app.conf /etc/nginx/conf.d/app.conf
COPY . /var/www
.env 文件
MONGO_DB_URI=mongodb://root:pass@mongodb:27017/crypto-terminal?authSource=admin
app.conf 文件
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
文件夹结构
project_folder
php
config
nginx
app.conf
.env
Dockerfile
Dockerfile_Nginx
docker-compose.yml
index.php
在您的 docker-compose 文件中,您调用 mongo 服务 mongo
。这是您可以在 docker 网络上称呼它的名称。在你的连接字符串中,你说
mongodb://root:pass@mongodb:27017/"
应该是
mongo://root:pass@mongo:27017/"
由于连接字符串是使用 docker-compose 文件中的环境变量构建的,因此需要更改的是
- ME_CONFIG_MONGODB_SERVER=mongodb
到
- ME_CONFIG_MONGODB_SERVER=mongo
enter code here
# Use root/example as user/password credentials
version: '3.1'
services:`enter code here`
mongodb:
image: mongo
restart: always
container_name: mongodb
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
volumes:
- mongodb:/data/db:rw
- mongoconfig:/data/configdb:rw
networks:
- mongodb
mongo-express:
image: mongo-express
restart: always
container_name: mongo-express
ports:
- 8889:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_SERVER: mongodb
depends_on:
- mongodb
links:
- mongodb:mongodb
networks:
- mongodb
volumes:
mongodb:
name: mongodb
driver: local
external: false
mongoconfig:
name: mongoconfig
driver: local
external: false
networks:
mongodb:
name: mongodb
driver: bridge
# docker exec -it mongodb /bin/bash
# mongo -u root -p
# localhost:8889
我有一个启动 nginx 服务器的 docker 配置。现在我必须 运行 mongo 和 mongo-express 才能连接到我的数据库。当我将这两个的配置放在 docker-compose.yml 中并尝试 docker 组合时,mongo 服务启动但 mongo express 显示此错误
Could not connect to database using connectionString: mongodb://root:pass@mongodb:27017/" mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect
感谢任何帮助,这是我的配置。
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: Dockerfile
image: cryptoterminal
container_name: app
restart: unless-stopped
volumes:
- ./:/var/www
webserver:
build:
context: .
dockerfile: Dockerfile_Nginx
image: nginx
container_name: webserver
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./config/nginx/:/etc/nginx/conf.d/
depends_on:
- app
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=admin123
depends_on:
- mongo
ports:
- "8888:8081"
Docker 文件
FROM php:7.4-fpm-alpine
WORKDIR /var/www
RUN apk update && apk add \
build-base \
vim
RUN addgroup -g 1000 -S www && \
adduser -u 1000 -S www -G www
USER www
COPY --chown=www:www . /var/www
EXPOSE 9000
Dockerfile_Nginx
FROM nginx:alpine
COPY ./config/nginx/app.conf /etc/nginx/conf.d/app.conf
COPY . /var/www
.env 文件
MONGO_DB_URI=mongodb://root:pass@mongodb:27017/crypto-terminal?authSource=admin
app.conf 文件
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
文件夹结构
project_folder
php
config
nginx
app.conf
.env
Dockerfile
Dockerfile_Nginx
docker-compose.yml
index.php
在您的 docker-compose 文件中,您调用 mongo 服务 mongo
。这是您可以在 docker 网络上称呼它的名称。在你的连接字符串中,你说
mongodb://root:pass@mongodb:27017/"
应该是
mongo://root:pass@mongo:27017/"
由于连接字符串是使用 docker-compose 文件中的环境变量构建的,因此需要更改的是
- ME_CONFIG_MONGODB_SERVER=mongodb
到
- ME_CONFIG_MONGODB_SERVER=mongo
enter code here
# Use root/example as user/password credentials
version: '3.1'
services:`enter code here`
mongodb:
image: mongo
restart: always
container_name: mongodb
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
volumes:
- mongodb:/data/db:rw
- mongoconfig:/data/configdb:rw
networks:
- mongodb
mongo-express:
image: mongo-express
restart: always
container_name: mongo-express
ports:
- 8889:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_SERVER: mongodb
depends_on:
- mongodb
links:
- mongodb:mongodb
networks:
- mongodb
volumes:
mongodb:
name: mongodb
driver: local
external: false
mongoconfig:
name: mongoconfig
driver: local
external: false
networks:
mongodb:
name: mongodb
driver: bridge
# docker exec -it mongodb /bin/bash
# mongo -u root -p
# localhost:8889