Search code examples
docker-composelaradock

How to assign a static ip for an nginx container using laradock and docker-compose version 3


Im using laradock for a local dev environment

I have a case where I have a soap server @ (soap.localhost.com) consuming the WSDL file from the nginx container @ (api.localhost.com) using php-fpm.

the php-fpm container doesn't know the ip address of the nginx container unless I hard code it the php-fpm section of the docker-comopose.yml

      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
        - "api.localhost.com:xxx.xxx.xxx.xxx"

Everytime I restart the containers the nginx container's ip address changes so I need to assign a static ip address somehow so I don't have to keep hardcoding the extra_hosts section continually.

Laradock already defines 2 networks interfaces:

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

Solution

  • FINALLY: Instead of assigning a static IP to the NGINX container and adding the domains to the extra_hosts: section to the PHP_FPM container. I have added aliases to the networks section of the NGINX container, like this

    ### NGINX Server #########################################
        nginx:
          build:
            context: ./nginx
            args:
              - PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
              - PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
              - CHANGE_SOURCE=${CHANGE_SOURCE}
          volumes:
            - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
            - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
            - ${NGINX_SITES_PATH}:/etc/nginx/sites-available
            - ${NGINX_SSL_PATH}:/etc/nginx/ssl
          ports:
            - "${NGINX_HOST_HTTP_PORT}:80"
            - "${NGINX_HOST_HTTPS_PORT}:443"
          depends_on:
            - php-fpm 
            - memcached       
          networks:
            frontend:              
            backend:
             aliases:
              - api.localhost.com