How to use a pre-existing network in Docker Compose - docker

Docker version 20.10.16, build aa7e414
docker-compose version 1.29.2, build 5becea4c
macOS Monterey
Version 12.2.1
MacBook Air (M1)
The following are the existing Docker networks:
NETWORK ID NAME DRIVER SCOPE
53c30c122cc6 bridge bridge local
06f81782db26 host host local
eba839136a82 none null local
I want to create a new mongodb container and connect it to the existing bridge network shown above.
I tried different Compose files but none of them worked:
Approach 1)
version: "3.7"
services:
mongodb_container:
image: mongo
container_name: mongodb_demo
ports:
- 27020:27017
networks:
- b
networks:
b:
external:
name: bridge
Approach 2)
version: "3.7"
services:
mongodb_container:
image: mongo
container_name: mongodb_demo
ports:
- 27020:27017
networks:
- bridge
networks:
bridge:
external: true
Approach 3)
version: "3.7"
services:
mongodb_container:
image: mongo
container_name: mongodb_demo
ports:
- 27020:27017
networks:
default:
name: bridge
external: true
All 3 approaches give me the following error:
Starting mongodb_demo ... error
ERROR: for mongodb_demo network-scoped alias is supported only for
containers in user defined networks
ERROR: for mongodb_container network-scoped alias is supported only
for containers in user defined networks ERROR: Encountered errors
while bringing up the project.

The only issue I see with all 3 approaches is indentation. network attribute is usable within the services tag and not within service_name which is mongodb_container in your case. Hence your code should look like:
version: "3.7"
services:
mongodb_container:
image: mongo
container_name: mongodb_demo
ports:
- 27020:27017
networks:
- bridge
networks:
bridge:
external: true

Related

Accessing container on bridge network

I have 2 docker-compose files
1:
version: "3.1"
services:
database:
image: mysql:5.6
networks:
mynetwork:
driver: bridge
2:
version: "3.1"
services:
api:
image: myapi:1
networks:
mynetwork:
external: true
But inside the api container, the hostname database is not found. I have also tried mynetwork_database but that is also not found
How do I access a container on an external network?
Solution was to add:
networks:
<mysql_project_folder_name>_mynetwork:
external: true
And:
networks:
- <mysql_project_folder_name>_mynetwork
to api service

How do I create an external volume using docker compose?

Basing on this Node-RED tutorial, I'm trying to mount an external volume with the Node-RED files outside the docker machine. I'm using the following docker-compose file:
version: "3.7"
services:
node-red:
image: nodered/node-red:latest
environment:
- TZ=Europe/Amsterdam
ports:
- "2000:1880"
networks:
- node-red-net
volumes:
- node-red-data:/home/user/node-red1
volumes:
node-red-data:
networks:
node-red-net:
However, even though this file works fine when I run docker-compose up, the volume exists only inside the docker machine. I've tried adding the line external: true in volumes but I get the following error:
ERROR: In file './docker-compose.yml', volume 'external' must be a mapping not a boolean.
What am I missing? How do I mount an external volume using docker-compose files?
I ended up finding a related question with this answer. There are multiple answers that didn't work for me there (also there's no accepted answer). The syntax that worked was:
node-red-data:
driver: local
driver_opts:
o: bind
type: none
device: /path/external/folder
So the final dockerfile that works after running docker-compose up is:
version: "3.7"
services:
node-red:
image: nodered/node-red:latest
environment:
- TZ=Europe/Amsterdam
ports:
- "2000:1880"
networks:
- node-red-net
volumes:
- node-red-data:/data
container_name: node-red
volumes:
node-red-data:
driver: local
driver_opts:
o: bind
type: none
device: "/home/user/node-red1"
networks:
node-red-net:
Update
If we don't mind having a random name for the volume, the following solution also works fine:
version: "3.7"
services:
node-red:
image: nodered/node-red:latest
environment:
- TZ=Europe/Amsterdam
ports:
- "2000:1880"
volumes:
- /home/user/node-red1:/data
container_name: node-red

Docker container has no network

I have the following docker-compose.yml file:
version: '3.7'
services:
chrome:
image: "image1:v0-1-1"
restart: "no"
networks:
- internal
grpc:
image: "image2:v0-1-1"
restart: "no"
networks:
- internal
depends_on:
- chrome
networks:
internal:
name: internal
# driver: bridge
After running docker-compose up -d --remove-orphans grpc container (image2) has none network, but chrome container (image1) has internal network.
I have faced this strange issue for the first time. What is the problem?
I have the latest version of docker and docker-compose.
Your compose file should be like this try this
version: '3.7'
services:
chrome:
image: "image1:v0-1-1"
restart: "no"
networks:
- internal
grpc:
image: "image2:v0-1-1"
restart: "no"
networks:
- internal
depends_on:
- chrome
networks:
internal: {}

docker compose issue on windows 10

I have an opensource project cloned, and it has docker-compose.yml.
I execute
docker-compose up
But I see error:
ERROR: could not find plugin bridge in v1 plugin registry: plugin not found
I even tried a commonly mentioned solution on SO:
docker network create --driver nat network-name
But issue still persists.
I understand that docker-compose is part of docker desktop install on windows.
How to solve it?
Content of the file:
version: "3"
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
ports: ["9200:9200"]
networks: ["sandbox"]
environment: ["discovery.type=single-node"]
kibana:
image: docker.elastic.co/kibana/kibana:6.7.0
ports: ["5601:5601"]
networks: ["sandbox"]
depends_on: ["elasticsearch"]
logstash:
image: docker.elastic.co/logstash/logstash:6.7.0
volumes:
- ./config/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
networks: ["sandbox"]
ports: ["5000:5000/udp"]
depends_on: ["elasticsearch"]
grafana:
image: grafana/grafana:6.0.2
volumes: ["./grafana/plugins/cinnamon-elasticsearch-app:/var/lib/grafana/plugins/cinnamon-elasticsearch-app"]
ports: ["3000:3000"]
networks: ["sandbox"]
depends_on: ["elasticsearch"]
networks:
sandbox:
driver: bridge

Docker-compose external_links not able to connect

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.

Resources