Docker Community Forums

Share and learn in the Docker community.

Suggestion for running my app in multiple containers


(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
    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 .


(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

** - 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


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'

    build: ./web
      - new

    build: ./worker
      - legacy

    image: mysql
          - database
          - mysql


(bk) #7

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

version: "3"
    image: client:latest
    command: "/app/ ${hostname}"
      replicas: 3
        condition: on-failure
      - "8999:8999"
      - webnet
    image: service:latest
      replicas: 1
        condition: on-failure
      - "38080:8080"

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
      - 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 .