Network mode container in docker-compose - docker

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

Related

Connect to another container's network stack in Docker Compose

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

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

docker-compose run with specified network name

I have a docker-compose file with three services (Solr, PostgreSQL and pgAdmin), all sharing a Docker network.
version: '2'
services:
solr:
image: solr:7.7.2
ports:
- '8983:8983'
networks:
primus-dev:
ipv4_address: 10.105.1.101
volumes:
- data:/opt/solr/server/solr/mycores
entrypoint:
- docker-entrypoint.sh
- solr-precreate
- primus
- /opt/solr/server/solr/configsets/sample_techproducts_configs
environment:
- SOLR_HEAP=2048m
logging:
options:
max-size: 5m
db:
image: "postgres:11.5"
container_name: "primus_postgres"
ports:
- "5432:5432"
networks:
primus-dev:
ipv4_address: 10.105.1.102
volumes:
- primus_dbdata:/var/lib/postgres/data
environment:
- POSTGRES_DB=primus75
- POSTGRES_USER=primus
- POSTGRES_PASSWORD=primstav
pgadm4:
image: "dpage/pgadmin4"
networks:
primus-dev:
ipv4_address: 10.105.1.103
ports:
- "3050:80"
volumes:
- /home/nils/docker-home:/var/docker-home
environment:
- PGADMIN_DEFAULT_EMAIL=nils.weinander#kulturit.se
- PGADMIN_DEFAULT_PASSWORD=dev
networks:
primus-dev:
driver: bridge
ipam:
config:
- subnet: 10.105.1.0/24
volumes:
data:
primus_dbdata:
This works just fine after docker-compose up (at least pgAdmin can talk to PostgreSQL).
But, then I have a script (actuall a make target, but that's not the point here), which builds, runs and deletes a container with docker-compose run:
docker-compose run -e HOME=/app -e PYTHONPATH=/app/server -u 0 --rm backend \
bash -c 'cd /app/server && python tools/reindex_mp.py -s -n'
This does not work as the reindex_mp.py cannot reach Solr on 10.105.1.101, as the one shot container is not on the same Docker network. So, is there a way to tell docker-compose to use a named network with docker-compose run? docker run has an option --network but that is not available for docker-compose.
You can create a docker network outside your docker-compose and use that network while running services in docker-compose.
docker network create my-custom-created-network
now inside your docker-compose, use this network like this:
services:
serv1:
image: img
networks:
my-custom-created-network
networks:
my-custom-created-network:
external: true
The network creation example creates a bridge network.
To access containers across hosts, use an overlay network.
You can also use the network created inside docker-compose and connect containers to that network.
Docker creates a default network for docker-compose and services which do not have any network configuration specified, will be using default network created by docker for that compose file.
you can find the network name by executing this command:
docker network ls
Use the network appropriate name while starting a container, like this
docker run [options] --network <network-name> <image-name>
Note: Containers in a same network are accessible using container names, you can leverage this instead of using ips

Docker-Compose, How To Connect Java Application With Custom Docker Network On Redis Container

I have a java application, that connects through external database through custom docker network
and I want to connect a Redis container.
docker-redis github topic
I tried the following on the application config:
1 localhost:6379
2 app_redis://app_redis:6379
3 redis://app_redis:6379
nothing works on my setup
docker network setup:
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 mynet
Connect to a Database Running on Your Docker Host
PS: this might be off-topic, how I can add the network on docker-compose instead of external
docker-compose:
services:
app-kotin:
build: ./app
container_name: app_server
restart: always
working_dir: /app
command: java -jar app-server.jar
ports:
- 3001:3001
links:
- app-redis
networks:
- front
app-redis:
image: redis:5.0.9-alpine
container_name: app-redis
expose:
- 6379
networks:
front:
external:
name: mynet
with the setup above how can I connect through a Redis container?
Both containers need to be on the same Docker network to communicate with each other. The app-kotin container is on the front network, but the app-redis container doesn't have a networks: block and so goes onto an automatically-created default network.
The simplest fix from what you have is to also put the app-redis container on to the same network:
app-redis:
image: redis:5.0.9-alpine
networks:
- front
The Compose service name app-redis will then be usable as a host name, from other containers on the same network.
You can simplify this setup considerably. You don't generally need to manually specify IP configuration for the Docker-private networks. Compose can create the network for you, and in fact it will create a network named default for you. (Networking in Compose discusses this further.) links: and expose: aren't used in modern Docker networking; Compose can provide a default container_name: for you; and you don't need to repeat the working_dir: or command: from the image. Removing all of that would leave you with:
version: '3'
services:
app-kotin:
build: ./app
restart: always
ports:
- '3001:3001'
app-redis:
image: redis:5.0.9-alpine
The server container will be able to use the other container's Compose service name app-redis as a host name, even with this minimal configuration.

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?

Resources