Skip to content

Instantly share code, notes, and snippets.

@RioChndr
Last active March 11, 2023 05:10
Show Gist options
  • Save RioChndr/e2150d71409b5472338b9c812ded85e6 to your computer and use it in GitHub Desktop.
Save RioChndr/e2150d71409b5472338b9c812ded85e6 to your computer and use it in GitHub Desktop.

Docker problem

Connect from container to host

Docker container cannot connect to host wsl. I don't know why.

microsoft/WSL#6364

Check your connection from container to host using this code. This will run curl code from container. alternatively you can do 2 test, using --add-host or not.

docker run -it --rm --add-host=host.docker.internal:host-gateway alpine sh -c "apk add curl; curl -X HEAD -vvv http://host.docker.internal:3000"

source : microsoft/WSL#6364 (comment)

Get ip host windows and linux (wsl)

The solution is get IP wsl and set it directly to container. first get ip using this code. Source solution microsoft/WSL#1032 (comment)

export WINDOWS_HOST=$(ip --json route show default | jq -re '.[].gateway')
export WSL_HOST=$(ip --json --family inet addr show eth0  | jq -re '.[].addr_info[].local')

save it at ~/.profile or ~/.zshrc, rc file

Solve get IP from inside container

https://dev.to/natterstefan/docker-tip-how-to-get-host-s-ip-address-inside-a-docker-container-5anh

after save environtment variable to system. Set environtment on docker compose file

serives:
  services-1:
    environtment:
      - ACTION_BASE_URL: ${WSL_HOST:-host.docker.internal}

Read Docker environtment variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment