How to Set a Static Ip Address in a Docker Container

Provide static IP to docker containers via docker-compose

Note that I don't recommend a fixed IP for containers in Docker unless you're doing something that allows routing from outside to the inside of your container network (e.g. macvlan). DNS is already there for service discovery inside of the container network and supports container scaling. And outside the container network, you should use exposed ports on the host. With that disclaimer, here's the compose file you want:

version: '2'

services:
mysql:
container_name: mysql
image: mysql:latest
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
networks:
vpcbr:
ipv4_address: 10.5.0.5

apigw-tomcat:
container_name: apigw-tomcat
build: tomcat/.
ports:
- "8080:8080"
- "8009:8009"
networks:
vpcbr:
ipv4_address: 10.5.0.6
depends_on:
- mysql

networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1

How to set a static IP to my container for a pre-existent external network in docker-compose?

Yes!

I get it.

I just edit the VPN compose to add the subnet in the "networks" options:

version: "3" 
services: wireguard:
image: linuxserver/wireguard
container_name: wireguard
cap_add:
- NET_ADMIN
- SYS_MODULE
environment:
- PUID=1000
- PGID=1000
- TZ=America/Santiago
- SERVERURL=auto #optional
- SERVERPORT=51820 #optional
- PEERS=5 #optional
- PEERDNS=auto #optional
- INTERNAL_SUBNET=10.13.13.0 #optional
volumes:
- ./config:/config
- ./lib/modules:/lib/modules
ports:
- 51820:51820/udp
sysctls:
- net.ipv4.conf.all.src_valid_mark=1
restart: unless-stopped

networks:
default:
ipam:
config:
- subnet: 172.23.0.0/24

and add the static IP in the compose networks options of each docker to connect after.

version: '3.7'

services:
web:
container_name: web
image: nginx
restart: unless-stopped
expose:
- "80"
volumes:
- ./:/usr/share/nginx/html:ro
networks:
default:
ipv4_address: 172.23.0.25

networks:
default:
external:
name: wireguard_default


Related Topics



Leave a reply



Submit