Docker use ip from host - windows

I have the problem, that I want to create multiple docker container (with docker-compose) and call each container with the ip address.
On a Linux host it works but not in Windows as host.
Example:
container 1 (php) ip: 192.168.100.10
container 2 (mysql) ip: 192.168.100.11
container 3 (nginx) ip: 192.168.100.12
so I want to add a hosts item with the ip of the nginx to use a hostname like project.local for develop on browser.
Have anyone a idea what I must do on windows to realize this?

Each container use a internal IP for the base system that you using with your containers, for example if you are using Linux all container will have an internal IP that can be seen between container, but not for the principal host, if you want to connect to the containers you will need to use the ports for example for MySQL 3306, if the containers aren't seen between it you will need to create links or networks using a docker compose or the bash to set up this configuration, here I adjunct an example:
https://docs.docker.com/compose/networking/#specify-custom-networks
version: "3"
services:
proxy:
build: ./proxy
networks:
- frontend
app:
build: ./app
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend
networks:
frontend:
# Use a custom driver
driver: custom-driver-1
backend:
# Use a custom driver which takes special options
driver: custom-driver-2
driver_opts:
foo: "1"
bar: "2"
https://docs.docker.com/compose/networking/#links
version: "3"
services:
web:
build: .
links:
- "db:database"
db:
image: postgres

Related

How to connect to my local machine domain from docker container?

I have local server with domain mydomain.com it is just alias to localhost:80
And I want to allow make requests to mydomain.com from my running docker-container.
When I'm trying to request to it I see
cURL error 7: Failed to connect to mydomain.com port 80: Connection refused
My docker-compose.yml
version: '3.8'
services:
nginx:
container_name: project-nginx
image: nginx:1.23.1-alpine
volumes:
- ./docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./src:/app
ports:
- ${NGINX_PORT:-81}:80
depends_on:
- project
server:
container_name: project
build:
context: ./
environment:
NODE_MODE: service
APP_ENV: local
APP_DEBUG: 1
ALLOWED_ORIGINS: ${ALLOWED_ORIGINS:-null}
volumes:
- ./src:/app
I'm using docker desktop for Windows
What can I do?
I've tried to add
network_mode: "host"
but it ruins my docker-compose startup
When I'm trying to send request to host.docker.internal I see this:
The requested URL was not found on this server. If you entered
the URL manually please check your spelling and try again.
The host network is not supported on Windows. If you are using Linux containers on Windows, make sure you have switched to Linux containers on Docker Desktop. That uses WSL2, so you should be able to use that in there.

