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

How to view the status of remote server files in Linux

As shown below: The test command determines wheth...

Vue2/vue3 routing permission management method example

1. There are generally two methods for Vue routin...

MYSQL Left Join optimization (10 seconds to 20 milliseconds)

Table of contents 【Function Background】 [Raw SQL]...

Detailed explanation of using MySQL where

Table of contents 1. Introduction 2. Main text 2....

Steps to transplant the new kernel to the Linux system

1. Download the ubuntu16.04 image and the corresp...

Pure CSS to achieve the effect of picture blinds display example

First, let me show you the finished effect Main i...

25 Ways and Tips to Increase Web Page Loading Speed

Introduction <br />Not everyone has access t...

Detailed example of mysql trigger usage

MySQL trigger syntax details: A trigger is a spec...

Vue implements real-time refresh of the time display in the upper right corner

This article example shares the specific code of ...

Deployment and Chinese translation of the docker visualization tool Portainer

#docker search #docker pull portainer 1. Download...

How to use fdisk to partition disk in Linux

Commonly used commands for Linux partitions: fdis...

Detailed introduction and usage examples of map tag parameters

Map tags must appear in pairs, i.e. <map> .....

IE6/7 is going to be a mess: empty text node height issue

Preface: Use debugbar to view document code in iet...

vue $set implements assignment of values ​​to array collection objects

Vue $set array collection object assignment In th...