Docker Community Forums

Share and learn in the Docker community.

Using docker-compose network to give a service a fixed IP address

Hello,

I’m have several docker containers that are bult from multiple dockfiles using a docker-compose file. They all seem to work and i can connect to them through opc.tcp://localhost:xxx. Rather than bridge these with the host system i’d like to make it appear that these services are separate machines with their own IP address.

From what i understand inorder to do this i need to creat a docker network?

looking through the guides i’ve added the following to one of my services in my docker-compose file.

services:
sample-server:
build:
context: .
dockerfile: ./sample-server/Dockerfile
networks:
app_net:
ipv4_address: 192.168.70.66
restart: always
ports:
- “7021:7021”

networks:
app_net:
driver: bridge
enable_ipv6: false
ipam:
driver: default
config:
- subnet: 192.168.70.0/24
gateway: 192.168.70.1

The conatiner builds ok and i can still connect to it at opc.tcp:localhost:7021 - however i can’t connect to it at the specified IP address 192.168.70.66?

Is there something i’m missing?

Many thanks!
Ollie

Hello Ollie,

you didn’t mention if you are running a windows-box (with windows-containers or linux-containers) or on a linux-box.
I’ve tested on my linux box and was able to ping a container within my bridge-network by its IP-address.
Whereas on my windows-box running linux-containers I was not able to do so. See Networking features in Docker Desktop for Windows | Docker Documentation for more information.

But usually it is not needed to have fixed ip-addresses for your containers. Let Docker decide which ip to use for which container and simply use port-mapping and/or the container’s names to access one container from another container.

Best regards
Matthias :slight_smile:

Hi Matthais!

Thanks for getting back to me - sorry i’m very new to docker!! :slight_smile:

I’m running these linux containers on my windows machine. My machine has two NICs (one for my VLAN and one for the wider network) i’d like to specify the VLAN and make the containers appear as indepdant machines on thta network.

Do i need to specify a macvlan in docker for this or switch from Bridged to NAT as the driver?

i can also fireup a linux machine and install docker on there if that would be easier?

Would the code work as it is on there to make the conatiner appear as a separate machine?

Thanks again!
Ollie