Accessing container on bridge network - docker

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

Related

How to use a pre-existing network in Docker Compose

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

docker does not expose static ip for container

i am trying to bind container with nginx to ip 172.16.238.10 , but for some reason docker ignores settings in docker-compose
#my docker-compose file
version: "3.9"
services:
nginx:
build: nginx/
ports:
- 80:80/tcp
volumes:
./dokcer/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
./dokcer/nginx/conf/hosts:/etc/hosts
./docker/project:/var/www/project
networks:
app_net:
ipv4_address: 172.16.238.10
php-fpm:
build: php-fpm/
ports:
- 9000:9000/tcp
volumes:
./dokcer/php-fpm/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
./dokcer/php-fpm/conf/hosts:/etc/hosts
./dokcer/project:/var/www/project
networks:
app_net:
ipv4_address: 172.16.238.11
networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
after build and launch, I look at the docker_app_net network and see that the nginx container has ip 172.16.238.2 although I expected it to be 172.16.238.10.
what could be the problem? I will be grateful for every answer because I am confused :c

Connect to database from another container

Please help me if it's possible.
I need to start 2 applications with a single database.
I have 2 applications. First domain.com, 2-nd api.domain.com. Each application has docker-compose.yaml files.
domain.com - CMS
version: "3.8"
services:
web:
container_name: domain_web
build:
context: ./docker/php
dockerfile: Dockerfile
working_dir: /var/www/html
#command: composer install
volumes:
- ./:/var/www/html
- ./docker/php/app.conf:/etc/apache2/sites-available/000-default.conf
- ./docker/php/hosts:/etc/hosts
networks:
domain:
ipv4_address: 10.9.0.5
networks:
domain:
driver: bridge
ipam:
config:
- subnet: 10.9.0.0/16
gateway: 10.9.
volumes:
bel_baza:
api.domain.com - Laravel 5.6
version: "3.8"
services:
web:
container_name: api_domain_web
build:
context: ./docker/php
dockerfile: Dockerfile
working_dir: /var/www/html
# command: composer install
volumes:
- ./:/var/www/html
- ./docker/php/app.conf:/etc/apache2/sites-available/000-default.conf
- ./docker/php/hosts:/etc/hosts
networks:
api_domain:
ipv4_address: 10.15.0.5
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
container_name: api_domain_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: domain
MYSQL_USER: user
MYSQL_PASSWORD: user
volumes:
- api_domain_baza:/var/lib/mysql
- ./docker/db:/docker-entrypoint-initdb.d
networks:
api_domain:
ipv4_address: 10.15.0.6
phpmyadmin:
image: phpmyadmin
restart: always
container_name: api_domain_pma
networks:
api_domain:
ipv4_address: 10.15.0.7
redis:
image: redis:3.0
container_name: api_domain_redis
networks:
api_domain:
ipv4_address: 10.15.0.10
networks:
api_domain:
driver: bridge
ipam:
config:
- subnet: 10.15.0.0/16
gateway: 10.15.0.1
volumes:
api_domain_baza:
api_domain started successfully.
I need to connect domain.com with database api_domain_db. For connecting host, I used IP address 10.15.0.6. First application not connected to the database from 2nd application.
What is my problem?
How I can connect domain.com with the database of 2nd application?
your problem is your are using separate docker compose applications for each application. And by default those applications can not access each other inner parts:
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
doc is here - https://docs.docker.com/compose/networking/
So it is like it creates separate network for each docker compose.
if you want them both to see each other inner part you can create external docker network as this:
docker network create --subnet 10.1.0.0/24 network_name
and then use that network in both docker compose like this:
networks:
default:
external:
name: network_name
services:
.....
if you need fixed IPs, you can define them as
app:
image: ...
networks:
default:
ipv4_address: 10.1.0.10

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: {}

Re-route backend routes between containers within the same host

Hi I'm new to docker containers. I have the following setup:
The docker host is running on a Linux server, it is running 4 apps:
reverse-proxy (nodejs-based)
frontend container
backend container
active mq container
All of them are running in my-network
Here is the problem, I would like to re-route every urls that come into the reverse-proxy to where it suppose to be. And restriction is I can't use re-write the reverse-proxy, because it is not worthy to re-write
The following are the docker-compose.yml files
version: '3'
services:
backend:
image: tomcat/test:first
ports:
- "8080:8080"
networks:
- my-network
volumes:
- "/home/lluo/app/myApp/target/MyApp/WEB-INF/classes/META-INF/config:/usr/local/tomcat/conf/myApp-configuration/"
depends_on:
- active_mq
active_mq:
image: active_mq/test:first
ports:
- "24043:24043"
networks:
- my-network
networks:
my-network:
external: true
version: '3'
services:
reverse_proxy:
container_name: proxy_container
image: reverse/proxy:docker
ports:
- "0.0.0.0:15009:15009"
networks:
- my-network
networks:
my-network:
external: true
version: '3'
services:
frontend:
image: template/frontend:after_merge_develop
container_name: fe_container
ports:
- "3000:3000"
networks:
- my-network
volumes:
- "/home/lluo/config.json:/app/dist/config.json"
networks:
my-network:
external: true
And I bind the network ipv4_address to be 0.0.0.0
Please help me resolve this issue, it's been days.
Thank you very much!

Resources