Accessing to outside network from docker

Hi everyone
I have a docker-compose.yaml file and within it i have 3 image one for frontend one for backend and lase one for database. i should use a proxy on my host for connecting to docker in order to run the container

what i want to do is installing git inside backend image but docker could not connect to host proxy
could any one tell how should i set docker so it could connect through my host proxy for fetching data

Recommended documentation

1 Like

Thanks for your reply
i read those documentation and other related sites and make my docker-compose.yml file as below

services:
 
 proxy-relay:
    image: alpine/socat:latest
    network_mode: host
    command: TCP-LISTEN:80,fork,bind=host.docker.internal TCP-CONNECT:127.0.0.1:2081
    extra_hosts:
      - host.docker.internal:host-gateway    
 
 frontend:
  build:
   context: ./frontend
   dockerfile: dockerfile
  image: storeapp-frontend
  ports:
   - 3000:3000

  volumes: 
   - frontend:/storeapp/frontend
  
  develop:
   watch:
    - action: sync+restart
      path: ./frontend
      target: /storeapp/frontend
    - action: rebuild
      path: ./frontend  	  
  depends_on:
   - backend
 
 backend:
  build:
   context: ./backend
   dockerfile: dockerfile
  image: storeapp-backend
  
  develop:
   watch:
    - action: sync+restart
      path: ./backend
      target: /storeapp/backend
    - action: rebuild
      path: ./backend
  command: python manage.py runserver 0.0.0.0:8000
  extra_hosts:
   - host.docker.internal:host-gateway
  volumes: 
   - backend:/storeapp/backend  
  ports:
   - 8000:8000
  depends_on:
   - db
  
  environment:              
   - DJANGO_SECRET_KEY="a5a3=3^rn$$gb0==@wy3d2jxrd^pro!4=0+jrb-9fyu0n%z5=jo" 
   - DJANGO_DEBUG="True"
   - POSTGRES_PASSWORD="123456"
   - POSTGRES_USER=postgres   
 
 db:
  image: postgres:16
  volumes:
   - postgres_data:/var/lib/postgresql/data/
  environment:
   - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
 postgres_data:
 backend:
 frontend:
 external:

my host proxy runs at 127.0.0.1:2081 and any connection to outside network should go through this
when i go into backend bash a want to run apt-get update it could not connect to ubuntu repository
what am i missing?

Localhost or loopback IP adresses would never work as it would mean the processes in the containers would want to connect to the localhost of the container which is not the same as the localhost of the host. You need an IP which can be accesses from the container. It can be the host IP or the docker network gateway ip for example

You mean that i should replace 127.0.0.1 in line7 with my host ip
or i should change any thing else

You understood well, but I missed your proxy-relay service.

Normaly, since 127.0.0.1 always points back to the same network namespace (that is why it is loopback) and it is different in every container and not the same as on the host, unless you run the container in host network mode (except when you run Docker Desktop even with the fake host network enabled) as you did with the proxy-relay service, but then your proxy-relay service will have no ip address and you wil have the same problem with connecting to it. Since you forwarded a host port using socat listening on all interfaces, it should indeed listen on the host gateway, which is the default docker bridge IP. So if you can send the request through that port, it should work.

hHow do you send the request through the host.docker.internal domain? I don’t see the proxy settings in your shared code.

Your’re right
i just set the LISTEN part
could you tell me how to send request through host.docker.internal?

I shared how you could configure a proxy in my first reply. The first link describes how to configure it for containers.

The documentation that you referred is all about setting a proxy for docker ( cli, desktop , …) for connecting to internet
could you please tell me specifically witch part describes how to send request trough host.docker.intenal

You have a proxy on the host, but you forwarded a different port to it for the containers. So that new port is your proxy endpoint that you need to set in the proxy settings described in the documentation. Just use the host.docker.internal domain in the proxy settings. Once you do that, the only problem could be that the app you want to use don’t support proxy variables. But curl will.