1

I used expose in docker file and open the port.

However still problem occurs though, @BMitch's answer ports is also the necessary solution in this case.

For now, My first problem is solved. thank you very much.


Latest update.

I logined manager and check services docker service ls and containers docker container ls.

then found out

service port is open and container port is not open.

When running container manually you can set like -p 8000:8000.

However in this case, container is launched by stack config yml, ports works for service but not for container.

So,How can I open the port of container??

docker service ls

t402qi5kqifz        django_python           global              1/1                 registry:5000/myapp/djangosrc:latest   *:8000->8000/tcp
yadtkm6nhwdc        phpmyadmin_phpmyadmin   global              1/1                 phpmyadmin/phpmyadmin:latest             *:8009->80/tcp
3mvzc1rjc8pg        visualizer_app          global              1/1                 dockersamples/visualizer:latest          *:9009->8080/tcp

docker container ls

CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS                 PORTS               NAMES
1d2b50730959        registry:5000/myapp/djangosrc:latest   "uwsgi --socket :800…"   2 minutes ago       Up 2 minutes                               django_python.ne4qcvhmmtchn3popj26plcmz.20txv4hixixgsvwzw23jmhqln
fd5d42cd5fd6        dockersamples/visualizer:latest          "npm start"              2 hours ago         Up 2 hours (healthy)   8080/tcp            visualizer_app.ne4qcvhmmtchn3popj26plcmz.o60n8gzbxod6p6rmznvpls4z6
fd28f1efd77d        phpmyadmin/phpmyadmin:latest             "/docker-entrypoint.…"   2 hours ago         Up 2 hours             80/tcp              phpmyadmin_phpmyadmin.ne4qcvhmmtchn3popj26plcmz.v1flhjuez68lmhb9erlsoz8wc

I have some services in docker-swarm.

if I put service as global, it can be accessed simply

localhost:8000

Howeber I want to access the service in each node from outside.

I need the basic practice for this case.

What I have done is like this below

1) I made the overlay network

docker container exec -it man docker network create --driver=overlay --attachable djangonet

2) open manager node port 8000:8000

version: "3"
services:
  registry:
    container_name: reg
    image: registry:2.6
    ports:
      - 5000:5000
    volumes:
      - "./registry-data:/var/lib/registry"
  manager:
    container_name: man
    image: docker:18.05.0-ce-dind
    privileged: true
    ports:
      - 8000:8000 ## here
      - 8082:8082
      - 9009:9009
    depends_on:
      - registry
    expose:
      - 3375
    command: "--insecure-registry registry:5000"
    volumes:
      - "./stack:/stack"
  node01:
    container_name: node01
    image: docker:18.05.0-ce-dind
    privileged: true
    tty: true
    depends_on:
      - manager
      - registry
    expose:
      - 7946
      - 7946/udp
      - 4789/udp
    command: "--insecure-registry registry:5000"
  node02:
    container_name: node02
    image: docker:18.05.0-ce-dind
    privileged: true
    tty: true
    depends_on:
      - manager
      - registry
    expose:
      - 7946
      - 7946/udp
      - 4789/udp
    command: "--insecure-registry registry:5000"

3) set service_port 8000 in services and set networks external.

services:
  python:
    image: registry:5000/myapp/djangosrc:latest
    environment:
      SERVICE_PORTS: 8000
    networks:
      - djangonet
    deploy:
      replicas: 1
      placement:
        constraints: [node.role != manager]
networks:
  djangonet:
    external: true

However still not work.

Where should I set more??


I am trying some staffs but in vain. So I change the way for test purpose.

Put my services as global on the manager in the most simple way.

In the same way I can access phpmyadmin and visualizer.

But even in this case I can't access to localhost:8000

I am afraid there is something blocking the ports in myapp/djangosrc??

It might be happen??

w7g3h9poppb4        django_python           global              1/1                 registry:5000/myapp/djangosrc:latest   *:8000->8000/tcp
yadtkm6nhwdc        phpmyadmin_phpmyadmin   global              1/1                 phpmyadmin/phpmyadmin:latest             *:8009->80/tcp
3mvzc1rjc8pg        visualizer_app          global              1/1                 dockersamples/visualizer:latest          *:9009->8080/tcp



version: '3'
services:
  python:
    image: registry:5000/myapp/djangosrc:latest
    #command: uwsgi --socket :8001 --module myapp.wsgi --py-autoreload 1 --logto /tmp/mylog.log

    ports:
      - 8000:8000
    deploy:
      mode: global
      placement:
        constraints: [node.role == manager]
2
  • Do you need docker running inside of docker, rather than configuring the parent host to enable swarm?
    – BMitch
    Commented Mar 21, 2020 at 13:07
  • This is just the simulation node.
    – whitebear
    Commented Mar 21, 2020 at 17:08

1 Answer 1

1

I'm presuming you deployed the second compose file inside of the man container using docker stack deploy, and that this file has a version 3 or higher.

To access a port of any application running inside of docker from outside, you need to publish that port:

version: '3'
services:
  python:
    image: registry:5000/myapp/djangosrc:latest
    environment:
      SERVICE_PORTS: 8000
    # include the below ports section to publish a port
    ports:
      - 8000:8000
    networks:
      - djangonet
    deploy:
      replicas: 1
      placement:
        constraints: [node.role != manager]
networks:
  djangonet:
    external: true

Note that it is not necessary to use an external or attachable network with swarm to access the port. Attachable allows you to run other containers outside of swarm mode on the same network, and external gives a predictable network name without the stack name prefix.

2
  • THank you verymuch I am using ports I think the situation is progress... but the problem still occurs...
    – whitebear
    Commented Mar 21, 2020 at 17:14
  • This is not working. The app is still not reachable via port 8000.
    – Soerendip
    Commented Aug 12, 2021 at 4:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .