I'm trying to define my networks in separate docker-compose.yml file (docker-compose.networks.yml).
Here it is:
version: '3.8'
networks:
pypinfo-rabbitmq:
name: pypinfo_rabbitmq
driver: bridge
When I try to apply this configuration it shows the following warning:
WARNING: Some networks were defined but are not used by any service: pypinfo-rabbitmq
The main configuration file is:
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: pypinfo_rabbitmq
ports:
- "5672:5672"
- "15672:15672"
environment:
- RABBITMQ_DEFAULT_USER
- RABBITMQ_DEFAULT_PASS
- RABBITMQ_DEFAULT_VHOST
volumes:
- rabbitmq_data:/var/lib/rabbitmq/
- rabbitmq_log:/var/log/rabbitmq/
networks:
- pypinfo-rabbitmq
volumes:
rabbitmq_data:
driver: local
rabbitmq_log:
driver: local
networks:
pypinfo-rabbitmq:
external:
name: pypinfo_rabbitmq
And when I apply my main configuration file for my services it says:
ERROR: Network pypinfo_rabbitmq declared as external, but could not be found. Please create the network manually using `docker network create pypinfo_rabbitmq` and try again.
The question: Why are my networks defined in docker-compose.networks.yml not created? Why should I do to force docker-compose to create them?
I found a solution. I can just apply both files at the same time:
docker-compose -f docker-compose.rabbitmq.yml -f docker-compose.networks.yml up -d
It will create the network and containter that has this network in networks section in configuration:
Creating network "pypinfo_rabbitmq" with driver "bridge"
Recreating pypinfo_rabbitmq ... done
Try to create network before running docker compose up
docker network create pypinfo-rabbitmq
Related
I'm trying to run two Docker containers attached to a single Docker network using Docker Compose.
I'm running into the following error when I run the containers:
Error response from daemon: failed to add interface veth5b3bcc5 to sandbox:
error setting interface "veth5b3bcc5" IP to 172.19.0.2/16:
cannot program address 172.19.0.2/16 in sandbox
interface because it conflicts with existing
route {Ifindex: 10 Dst: 172.19.0.0/16 Src: 172.19.0.1 Gw: <nil> Flags: [] Table: 254}
My docker-compose.yml looks like this:
version: '3'
volumes:
dsn-redis-data:
driver: local
dsn-redis-conf:
driver: local
networks:
dsn-net:
driver: bridge
services:
duty-students-notifier:
image: duty-students-notifier:latest
network_mode: host
container_name: duty-students-notifier
build:
context: ../
dockerfile: ./docker/Dockerfile
env_file: ../.env
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
networks:
- dsn-net
restart: always
dsn-redis:
image: redis:latest
expose:
- 5432
volumes:
- dsn-redis-data:/var/lib/redis
- dsn-redis-conf:/usr/local/etc/redis/redis.conf
networks:
- dsn-net
restart: always
Thanks!
The network_mode: host setting generally disables Docker networking, and can interfere with other options. In your case it looks like it might be trying to apply the networks: configuration to the host system network layer.
network_mode: host is almost never necessary, and deleting it may resolve this issue.
I have a simple app that i am trying to connect to an existing network which exists, but when i do docker-compose up it is just creating a new network for itself without joining the existing network. The existing network I am trying to join is for traefik which is called proxy. All my other services running behind traefik is working fine. I have to manually add mynewapp to the proxy network for it to work. All my other services compose files are also decoupled in individual folders, so I dont think thats it.
I tried defining external and internal values to true and false, but it still creates its own network. If i define a default network along with the proxy network, then it just creates two new networks called mynodeapp_default and mynodeapp_proxy. If i run docker network inspect proxy, I dont see mynewapp in it (but I see all my other running services).
The output of my docker network list is
62eba8d7e127 proxy bridge local
a50cdb97b743 testapp_proxy bridge local
What am I doing wrong here?
version: '3'
services:
mynodeapp:
container_name: mynodeapp
build: .
volumes:
- ./:/app/
networks:
- proxy
labels:
- traefik.enable=true
- traefik.network=proxy
networks:
proxy:
If you want a container to join an existing network you need to use the external option:
version: '3'
services:
mynodeapp:
container_name: mynodeapp
build: .
volumes:
- ./:/app/
networks:
- proxy
labels:
- traefik.enable=true
- traefik.network=proxy
networks:
proxy:
external: true
you can specify like below also,
version: "3"
services:
mynodeapp:
container_name: mynodeapp
build: .
volumes:
- ./:/app/
networks:
- proxy
labels:
- traefik.enable=true
- traefik.network=proxy
networks:
outside:
external: true
You can also specify the name of the network separately from the name used to refer to it within the Compose file:
networks:
outside:
external:
name: actual-name-of-network
Not supported for version 2 docker-compose files. Use network_mode instead.
name
Added in version 2.1 file format
Set a custom name for this network.
version: "2.4"
networks:
network1:
name: my-app-net
I have created a network using:
docker network create my_network
And when I run docker network ls I see:
NETWORK ID NAME DRIVER SCOPE
80e99e7a8f98 bridge bridge local
ff48b8c6586b host host local
cdf5969b458d none null local
9bd1e13004b7 my_network bridge local
but when I try to create a redis node using that network, it says:
Service "redis" uses an undefined network "my_network"
docker-compose.yml
version: '2.1'
services:
redis:
image: redis
container_name: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- 6379:6379
networks:
- my_network
Any idea why?
You need to define a pre-existing network in your docker-compose file, as described in the documentation.
networks:
my_network:
external:
name: my_network
I have two docker containers, each running roscore which uses port 11311. Each of the containers has seperate IP address and uses different namespaces when publishing and subscribing. Shouldn't I be able to treat each container as a separate machine? What I want to do is rostopic pub from the host to one of the containers based on namespace.
When I start the containers, I get the following:
$ docker-compose up
Creating mach1 ... error
Creating mach1 ...
ERROR: for mach1 Cannot start service mach1: driver failed programming external
Creating mach2 ... done
cab7aa376623c708c): Bind for 0.0.0.0:11311 failed: port is already allocated
ERROR: for mach1 Cannot start service mach1: driver failed programming external connectivity on endpoint mach1 (9f755a1bd3f1dad40cce6963105a5d7224127dca3e0bb72cab7aa376623c708c): Bind for 0.0.0.0:11311 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
The YAML for docker-compose is:
version: '3'
services:
mach1:
build:
context: .
dockerfile: ./mach1/Dockerfile
environment:
- "ROS_IP=10.10.0.20"
- "ROS_MASTER_URI=http://10.10.0.20:11311"
image: my-image:v1
ports:
- "11311:11311"
networks:
my_net:
ipv4_address: 10.10.0.20
mach2:
build:
context: .
dockerfile: ./mach2/Dockerfile
environment:
- "ROS_IP=10.10.0.21"
- "ROS_MASTER_URI=http://10.10.0.21:11311"
image: my-image:v1
ports:
- "11311:11311"
networks:
my_net:
ipv4_address: 10.10.0.21
networks:
my_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.10.0.0/24
#- gateway: 10.10.0.1
The issue is that you are attempting to map both containers' ports 11311 to 11311 on the host
ports:
- "11311:11311"
Instead, try mapping to different host ports:
ports:
- "11311:11311"
and
ports:
- "11312:11311"
I have a couple of app containers that I want to connect to the mongodb container. I tried with external_links but I can not connect to the mongodb.
I get
MongoError: failed to connect to server [mongodb:27017] on first
connect
Do I have to add the containers into the same network to get external_links working?
MongoDB:
version: '2'
services:
mongodb:
image: mongo:3.4
restart: always
ports:
- "27017:27017"
volumes:
- data:/data/db
volumes:
data:
App:
version: '2'
services:
app-dev:
restart: Always
build: repository/
ports:
- "3000:80"
env_file:
- ./environment.env
external_links:
- mongodb_mongodb_1:mongodb
Networks:
# sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
29f8bae3e136 bridge bridge local
67d5519cb2e6 dev_default bridge local
9e7097c844cf host host local
481ee4301f7c mongodb_default bridge local
4275508449f6 none null local
873a46298cd9 prod_default bridge local
Documentation at https://docs.docker.com/compose/compose-file/#/externallinks says
If you’re using the version 2 file format, the externally-created containers must be connected to at least one of the same networks as the service which is linking to them.
Ex:
Create a new docker network
docker network create -d bridge custom
docker-compose-1.yml
version: '2'
services:
postgres:
image: postgres:latest
ports:
- 5432:5432
networks:
- custom
networks:
custom:
external: true
docker-compose-2.yml
version: '2'
services:
app:
image: training/webapp
networks:
- custom
external_links:
- postgres:postgres
networks:
custom:
external: true
Yuva's answer above for the version 2 holds good for version 3 as well.
The documentation for the external_links isn't clear enough.
For more clarity I pasted the version 3 variation with annotation
version: '3'
services:
app:
image: training/webapp
networks:
- <<network created by other compose file>>
external_links:
- postgres:postgres
networks:
<<network created by other compose file>>:
external: true
Recently I faced Name resolution failure trying to link 2 containers handled by docker-compose v3 representing gRPC server and client in my case, but failed and with external_links.
I'll probably duplicate some of the info posted here, but will try to summarize
as all these helped me solving the issue.
From external_links docs (as mentioned in earlier answer):
If you’re using the version 2 or above file format, the externally-created containers must be connected to at least one of the same networks as the service that is linking to them.
The following configuration solved the issue.
project-grpc-server/docker-compose.yml
version: '3'
services:
app:
networks:
- some-network
networks:
some-network:
Server container configured as expected.
project-grpc-client/docker-compose.yml
services:
app:
external_links:
# Assigning easy alias to the target container
- project-grpc-server_app_1:server
networks:
# Mentioning current container as a part of target network
- project-grpc-server_some-network
networks:
# Announcing target network (where server resides)
project-grpc-server_some-network:
# Telling that announced network already exists (shouldn't be created but used)
external: true
When using defaults (no container_name configured) the trick with configuring client container is in prefixes. In my case network name had prefix project-grpc-server_ when working with docker-compose and than goes the name itself some-network (project-grpc-server_some-network). So fully qualified network names should be passed when dealing with separate builds.
While container name is obvious as it appears from time to time on the screen the full network name is not easy-to-guess candidate when first facing this section of Docker, unless docker network ls.
I'm not a Docker expert, so please don't judge too strict if all this is obvious and essential in Docker world.