简体   繁体   English

如何给一个容器一个 static ip,或者我如何链接两个 Docker 容器?

[英]How do I give a container a static ip, or how do I link two Docker containers?

I have a container with RabbitMQ, it has to connect to the external IP of the application container, the problem is that this external IP is dynamic and changes every time.我有一个带有 RabbitMQ 的容器,它必须连接到应用程序容器的外部 IP,问题是这个外部 IP 是动态的并且每次都在变化。 Accordingly, I have to manually change the configuration of my RabbitMQ every time.因此,我每次都必须手动更改 RabbitMQ 的配置。 Maybe you can somehow set a static ip for the "App" container so that it does not change or somehow connect these two containers?也许您可以以某种方式为“App”容器设置一个 static ip 以便它不会更改或以某种方式连接这两个容器?

version: '3'
services:
#PHP Service
app:
    build:
        context: .
        dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
        SERVICE_NAME: app
        SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
        - ./:/var/www
        - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
        - postgres
#Nginx Service
webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
        - "80:80"
        - "443:443"
    volumes:
        - ./:/var/www
        - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
        - postgres
#Redis
redis:
    image: 'redis:alpine'
    ports:
        - "6379:6379"
#PostgreSQL        
postgres:
    container_name: postgres_container
    image: postgres
    hostname: postgres
    environment:
        POSTGRES_DB: postgres
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: secret
        PGDATA: /data/postgres
    volumes:
        - postgres:/data/postgres
    ports:
        - "5432:5432"
    networks:
        - postgres
    restart: unless-stopped

pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
        PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
        PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
    volumes:
        - pgadmin:/root/.pgadmin
    ports:
        - "${PGADMIN_PORT:-5050}:80"
    networks:
        - postgres
    restart: unless-stopped  
    depends_on: 
        - postgres     
#RabbitMQ
rabbit:
    image: "rabbitmq:3-management"
    hostname: "rabbit"
    environment:
        RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
        RABBITMQ_DEFAULT_USER: "rabbitmq"
        RABBITMQ_DEFAULT_PASS: "rabbitmq"
        RABBITMQ_DEFAULT_VHOST: "/"
    ports:
        - "15672:15672"
        - "5672:5672"
    labels:
        NAME: "rabbitmq"    
    networks:
        - postgres     
#ElasticSearch
elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
    ports:
        - "9200:9200"
        - "9300:9300"
    networks:
        - postgres                      
#Docker Networks
networks:
  postgres:
    driver: bridge
volumes:
    postgres:
        pgadmin:  

If they are in the same networks You can simply use the service name instead of the IP, just use the app, You can ping the app service name inside the rabbitMQ container and it works.如果它们在同一个网络中,您可以简单地使用服务名称而不是 IP,只需使用应用程序,您可以在 rabbitMQ 容器内 ping 应用程序服务名称,它可以工作。

as you are using docker-compose all your containers are already on the same network.当您使用 docker-compose 时,您的所有containers都已经在同一个网络上。 You can access a container from another container using the name you have specified: ie for rabbitmq it is rabbit for redis it will be redis您可以使用您指定的名称从另一个容器访问容器:即对于 rabbitmq 它是rabbit对于 redis 它将是redis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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