HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How do I get real client IP inside docker container for logging to the database

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
realloggingthecontainerdockerdatabaseclientgetforhow

Problem

I have a following docker compose file:

```
version: "3.8"

services:
postgres:
image: postgres:11
volumes:
- myapp_postgres_volume:/var/lib/postgresql/data
- type: tmpfs
target: /dev/shm
tmpfs:
size: 536870912 # 512MB
environment:
POSTGRES_DB: elearning_academy
POSTGRES_USER: myapp
POSTGRES_PASSWORD: myapp123
networks:
- myapp_network

pgadmin:
image: dpage/pgadmin4:5.4
volumes:
- myapp_pgadmin_volume:/var/lib/pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: myapp@cse.iitb.ac.in
PGADMIN_DEFAULT_PASSWORD: myapp123
ports:
- 8080:80
networks:
- myapp_network

redis:
image: redis:6.2.4
volumes:
- myapp_redis_volume:/data
networks:
- myapp_network

wsgi:
image: wsgi:myapp3
volumes:
- /myapp/frontend/static/
- ./wsgi/myapp:/myapp
- /myapp/frontend/clientApp/node_modules
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
depends_on:
- postgres
- redis
ports:
- 9090
- 3000:3000
- 8000:8000
environment:
C_FORCE_ROOT: 'true'
SERVICE_PORTS: 9090
networks:
- myapp_network
deploy:
replicas: 1
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
max_attempts: 3
window: 120s

nodejs:
image: nodejs:myapp3
volumes:
- ./nodejs/frontend:/frontend
- /frontend/node_modules
depends_on:
-

Solution

Although you've set the network_mode to host, I believe you need to set the ports -> mode to host to get the real client IP.

For example:

ports: 
  - mode: host


Hopefully this works for you.

Code Snippets

ports: 
  - mode: host

Context

StackExchange DevOps Q#17855, answer score: 1

Revisions (0)

No revisions yet.