Connect to another container's network stack in Docker Compose - docker

I am able to connect to another container's network stack by running this command:
docker run -it --net=container:<container name> <container image> bash
How something like that can be achieved in Docker Compose?
version: "3.8"
services:
client:
image: ubuntu
networks:
- mynet
attachedclient:
image: ubuntu
networks:
- <???>
networks:
mynet:
What should be added in ??? or somewhere else, so that the attachedclient container would connect to client container's network stack?

simply mynet, containers on the same network can communicate

Related

Docker compose specify container network as host network

What is the docker compose equivalent of running a docker container with network of container === host network ?
docker run --rm -d --network host --name my_nginx nginx
Basically, how to specify the --network host inside docker-compose
Currently, I am running this by just specifying the port mappings from host to docker container, where I use the same port on both
eg :
version: '3.8'
services:
api-server:
build: '.'
ports:
- "8080:8080"
You need to specify the network mode inside your service at docker-compose.yml. Add it like this:
version: '3.8'
services:
network_mode: "host"
api-server:
build: '.'

Connect java application in docker container to rabbitmq

I have a Java application running in a Docker container and rabbitmq in another container.
How can I connect the containers to use rabbitmq in my Java application?
You have to set up a network and attach the running containers to the network.
Then you have to set the connection URL of your app to the name of the rabbitmq's network name in Docker container.
The easiest way is to create docker-compose file because it will create the network and attach the containers automatically.
Create a network
Connect the container
Or
Docker compose file
Example of docker-compose.yml
version: '3.7'
services:
yourapp:
image: image_from_dockerhub_or_local // or use "build: ./myapp_folder_below_this_where_is_the_Dockerfile" to build container from scratch
hostname: myapp
ports:
- 8080:8080
rabbitmq:
image: rabbitmq:3.8.3-management-alpine
hostname: rabbitmq
environment:
RABBITMQ_DEFAULT_USER: user
RABBITMQ_DEFAULT_PASS: pass
ports:
- 5672:5672
- 15672:15672
You can run it with docker-compose up command.
Then in your connection url use host:rabbitmq, port:5672.
Note that you don't have to create a port forward if you don't want to reach rabbitmq from your host machine.

Network mode container in docker-compose

With plain docker I can use network mode container like this:
docker run -d --name container-b --network container:container-a <image>
Can this be achieved using docker-compose?
services:
service-b:
container_name: container-b
network_mode: "container:container-a"
Leads to:
ERROR: Please provide 'network_mode: "bridge"' or 'network_mode: "host"' in your docker-compose.yaml
I have a docker-compose file with version '2.1', but I am able to do this:
services:
service-b:
container_name: container-b
network_mode: service:container-a
This makes all of my container-b traffic flow through container-a
From https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode
Note
This option is ignored when deploying a stack in swarm mode.
network_mode: "host" cannot be mixed with links.
It won't work if your docker-compose "wrapper" uses docker stack deploy under the hood.
You would have to use something like this:
docker network create some-other-network -d bridge --attachable --scope swarm
... where some-other-network is bound to the target container.
And then:
version: "3.9"
services:
my-service:
image: ...
networks:
- some-other-network
...
networks:
some-other-network:
external: true

Docker compose is not finding network from swarm host

I have one server where I create an overlay network with the following command:
docker network create --driver=overlay --attachable caja_gestiones
In server two I want to use my docker compose to deploy all my containers and one of them use the gestiones network and the default network, this is my docker-compose.yml:
version: '3.3'
services:
msgestiones:
image: msgestiones:latest
hostname: msgestiones
container_name: msgestiones
environment:
- perfil=desarrollo
- JAVA_OPTS=-Xmx512M -Xms512M
- TZ=America/Mexico_City
networks:
- marcador
- caja_gestiones
msmovimientos:
image: msmovimientos:latest
hostname: msmovimientos
container_name: msmovimientos
environment:
- perfil=desarrollo
- JAVA_OPTS=-Xmx512M -Xms512M
- TZ=America/Mexico_City
networks:
- marcador
networks:
marcador:
driver: bridge
caja_gestiones:
external:
name: caja_gestiones
When I ran the docker compose it throws an error saying that the network does not exists, but if I run a dummy container using that network, the network appear and the compose works, how can I make the compose use the overlay network without run a dummy container before?
Did you try to deploy it as a stack instead of a compose? You can use the same compose file, but deploy it with docker stack deploy -c composefile.yaml yourstackname?

Conection to external host from docker container

My local enviroment has connection with a machine that has a BDD. When I ping this machine's IP I have a response.
When I sart my container with docker compose, this container doesn't have connection with this machine. If I enter the container with docker exec -it my-container sh and I ping the DBB machine I have no response.
I have docker installed in Windows 10 y my Docker Compose file looks like this:
version: '3.1'
services:
tomcat:
image: 'tomcat:7.0.91-jre8-alpine'
restart: always
volumes:
- ./warfiles:/home
- ./conf:/usr/local/tomcat/conf/Catalina/localhost
- ./context.xml:/usr/local/tomcat/conf/context.xml
- ./ik-report-config:/ik-report-conf
- ./lib/postgresql-9.3-1102-jdbc4.jar:/usr/local/tomcat/lib/postgresql-9.3-1102-jdbc4.jar
ports:
- 8070:8080
- 8000:8000
environment:
- REPORT_ENGINE_HOME=/ik-report-conf
Thank you very much for your help.
You should probably use network_mode: host.
From the Docker Network Guide:
host: For standalone containers, remove network isolation between the
container and the Docker host, and use the host’s networking directly.
What I had to use was network_mode: bridge

Resources