Use pipeline to configure Docker container with fixed ip

Four network modes of docker

    1. The host mode
        docker run uses --net=host to specify, the network used by docker is actually the same as the host

    2.
        Use --net=container:container_id/container_name in container mode. Multiple containers use the same network and see the same ip.    
    3. The none mode is
        specified with --net=none. In this mode, no network will be configured.

    4. The bridge mode is
        specified with --net=bridge, the default mode, this mode will assign an independent network namespace to each container. If
        bridge is selected by default, the container will obtain an address through DHCP after startup
====== ===================================================== ========


First, let's talk about the network mode of docker:

When we use docker run to create a container, we can use the --net option to specify the network mode of the container. There are 4 network modes in docker:

   

1: bridge mode, --net=bridge (default).
This is the default setting for dokcer networks. After installing docker, the system will automatically add a bridge docker0 for docker to use. When we create a new container, the container obtains an IP address on the same network segment as docker0 through DHCP. And connect to the docker0 bridge by default, so as to realize the network communication between the container and the host. As follows:
docker inspect --format={{.NetworkSettings.IPAddress}} test
 

 

2: host mode, --net=host.
  The container created in this mode will not have its own independent Network Namespace, that is, there will be no independent network environment. It uses the host's ip and port.

 

3: container mode, --net=container:NAME_or_ID.
This mode is to specify an existing container and share the IP and port of the container. In addition to the network sharing between the two containers, other such as file systems, processes, etc. are still isolated.

 

4: none mode, --net=none.
In this mode, dokcer does not perform any network configuration for the container. We need to add a network card to the container and configure the IP.


Therefore, if you want to use the pipeline to configure the ip address of the docker container, you must be in none mode.

Pipework installation:  https://github.com/jpetazzo/pipework/archive/master.tar.gz

# wget https://github.com/jpetazzo/pipework/archive/master.zip
# unzip pipework-master.zip
# cp pipework-master/pipework /usr/local/bin/
# chmod +x /usr/local/bin /pipework
creates a container in none mode and assigns it an IP.
#docker run -idt --name test --net=none resin
#pipework docker0 test 172.17.0.100/[email protected]
#docker-enter test
#ip a (centos7)     or ifconfig
 

 
The above operations are assigned to the newly created test container An IP address of 172.17.0.100.


Pipework has a flaw. After the container is restarted, the IP settings will disappear automatically and need to be reset.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326072501&siteId=291194637
Recommended