即使指定了不同的mongo_uri,烧瓶应用程序仍尝试连接到本地主机:27017

时间:2020-09-17 07:18:35

标签: python docker flask docker-compose dockerfile

我创建了一个flask应用程序,该应用程序连接到mongod服务器以获取数据。我在不同的Docker容器中运行mongod和flask。下面分别是我的docker compose文件和flask的docker文件。

error: this function has too many arguments (4/3)
  --> src/mem/allocator/heap.rs:14:1
   |
14 | pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: the lint level is defined here
  --> src/lib.rs:14:9
   |
14 | #![deny(clippy::all)]
   |         ^^^^^^^^^^^
   = note: `#[deny(clippy::too_many_arguments)]` implied by `#[deny(clippy::all)]`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

global_allocator

自此,flask应用程序和mongod将在不同的容器中运行。我在烧瓶配置中添加了以下mongo_uri。

#![deny(clippy::all)]
#![feature(alloc_error_handler)]
#![feature(start)]
#![feature(lang_items)]
#![no_std]

#[global_allocator]
pub static LIST: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();

#[start]
fn _start(c: isize, v: *const *const u8) -> isize {
    3
}

#[alloc_error_handler]
fn oom(_layout: core::alloc::Layout) -> ! {
    panic!()
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[lang = "eh_personality"]
fn eh() {}

但是当我运行flask应用程序时,它仍然尝试连接到localhost:27017来连接数据库。即使我在配置中提到了其他地址。 我收到以下提到的超时错误。

version: "3.3"

services:
  flaskapp:
    build:
      context: .
      dockerfile: DockerFile
    networks:
      - bridge_network
    depends_on:
      - mongodatabase
    ports:
      - 9000:5000
    container_name: compose_flaskapp

  mongodatabase:
    image: mongo
    networks:
      - bridge_network
    ports:
      - 27017:27017
    container_name: compose_mongo
    command: mongod --bind_ip 0.0.0.0
    volumes:
      - mongodbdata:/data/db
     
networks:
  bridge_network:
    driver: bridge

volumes:
  mongodbdata:

编辑: 下面是我的烧瓶代码:

FROM python

ENV PORT=5000

COPY . /flask
WORKDIR /flask

RUN pip install -r requirements.txt

EXPOSE $PORT

WORKDIR /flask/src

ENTRYPOINT [ "flask","run","--host=0.0.0.0" ]

我注意到的另一件事是,当我正常运行以上代码时,mongo uri会打印到控制台,但是当我在docker中运行时,它不会被打印。烧瓶环境设置为开发。这可能是原因吗?

此外,我正在使用dockerignore文件不复制pyc文件。因此,问题似乎与缓存的预编译文件无关。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我认为您的MONGO_URI设置不正确。

尝试以下方法:

MONGO_URI = 'mongodb://mongodatabase:27017/UTA_Enrollment'