hkguile
hkguile

Reputation: 4369

How to assign static ip for docker containers (docker compose) in windows WSL2?

I use docker compose to build up the services, i want to set the mysql to be static, i tried to use networks directive but it didn't work and the errors say the ip already occupied, every this i restart the windows, the mysql ip was changed, sometimes 172.18.0.3,or 172.18.0.4, anyone know how to assign a static ip to the container? here is the yml

version: '3.7'
services:
  nginx:
    container_name: hki_nginx
    image: nginx:latest
    ports:
      - 80:80
      - 4433:443
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf    
      - ./nginx/conf:/etc/nginx/conf.d
      - ./src:/var/www
    links:
      - php
      - php72
  php:
    container_name: hki_php
    image: php:5.6-fpm-ext1
    volumes:
      - ./src:/var/www
      - ./php/php.ini:/usr/local/etc/php/php.ini
      - ./php/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
      #- ./php/phpfpm/:/usr/local/etc/php-fpm.d/
  php72:
    container_name: web_php
    image: php:7.2-fpm-ext2
    volumes:
      - ./src:/var/www
      - ./php72/php.ini:/usr/local/etc/php/php.ini
      - ./php72/php-fpm.conf:/usr/local/etc/php-fpm.d/www.conf
      #- ./php/phpfpm/:/usr/local/etc/php-fpm.d/      
  mysql:
    container_name: hki_mysql
    image: mysql:5.7
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./mysql/init:/docker-entrypoint-initdb.d/
    ports: 
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=*Abcd1234
      - MYSQL_USER=abc
      - MYSQL_PASS=*Abcd1234
    #networks:
        #default:
            #ipv4_address: 172.18.0.3 

Upvotes: 0

Views: 943

Answers (1)

Pat. ANDRIA
Pat. ANDRIA

Reputation: 2412

This is because you commented the part that assigns a static IP to your mysql container

 mysql:
    ...
    #networks:
        #default:
            #ipv4_address: 172.18.0.3

If you take away the #, it will have a static IP.

And you might have forgotten the top-level network section in your docker-compose.yml as the official doc setting static IP states

networks:
  app_net:
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/24

Upvotes: 1

Related Questions