How to correctly config docker-compose to connect to localhost (non-docker service) [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
I'm new to Docker. I'm build a Spring Boot Application deploying on Docker.
Here is my example docker-compose.yml:
version: "3"
services:
user:
container_name: user
image: user-service
build:
context: user-api/
ports:
- "3001:8000"
restart: always
volumes:
- /home/ubuntu/logs/user_service:/opt/app/logs
networks:
- api_network
cms:
container_name: cms
image:cms-service
build:
context: cms-service/
ports:
- "3003:8000"
restart: always
volumes:
- /home/ubuntu/logs/cms_service:/opt/app/logs
networks:
- api_network
networks:
api_network:
driver: bridge
In the server machine, there's a Redis Server running on Ubuntu. I cannot connect the the Redis Server from Docker container to the host machine.
Here is my redis config inside application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=Password123!##
I also tried to change the localhost to
127.0.0.1
172.17.0.1
0.0.0.0
host
host.docker.internal
That's I've found on the internet. But nothing works. Do I need to specifically config anything to allow my Spring Boot Service inside Docker connect to Redis that's running on localhost.
The issue is probably due to the fact your Redis is bound to the address 127.0.0.1 (which is the default configuration) and your containers are not running on the network host.
To solve this, you should reconfigure Redis to bind to both 127.0.0.1 as well as to the IP address of the host as seen from api_network (sudo ip addr show on the host): the easiest thing to do here, if your scenario allows that, is to just bind Redis to 0.0.0.0 (via redis.conf).
As an alternative, you may also want to run your containers on the host network instead of using the api_network bridge: this appears to be overkill according to your issue, by the way, and may lead to security issues and exposed network ports.

Can't connect to Docker sql for Windows by hostname

I have next docker compose file (part of it)
version: '3.7'
services:
# DB Server ==========================================================================================================
mssqlsimple:
image: microsoft/mssql-server-windows-developer:2017-latest
volumes:
- ".\\Prm.DbContext.Application\\FullInit:C:\\data"
container_name: pbpmssqlsimple
ports:
#- "1403:1433"
- target: 1433
published: 1403
protocol: tcp
mode: host
networks:
- backend
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "SP_116b626d-ed7e-4f5d123#"
...
after command docker-compose up i have instance of sql server and can to connect to it by IP (172.21.69.132) or alias id (0338726df5ba) from docker config.
but i can't connect by host name mssqlsimple (or pbpmssqlsimple)
fragment config json
I tried to do it, but failed
disable windows firewall
connect with port mssqlsimple, 1403
used simple syntax for ports "1403:1433"
Tell me please how to solve my problem

traefik proxy for docker container running in host network

i am running traefik as a proxy in docker container
i am using DockerToolBox in windows 10
the traefik proxy was able to recognize the service app which is running in 127.0.0.1, but the service app is actually running in docker host = 192.168.99.x ip
version: '3'
services:
reverse_proxy:
image: traefik
command: --api --docker
ports:
- "81:80"
- "8081:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- backend
whoami:
image: containous/whoami
labels:
- "traefik.frontend.rule=Host:whoami.default"
- "traefik.enable=true"
- "traefik.port=80"
network_mode: host
networks:
backend:
driver: bridge
in the Traefik dashboard http://192.168.99.100:8081
it shows http://127.0.0.1:80 for whoami service
instead of http://192.168.99.100:80
any help would be appreciated.
i want network_mode: host to pick 192.168.99.100 instead of 127.0.0.1
As traefik official documentation says, when resolving service IP, first it
try a lookup of host.docker.internal
and second
if the lookup was unsuccessful, fall back to 127.0.0.1
This means we can just add a host in the traefik container, using --add-host {docker0_IP}(it's the bridge's IP, you can easily use docker inspect {NAME_OF_TRAEFIK} and find the IP of Gateway(for me, it's 172.18.0.1). If you use docker-compose, you can use add following lines to your definition of traefik:
extra_hosts:
- host.docker.internal:{docker0_IP}
Also, I find that it's ok to use the IP my eth0 IP, which means the IP of your LAN(for me, it's 192.168.0.20).
Then, recreate traefik and everything works like a daisy.

Using a shared MySQL container

Tl;Dr; Trying to get WordPress docker-compose container to talk to another docker-compose container.
On my Mac I have a WordPress & MySQL container which I have built and configured with a linked MySQL server. In production I plan to use a Google Cloud MySQL storage instance, so plan on removing the MySQL container from the docker-compose file (unlinking it) and then separate shared container I can use from multiple docker containers.
The issue I'm having is that I cant connect the WordPress container to the separate MySQL container. Would anyone be able to shed any light on how I might go about this?
I have tried unsuccessfully to create a network as well as tried creating a fixed IP that the local box has reference to via the /etc/hosts file (my preferred configuration as I can update the file according to ENV)
WP:
version: '2'
services:
wordpress:
container_name: spmfrontend
hostname: spmfrontend
domainname: spmfrontend.local
image: wordpress:latest
restart: always
ports:
- 8080:80
# creates an entry in /etc/hosts
extra_hosts:
- "ic-mysql.local:172.20.0.1"
# Sets up the env, passwords etc
environment:
WORDPRESS_DB_HOST: ic-mysql.local:9306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
WORDPRESS_DB_NAME: wordpress
WORDPRESS_TABLE_PREFIX: spm
# sets the working directory
working_dir: /var/www/html
# creates a link to the volume local to the file
volumes:
- ./wp-content:/var/www/html/wp-content
# Any networks the container should be associated with
networks:
default:
external:
name: ic-network
MySQL:
version: '2'
services:
mysql:
container_name: ic-mysql
hostname: ic-mysql
domainname: ic-mysql.local
restart: always
image: mysql:5.7
ports:
- 9306:3306
# Create a static IP for the container
networks:
ipv4_address: 172.20.0.1
# Sets up the env, passwords etc
environment:
MYSQL_ROOT_PASSWORD: root # TODO: Change this
MYSQL_USER: root
MYSQL_PASS: root
MYSQL_DATABASE: wordpress
# saves /var/lib/mysql to persistant volume
volumes:
- perstvol:/var/lib/mysql
- backups:/backups
# creates a volume to persist data
volumes:
perstvol:
backups:
# Any networks the container should be associated with
networks:
default:
external:
name: ic-network
What you probably want to do is create a shared Docker network for the two containers to use, and point them both to it. You can create a network using docker network create <name>. I will use sharednet as an example below, but you can use any name you like.
Once the network is there, you can point both containers to it. When you're using docker-compose, you would do this at the bottom of your YAML file. This would go at the top level of the file, i.e. all the way to the left, like volumes:.
networks:
default:
external:
name: sharednet
To do the same thing on a normal container (outside compose), you can pass the --network argument.
docker run --network sharednet [ ... ]

Resources