Set Static IP for Docker Container

If you want to change the IP address of a Docker container, there are several methods you can use to achieve this:

  1. Utilize the docker network command to create a custom network, specify an IP range and subnet mask, and then connect the container to that network. This allows for assigning a specific IP address to the container.
docker network create --subnet=192.168.1.0/24 --gateway=192.168.1.1 mynetwork
docker run --net mynetwork --ip 192.168.1.2 -it myimage
  1. Specify the IP address using the –ip parameter when starting the container.
docker run --ip=<desired_ip> -it myimage
  1. Utilize a Docker Compose file to specify the IP address of containers.
version: '3'
services:
  myservice:
    image: myimage
    networks:
      mynetwork:
        ipv4_address: <desired_ip>

networks:
  mynetwork:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.1.0/24

No matter which method you choose, it is important to ensure that the selected IP address is within the network range and not already in use. Hopefully these methods can help you change the IP address of your Docker container.

bannerAds