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

Do you know what are the ways to jump routes in Vue?

Table of contents The first method: router-link (...

JavaScript array deduplication solution

Table of contents Method 1: set: It is not a data...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

Summary of solutions to common Linux problems

1. Connect Centos7 under VMware and set a fixed I...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...

Using JS to implement a simple calculator

Use JS to complete a simple calculator for your r...

How to write the style of CSS3 Tianzi grid list

In many projects, it is necessary to implement th...

MySQL series tutorials for beginners

Table of contents 1. Basic concepts and basic com...

How to start and restart nginx in Linux

Nginx (engine x) is a high-performance HTTP and r...

Turn off the AutoComplete function in the input box

Now we can use an attribute of input called autoco...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...