Implementation of setting fixed IP when starting docker container

Implementation of setting fixed IP when starting docker container

Network type after docker installation

[root@insure updev]# docker network ls
NETWORK ID NAME DRIVER SCOPE
14da40175b01 bridge bridge local
65fb78c28e4f host host local
e0d0c90c1462 none null local

Note: By default, all Docker containers started with bridge network use bridge, which is the bridge network created when Docker is installed. Each time the Docker container is restarted, it will obtain the corresponding IP address in sequence. This will cause the Docker IP address to change after restart. If no network is specified, use --network=none, and the Docker container will not be assigned a LAN IP.

The host network uses --network=host. At this time, the Docker container's network will be attached to the host, and the two can communicate with each other. For example, if you run a web service in a container and listen to port 8080, port 8080 on the host will be automatically mapped to the container.

Creating a custom network

First check the automatically assigned IP address

[root@insure updev]# docker inspect -f='{{.Name}} {{.NetworkSettings.IPAddress}} {{.HostConfig.PortBindings}}' $(docker ps -aq)
/awesome_lamarr 172.17.0.4 map[8091/tcp:[{ 8091}]]
/priceless_leavitt 172.17.0.2 map[]
/clever_davinci 172.17.0.3 map[8080/tcp:[{ 8888}]]

You can only create 16 network addresses at a time.

[root@insure updev]# docker network create --subnet=172.18.0.0/16 mynetwork
cf556844631a91a2a530fc07146cf03de650214ee50469675e232cd2b9e243b5
[root@insure updev]# ifconfig
br-cf556844631a: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:90:0c:71:1e txqueuelen 0 (Ethernet)
RX packets 29759 bytes 1736558 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 29759 bytes 1736558 (1.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Explanation: 172.18.0.1 will be occupied by the system

Create a docker container and start it

[root@insure updev]# docker run -itd -p 8091:8091 --name eurekadev --net mynetwork --ip 172.18.0.2 172.16.120.194:5000/claimeureka:latest /bin/bash
a6665cd3fd2e1cb7fca1215a1e75997276b928440e6b888cda4fe3644e0434df
[root@insure updev]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6665cd3fd2e 172.16.120.194:5000/claimeureka:latest "java -jar /app.jar …" 6 seconds ago Up 5 seconds 0.0.0.0:8091->8091/tcp eurekadev
 [root@insure updev]# docker inspect a6665cd3fd2e | grep IPAddress
  "SecondaryIPAddresses": null,
    "IPAddress": "",
    "IPAddress": "172.18.0.2",

Note: Through the command, you can see that the container has been successfully started, and the address is also the IP address we assigned

This is the end of this article about how to start a fixed IP address for a docker container. For more information about docker fixed IP addresses, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker binding fixed IP/cross-host container mutual access operation
  • How to fix IP settings in Docker
  • Docker cannot bind to static external network fixed IP and its solution
  • Detailed explanation of fixed IP allocation for Docker containers
  • Docker container specifies a fixed IP/static IP address in a custom network segment
  • How to configure fixed IP and bridge in Docker

<<:  Summarize the User-Agent of popular browsers

>>:  Example code for implementing simple ListViews effect in html

Recommend

What are the differences between xHTML and HTML tags?

All tags must be lowercase In XHTML, all tags must...

The viewport in the meta tag controls the device screen css

Copy code The code is as follows: <meta name=&...

Detailed explanation of the abbreviation of state in react

Preface What is state We all say that React is a ...

Docker renames the image name and TAG operation

When using docker images, images with both REPOSI...

Detailed explanation of CSS multiple three-column adaptive layout implementation

Preface In order to follow the conventional WEB l...

Native js to implement drop-down box selection component

This article example shares the specific code of ...

How to use JSX to implement Carousel components (front-end componentization)

Before we use JSX to build a component system, le...

Detailed tutorial for springcloud alibaba nacos linux configuration

First download the compressed package of nacos fr...

How to insert batch data into MySQL database under Node.js

In the project (nodejs), multiple data need to be...

36 principles of MySQL database development (summary)

Preface These principles are summarized from actu...

Complete steps to install boost library under linux

Preface The Boost library is a portable, source-c...

MySQL conditional query and or usage and priority example analysis

This article uses examples to illustrate the usag...