Docker Community Forums

Share and learn in the Docker community.

Suggestion for running my app in multiple containers

swarm

(bk) #1

Hello ,

I have been going through Docker compose which will help in scaling multiple containers . But I have not taken a deep dive into the concepts of it . So at first , I started my Docker journey with creating two containers one as Client ( website) and other as the service . Now when I try to scale it using Docker compose there are some questions in my mind.

  • How to make the services communicate to each other
    https://docs.docker.com/compose/gettingstarted/#step-5-edit-the-compose-file-to-add-a-bind-mount
    In the above example the web service is able to talk to redis . So in my scenerio I have two containers which are exposing different ports and I want the web to access the service through the exposed ports . How can I achieve this ?

  • How should I scale the Application . Should I need to scale both client and service or just scale client ? (

Pardon me if the questions are too basic . Appreciate your help .

Thanks


(Sam) #2

to learn how this works, stop using compose until you understand how the parts fit together…

and the parts fit together just like always… think of container as ‘system’.

system A runs app X
and system B runs app Y

X needs access to Y.

how do you configure that?

now… on the scaling question…

again, think in terms of normal systems…
as the demand increases, what part of your application SYSTEM needs to adjust 1st/more than the other?

once you understand the answers to these questions, and have tried to do it manually, then you can understand how compose simplifies the work effort…


(bk) #3

Thanks for your response . I have already achieved in client-server communication . Just hosted client and server in two different containers and the client was able to access the server container through the hostname:port it has exposed. In the Docker compose file there is

links:
** - client**

As you said , manually the client container is able to connect to server container through port . If I use Docker compose what significance does the above step has. I think the app might work without having it mentioned , right ?


(Sam) #4

were you using linking between the two containers? in compose I think you would use a logical network, and ports.

note that there have been quite a few posts where the container name resolution has not been working all the time…


(bk) #5

Hmmm … No . The server hosted in server container is mapped to 38080 port of the hosts. The client app is able to access the server using that . hostname:38080


(Sam) #6

see https://docs.docker.com/compose/compose-file/#aliases

here is an example compose file from that link (it has two networks)
I think you will want to use expose on the service (server) side so that the client can access it.
(it does NOT need to be mapped to a host port)

version: '2'

services:
  web:
    build: ./web
    networks:
      - new

  worker:
    build: ./worker
    networks:
      - legacy

  db:
    image: mysql
    networks:
      new:
        aliases:
          - database
      legacy:
        aliases:
          - mysql

networks:
  new:
  legacy:

(bk) #7

I was able to create a stack with web and service .

version: "3"
services:
  web:        
    image: client:latest
    command: "/app/startserver.sh ${hostname}"
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
    ports:
      - "8999:8999"
    networks:
      - webnet
  backend:        
    image: service:latest
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
    ports:
      - "38080:8080"
networks:
  webnet:

App is working fine . But I have some things to be clarified

  1. What is the difference between running Docker stack deploy -c docker-compose.yml servicename and docker-compose.yml up ?
  2. If I increase the replicas of backend service and add
  networks:
      - webnet

like its mentioned for web . So how will this work . If replica is set as 2 then will there be a common port 38080 which will load balance between the two ports ? I am trying to create 3 containers for web and 2 for backend and my understanding is 3 are mapped under 8999 and 2 backend containers will be mapped to 38080 . Please correct me if my understanding is wrong.


(Sam) #8

I think this might help


(bk) #9

Thanks man . I have udpated my response . will be glad if you can help me to understand that . thanks again.


(Sam) #10

i think that the host port will have to be changed if multiple service containers are executed on the same host (as there is only one host port 38080…) I think docker inspect of the containers running will show the exact mapping.

now, if there is a unique host per service container, then the specified port would work. but this seems very heavy…

I do not ‘know’ how swarm solves this problem.


(bk) #11

Hi can you help on this .