running multiple services on same port in docker-compsoe - docker

We have multiple microservices that are run on port 8080. As far as I know that only 1 service can run on port 8080.
Would it mean running the microservice as a port: 8080:8081 or 8081:8081?
Below are the services that we are trying to implement in docker-compose
reference-service:
image: **
ports:
- "8080:8080"
test-service:
image: **
ports:
- "8080:8081"

There are two kind of ports: container port and host port. Two processes cannot hold the same container port inside one container. You also cannot expose services' container ports to the same host port.
However each service runs inside its own container so that both can use container port 8080.
So that the following configuration is acceptable: you have two services, each is running in its own container and have container port 8080. Each of container ports are exposed to different host ports like this:
reference-service:
image: **
ports:
- "8080:8080"
test-service:
image: **
ports:
- "8081:8080"

Related

Why is that I am able to access container outside the bridge network?

I started mysqldb from a docker container . I was surprised that I could connect it via the localhost using the below command
mysql -uroot -proot -P3306 -h localhost
I thought the docker containers that start on the bridge network and wont be available outside that network. How is that mysql CLI is able to connect to this instance
Below is my docker compose that runs the mysqldb-docker instance
version: '3.8'
services:
mysqldb-docker:
image: 'mysql:8.0.27'
restart: 'unless-stopped'
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_PASSWORD=root
- MYSQL_DATABASE=reco-tracker-dev
volumes:
- mysqldb:/var/lib/mysql
reco-tracker-docker:
image: 'reco-tracker-docker:v1'
ports:
- "8083:8083"
environment:
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
- SPRING_DATASOURCE_URL="jdbc:mysql://mysqldb-docker:3306/reco-tracker-dev"
depends_on: [mysqldb-docker]
env_file:
- ./.env
volumes:
mysqldb:
You have published the port(s). That means you can reach them on the host system on the published port.
By default, when you create or run a container using docker create or docker run, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.
The critical section in your config is the below. You have added a ports key to your service. This is composes way to publish ports. The left part is the port where you publish it to on the host system. The right part is where the container actually listens on.
ports:
- "3306:3306"
Also keep in mind that when you start compose, a default network is created that joins all container in the compose stack. That's why These containers can find each other, with the service name and/or container name as hostname.
You don't need to publish the port(s) like you did in order for them to be able to communicate. I guess that's why you did it. You can and probably should remove any port mapping from internal services, if possible. This will add extra security to your setup, because then it behaves like you describe. Only containers in the same network find each other.

resolve host machine hostname inside docker container

I have one application running on http://home.local:8180 in container A. And the other container B is running on http://data.local:9010. Container B is using container A to hit the API. If I specify container A hostname as http://host.docker.internal:8180 in container B then it works. What I would have to do if I want to use the hostname as is (home.local:8180)
Following is the docker-compose file:
home_app:
hostname: "home.local"
image: "home-app"
ports:
- "8180:8080"
environment:
data_app:
hostname: "data.local"
image: "data-app"
links:
- "home_app"
ports:
- "9010:9010"
Just use "home.local:8080". 8180 is just on the host machine and forwards to 8080 on the container, whereas based on your docker-compose, 8080 is the port of your application on home_app container, so within the docker-compose network, other containers should be able to access it via hostname (home.local) and the actual ports (8080).
You need to configure your application to use the Compose service name home_app as a host name, and the port number that the process inside the container is using. Neither hostname: nor ports: has any effect on connections between containers. You don't need to (and can't) specify a custom DNS suffix. See Networking in Compose in the Docker documentation for additional details.
So I might specify:
version: '3.8'
services:
home_app:
image: "home-app"
ports:
- "8180:8080" # optional, only for access from outside Docker
data_app:
image: "data-app"
ports:
- "9010:9010"
environment:
HOME_APP_URL: 'http://home_app:8080'
You don't need hostname:, which only affects what a container thinks its own hostname is and has no effect on anything outside the container; and you don't need links:, which is an obsolete option from first-generation Docker networking.

Docker Per Network Port Mapping

I'm looking for a way to map the same port into 2 different ports, each for another container in a different network.
consider the below docker-compose scenario:
services:
web:
build: .
ports:
- "8080:8080"
networks:
Net1:
Net2:
serv1:
image: tomcat:7.0.92-jre8
networks:
Net1:
serv2:
image: tomcat:7.0.92-jre8
networks:
Net2:
Now what I would really like to do is to actually map the "web" service port 8080 so that serv1 could consume it as 8081 and serv2 will be using it as 8082.
Is that even possible?
Thanks
Ports are published to the host, not to docker networks, and not to other docker containers. So the above "8080:8080" maps port 8080 on the docker host into that container's port 8080.
For container-to-container communication, that happens using docker's internal DNS for service discovery, and the container port. So both serv1 and serv2 can connect to http://web:8080 to reach the web service on its container port. That in no way prevents serv1 and serv2 from listening within their own container on any ports they wish.

Is it possible deploy multiple container on single host with different sub domain and same listening port(80)?

I have deployed 20 ASP.Net containers on Single Windows Server 2016. Here I have point containers with different ports in Same IP working fine. I need to expose on port 80 all the sites in containers to separate subdomains (eg: site1.serveraction.com, site2.serveraction.com)
But only one container working another one is going down.
Here my Docker compose file:
version: '3'
services:
site1:
image: "raj/con:site1"
environment:
- HOST_NAME=13.92.51.1XX
- VIRTUAL_HOST=site1.serveractions.com
ports:
- "80:80"
version: '3'
services:
site2:
image: "raj/con:site2"
environment:
- HOST_NAME=13.92.51.1XX
- VIRTUAL_HOST=site2.serveractions.com
ports:
- "80:80"
It is working as expected. One host port(80 in this case) can only be used by one container because there is only one port 80. The solution in your case would be to map different ports of the host to port 80 in your containers, i.e.
For container 1 --> Use port say 72801 to port 80, i.e. -p 72801:80
For container 2 --> Use port say 72802 to port 80, i.e. -p 72802:80
.
.
For container 20 --> Use port 72820 to port 80, i.e -p 72820:80
Now any service accessing container 1 has to go (address):72801 which would be forwarded to container 1's port 80, this would make sure you don't have any code related changes but the way(or ports) for accessing container's service will change.

Docker inter container communication not working

Hi I am new to docker I have created docker images and able to start them using docker compose.
Able to access these services from browser using docker tcp IP and they can ping each other using ping command.
When I tried to access the service from one another using service name in docker compose it is not accessible.
Is it a firewall issue?But both the services can be accessible from browser.
Tried by creating network when I try to inspect network both containers are in same network and they can ping each other.
These are my docker files
backendservice
FROM java:8
EXPOSE 8080
ADD /target/microService.jar microService.jar
ENTRYPOINT ["java","-jar","microService.jar"]
uiservice
FROM java:8
EXPOSE 8081
ADD /target/csuiservice.war csuiservice.war
ENTRYPOINT ["java","-jar","csuiservice.war"]
Using spring boot to develop above services and they able to access independently on exposed ports
docker-compose.yml
version: '3'
services:
backendservice:
build:
./BAService
volumes:
- ./BAService:/usr/src/app
ports:
- 5001:8080
website:
image: uiservice
ports:
- 5000:8081
links:
- "backendservice:backendservice"
volumes:
- ./spring-boot-web-jsp:/usr/src/app1
depends_on:
- backendservice
networks:
default:
external:
name: mynetwork
I am trying to access the backendservice by following url
"http://backendservice:8080/getUsers"

Resources