从Docker堆栈模式访问本地网络主机

时间:2020-08-24 15:47:34

标签: docker docker-compose stack

我需要与192.168...网络主机(本地网络)建立ftp连接,并与mongo容器建立连接。

集群模式下的Docker阻止了network_mode:host(我在容器内看不到远程ftp主机)

Docker堆栈中有关于--publish mode=host,target=80,published=8080的文档,但是我不知道如何在docker-compose文件中编写它。

我的docker-compose.yml文件


version: '3'

services:

  node:
    image: tgbot-test_node_1
    build:
      context: ..
    env_file: .env.test
    network_mode: host
    links:
      - mongo # works
    depends_on:
      - mongo
    deploy:

  mongo:
    image: mongo
    network_mode: "bridge"
    restart: on-failure
    ports:
      - 8080:80 # not works, only expose 27017/tcp
#      not works
#      - mode: host
#        target: 27019
#        published: 27017
    env_file:
      - .env.test
    volumes:
    - db:/data/db
    deploy:
        limits:
          cpus: '0.75'

volumes:
  db:

我需要群体模式来限制资源。

如何访问ftp主机?

  • Docker版本19.03.12,内部版本48a66213fe
  • docker-compose版本1.26.2,构建eefe0d31

UPD 乔尔·马格努森(Joel Magnuson)回答,我得到了PORTS: 27017/tcp的mongo容器。它不会使用stack deploy转发端口,无论是-是“ 80:80”还是“ 27017”

我设置了

    ports:
      - 27018:27017

得到

docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS               NAMES
ab58c781fdb9        mongo:latest               "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        27017/tcp           tgbot-test_mongo.1.3i7yps3saqo3nk4xxyk0eka7h
43c0e3cfe960        tgbot-test_node_1:latest   "docker-entrypoint.s…"   3 seconds ago       Up 3 seconds                            tgbot-test_node.1.v23cufsrr683gdg2bicgf80q2

1 个答案:

答案 0 :(得分:1)

我认为这只是一个配置问题。您提到了“ FTP主机”,但没有提及运行FTP服务器。希望以下内容对您的mongo数据库有所帮助。

mongodb将始终默认在容器内部的端口27017上运行,除非进行配置,因此您必须将容器的27017的端口安装到主机,而不是端口80

version: '3'

services:

  node:
    image: tgbot-test_node_1
    env_file: .env.test       # configure with mongodb://mongo:27017/<db name>
    networks:
      - tgbot-test    

  mongo:
    image: mongo
    ports:
      - 27017:27017   # only needed if you want to access it outside of the stack
                      # otherwise it's always visible within the stack network as 'mongo'
    volumes:
      - /home/$USER/db:/data/db    # can mount to host instead
    networks:
      - tgbot-test

networks:
  tgbot-test:
    driver: overlay     #suggest overlay network
    
#volumes:
#  db:     # this is not persistent by itself - can mount to host

您还可以创建一个外部卷。 docker volume create --name tgbot-db

...
volumes:
  tgbot-db:
    external: true

您应该能够使用mongodb://192.160.X.X:27017/<db name>的DNS名称docker swarm从主机或远程主机mongo连接到mongodb实例,或者在同一堆栈中的容器内连接到mongodb实例(服务名称)和mongodb://mongo:27017/<db name>