简体   繁体   中英

How to set a specfic ip address with docker-compose who's accessible

currently I playaround with Docker and I liked it, but how can I set a IP-Address with docker-compose ?

Here's my problem, on the docker-compose website is a quickstart example with Wordpress.

version: '2'
services:
  db:
    image: mysql:5.7
    volumes:
   - "./.data/db:/var/lib/mysql"
 restart: always
 environment:
   MYSQL_ROOT_PASSWORD: wordpress
   MYSQL_DATABASE: wordpress
   MYSQL_USER: wordpress
   MYSQL_PASSWORD: wordpress

 wordpress:
   depends_on:
     - db
   image: wordpress:latest
   links:
     - db
   ports:
     - "8000:80"
   restart: always
   environment:
     WORDPRESS_DB_HOST: db:3306
     WORDPRESS_DB_PASSWORD: wordpress

And now I want that the Wordress-Site is available under the IP-Address 10.10.10.25, how can I do it ?

It's possible with Docker-compose ?

Edit

So I try to build my network, but it does not work.

 version: '2'
    services:
      db:
        image: mysql:5.7
        volumes:
          - "./.data/db:/var/lib/mysql"
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: wordpress
          MYSQL_DATABASE: wordpress
          MYSQL_USER: wordpress
          MYSQL_PASSWORD: wordpress
        networks:
          mynet:
            ipv4_address: 172.25.0.102
      wordpress:
        depends_on:
          - db
        image: wordpress:latest
        links:
          - db
        ports:
          - "8000:80"
        restart: always
        environment:
          WORDPRESS_DB_HOST: db:3306
          WORDPRESS_DB_PASSWORD: wordpress
        networks:
          mynet:
            ipv4_address: 172.25.0.101
    networks:
      mynet:
        driver: bridge
        ipam:
          config:


 subnet: 172.25.0.0/24

It got only the following error message.

wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 19 wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused wordpress_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.25.0.101. Set the 'ServerName' directive globally to suppress this message wordpress_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.25.0.101. Set the 'ServerName' directive globally to suppress

The docker model isn't to create containers with a specific IP, but to expose and publish only the ports from the container you need accessible from outside. So you'd include something like the following for the service in your docker-compose.yml:

ports:
  - 8080:80

That would direct all traffic that hits your host at 8080 to the container's port 80. You can adjust these ports as needed, or even make them the same. Then all outside traffic trying to reach your wordpress server goes to http://your.docker.host:8080 (for a local test, that would be http://localhost:8080 ).

If you really need to specify the IP address of a container, it can be done, but you need to define your own network first, then run the container on that network (giving it an IP), then you need to update the routing on your routers or other hosts on the network to send traffic to your docker host for that IP, and you may find that your default iptables rules won't allow traffic to that container. Therefore, I wouldn't recommend going down that path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM