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

A simple and in-depth study of async and await in JavaScript

Table of contents 1. Introduction 2. Detailed exp...

Detailed explanation of the application of the four states of hyperconnection

Although you think it may be a browser problem, i...

Sample code for implementing a background gradient button using div+css3

As the demand for front-end pages continues to in...

How to implement https with nginx and openssl

If the server data is not encrypted and authentic...

JavaScript Array Methods - Systematic Summary and Detailed Explanation

Table of contents Common array methods Adding and...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

React implements a general skeleton screen component example

Table of contents What is a skeleton screen? Demo...

Use standard dl, dt, dd tags to discard table lists

Now, more and more front-end developers are starti...

MySQL table and column comments summary

Just like code, you can add comments to tables an...

How to use translate and transition in CSS3

I always feel that translate and transition are v...

Summary of commonly used performance test scripts for VPS servers

Here is a common one-click performance test scrip...

VMware Workstation download and installation detailed tutorial

Virtual machines are very convenient testing soft...