Implementation example of specifying container ip when creating a container in docker

Implementation example of specifying container ip when creating a container in docker

When Docker creates a container, it uses the bridge network by default and allocates the IP address by itself, and does not allow you to specify it yourself.

In actual deployment, we need to specify the container IP and not allow it to allocate the IP by itself. Especially when building a cluster, a fixed IP is necessary.

We can create our own bridge network: mynet. When creating a container, specify the network as mynet and specify the IP.

View Network Mode

docker network ls 

Create a new bridge network

docker network create --driver bridge --subnet=172.18.12.0/16 --gateway=172.18.1.1 mynet

View network information

docker network inspect mynet 

Create a container and specify the container ip

docker run -e TZ="Asia/Shanghai" --privileged -itd -h hadoop01.com --name hadoop01 --network=mynet --ip 172.18.12.1 centos /usr/sbin/init

Running the container

docker exec -it hadoop01 /bin/bash 

CentOS minimal installation does not have ifconfig command, it can be installed through yum

yum install -y net-tools 

Install ssh service

yum install -y openssh-server

yum install -y openssh-clients

systemctl start sshd.service

Add a non-root user

useradd brock

passwd brock 

Access through a local client (by specifying a port or configuring an independent IP when creating a container)

docker run -e TZ="Asia/Shanghai" -p 6001:22 --privileged -itd -h hadoop01.com --name hadoop01 --network=mynet --ip 172.18.12.1 centos /usr/sbin/init

or

route -p add 172.18.12.0 MASK 255.255.255.0 10.0.75.2 

You may also be interested in:
  • Each time Docker starts a container, the IP and hosts specified operations
  • Docker container specifies a fixed IP/static IP address in a custom network segment
  • How to specify container IP for Docker network bridge mode

<<:  About the difference between inspecting elements and viewing the source code of a web page

>>:  Simple implementation of vue drag and drop

Recommend

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

Example code for mixing float and margin in CSS

In my recent studies, I found some layout exercis...

Details of watch monitoring properties in Vue

Table of contents 1.watch monitors changes in gen...

js uses FileReader to read local files or blobs

Table of contents FileReader reads local files or...

Detailed steps for quick installation of openshift

The fastest way to experience the latest version ...

A universal nginx interface to implement reverse proxy configuration

1. What is a proxy server? Proxy server, when the...

Mini Program implements list countdown function

This article example shares the specific code for...

Detailed explanation of two quick ways to write console.log in vscode

(I) Method 1: Define it in advance directly in th...

Nginx server https configuration method example

Linux: Linux version 3.10.0-123.9.3.el7.x86_64 Ng...

Tutorial on installing php5, uninstalling php, and installing php7 on centos

First, install PHP5 very simple yum install php T...

Detailed tutorial on installing phpMyAdmin on Ubuntu 18.04

We will install phpMyAdmin to work with Apache on...

In-depth study of how to use positioning in CSS (summary)

Introduction to Positioning in CSS position attri...

How to build a DHCP server in Linux

Table of contents 1. Basic knowledge: 2. DHCP ser...