Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve container name in extra_hosts option in docker-compose

Tags:

I need to curl my API from another container. Container 1 is called nginx Container 2 is called fpm

I need to by able to bash into my fpm container and curl the nginx container.

Config:

#docker-compose.yaml services:     nginx:     build:       context: .       dockerfile: ./docker/nginx/Dockerfile     volumes:       - ./docker/nginx/conf/dev/api.conf:/etc/nginx/conf.d/default.conf     ports:       - 8080:80     links:       - fpm             fpm:     build:       context: .       dockerfile: ./docker/fpm/Dockerfile     volumes:       - .:/var/www/html       - ./docker/fpm/conf/dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini       - ./docker/fpm/conf/dev/api.ini:/usr/local/etc/php/conf.d/api.ini     env_file:       - ./docker/mysql/mysql.env       - ./docker/fpm/conf/dev/fpm.env     links:       - mysql     shm_size: 256M     extra_hosts:       - myapi.docker:nginx 

My initial thought was to slap it in the extra_hosts option like:

extra_hosts:   - myapi.docker:nginx 

But docker-compose up fails with:

ERROR: for apiwip_fpm_1 Cannot create container for service fpm: invalid IP address in add-host: "nginx"

I have seen some examples of people using docker's network configuration but it seems over the top to just resolve an address.

How can I resolve/eval the IP address of the container rather than just passing it literally?

like image 575
Edward Avatar asked Nov 30 '17 15:11

Edward


People also ask

Can we change container name in docker?

To rename a docker container, use the rename sub-command as shown, in the following example, we renaming the container discourse_app to a new name disc_app. After renaming a containers, confirm that it is now using the new name. For more information, see the docker-run man page. That's all!

How do I specify a container name in Dockerfile?

The Dockerfile is for creating images not containers. You can now give names to your containers using the new --name flag for docker run . If --name is not provided Docker will automatically generate an alphanumeric string for the container name.

What is Docker-Compose option?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.


1 Answers

Add network aliases in the default network.

version: "3.7" services:    nginx:     # ...     networks:       default:         aliases:           - example.local    browser-sync:     # ...     depends_on:       - nginx     command: "browser-sync start --proxy http://example.local" 
like image 82
wildpeaks Avatar answered Oct 31 '22 01:10

wildpeaks