Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding extra_hosts to the compose file stubs for automatic host.docker.internal mapping #222

Merged
merged 1 commit into from
Aug 19, 2021

Conversation

vs0uz4
Copy link
Contributor

@vs0uz4 vs0uz4 commented Aug 19, 2021

Fantastic implementation of xdebug 3.0 by Nick Haynes in PR [#209], I think it's very pertinent to have the option of a step debugger built into the container.

However, in the PR description, which has even been approved and merged, Nick Haynes cites the issue that host.docker.internal is not available in containers running in linux environments, and the fact that users of this OS have to update the environment variable in their projects using the command below:

docker inspect -f {{range.NetworkSettings.Networks}}{{.Gateway}}{{end}} <container-name>

However, I would like to make a small contribution, which I believe is valid, as it will bring greater compatibility between Windows, Mac and Linux environments, so that we do not need extra commands to detect the IP of the Host machine.

The version used in the template (docker-compose.stub) of sail already supports the addition of hostname mappings, with this we were able to solve the host.docker.internal issue no longer needing the use of extra commands to detect the IP address of the host machine. For this we can add the option extra_hosts to the service where we create the application container, so we will be adding a mapping for host.docker.internal pointing to the host machine.

extra_hosts:
    - 'host.docker.internal:host-gateway'

More information about the extra_hosts option can be found in the documentation at compose-file#extra_hosts

A small change that would have value. The laravel.test service would look like this:

laravel.test:
    build:
        context: ./vendor/laravel/sail/runtimes/8.0
        dockerfile: Dockerfile
        args:
            WWWGROUP: '${WWWGROUP}'
    image: sail-8.0/app
    extra_hosts:
        - 'host.docker.internal:host-gateway'
    networks:
        - sail
    ports:
        - '${APP_PORT:-80}:80'
    environment:
        WWWUSER: '${WWWUSER}'
        LARAVEL_SAIL: 1
        XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
        XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
    volumes:
        - '.:/var/www/html'
    depends_on:
        - mysql
        - redis

With this, every time we create/start the application container the host.docker.internal mapping will be created and will point to the host machine. Inside the container in your /etc/hosts file, the appropriate appointment records will be created, as shown in the example below:

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
172.17.0.1      host.docker.internal

@vs0uz4
Copy link
Contributor Author

vs0uz4 commented Aug 19, 2021

This small change helps not only in the xdebug issue, but also for any need to make a connection from the container to the host machine. Like for example spatie's "ray".

@vs0uz4 vs0uz4 changed the title Adding extra_hosts to the compose file for automatic host.docker.internal mapping Adding extra_hosts to the compose file stubs for automatic host.docker.internal mapping Aug 19, 2021
@taylorotwell taylorotwell merged commit 480eaf8 into laravel:1.x Aug 19, 2021
@vs0uz4 vs0uz4 deleted the add-extra_hosts-in-compose-file branch August 19, 2021 13:30
cdarken pushed a commit to cdarken/sail that referenced this pull request Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants