Docker compose custom network to achieve fixed container IP address

Docker compose custom network to achieve fixed container IP address

Due to the default bridge network, the IP address will change after restarting the container. In some scenarios we want to fix the container IP address.
Docker-compose is an orchestration tool for Docker, which creates networks, containers, etc. relative to the command mode. Using configuration files is relatively more convenient and can trace problems.

Paste the docker-compose.yml file directly

version: '2'
services:
  nginx:
   image: nginx:1.13.12
   container_name: nginx
   restart: always
   tty: true
   networks:
     extnetwork:
      ipv4_address: 172.19.0.2
 
networks:
  extnetwork:
   ipam:
     config:
     - subnet: 172.19.0.0/16
      gateway: 172.19.0.1

illustrate:

  • gateway is the gateway address
  • subnet is the network segment
  • extnetwork is a custom network name

In the above configuration, our nginx container has a fixed IP of 172.19.0.2

Example, custom network mode:

version: '2'
services:
  nginx:
   image: nginx:1.13.12
   container_name: nginx
   restart: always
   networks:
     extnetwork:
   ports:
     - 80:80
   volumes:
     - '/nginx/conf.d:/etc/nginx/conf.d'
  nginx2:
   image: nginx:1.13.12
   container_name: nginx2
   restart: always
   networks:
     extnetwork:
      ipv4_address: 172.19.0.2
     
  db:
   image:mysql:5.7
   container_name: db
   volumes:
    - /var/lib/mysql:/var/lib/mysql
   restart: always
   networks:
     extnetwork:
   ports:
     -3306:3306
   environment:
    MYSQL_ROOT_PASSWORD: wordpress
    MYSQL_DATABASE: wordpress
    MYSQL_USER: wordpress
    MYSQL_PASSWORD: wordpress   
  
  wordpress:
   image: wordpress:latest
   container_name: wordpress
   depends_on:
     -db
   ports:
     - "8000:80"
   restart: always
   networks:
     extnetwork:
   environment:
     WORDPRESS_DB_HOST: db:3306
     WORDPRESS_DB_NAME: wordpress
     WORDPRESS_DB_USER: wordpress
     WORDPRESS_DB_PASSWORD: wordpress
networks:
  extnetwork:
   ipam:
     config:
     - subnet: 172.19.0.0/16
      gateway: 172.19.0.1

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker network mode and configuration method
  • Docker Compose network settings explained
  • Docker custom network implementation

<<:  Avoiding Problems Caused by Closures in JavaScript

>>:  Database query which object contains which field method statement

Recommend

Implementation of MySQL Multi-version Concurrency Control MVCC

Table of contents What is MVCC MVCC Implementatio...

Problems and pitfalls of installing Mysql5.7.23 in Win10 environment

I read many tutorials, but found that I could nev...

Mysql 5.6.37 winx64 installation dual version mysql notes

If MySQL version 5.0 already exists on the machin...

Zabbix uses PSK shared key to encrypt communication between Server and Agent

Since Zabbix version 3.0, it has supported encryp...

Implementation of Docker private warehouse registry deployment

As more and more Docker images are used, there ne...

How to install MySQL and Redis in Docker

This article is based on the CentOS 7.3 system en...

How to deploy SpringBoot project using Docker

The development of Docker technology provides a m...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

A little-known JS problem: [] == ![] is true, but {} == !{} is false

console.log( [] == ![] ) // true console.log( {} ...

HTML+CSS to achieve simple navigation bar function

Without further ado, I'll go straight to the ...

Docker starts MySQL configuration implementation process

Table of contents Actual combat process Let's...

MySql 5.7.21 free installation version configuration method under win10

1. Unzip to the location where you want to instal...

Summary of how to add root permissions to users in Linux

1. Add a user . First, use the adduser command to...

How to deploy SpringBoot project using Dockerfile

1. Create a SpringBooot project and package it in...