'How to access docker containers on wsl 2

I am having Ubuntu 20.04 LTS running on wsl 2. I have installed docker according to this guide https://docs.docker.com/engine/install/ubuntu/. I have a stack with few services (basically I want to set up an environment to test my services before pushing to prod), the stack is running as expected on production, there are portainer and RabbitMq running on it and they are accessible on the network.

However, when I deploy the same stack (via docker-compose.yml) on the docker that I have installed on the ubuntu on wsl 2 I can not access the RabbitMq and the Portainer services using localhost/127.0.0.1:<the published port/s>. The containers are running stable from what I see with "docker container ls".

I have tried to set an inbound rules in the firewall - nop luck.

Here is the whole service setup from the docker-compose.yml:

  rabbitmq:
    image: rabbitmq:3-management
    logging:
      driver: "json-file"
      options:
        max-file: 3
        max-size: 5m
    hostname: rabbitmq
    deploy:
      resources:
        limits:
          memory: 200M
        reservations:
          memory: 199M
    ports:
      - 15672:15672
      - 5672:5672
    env_file:
      - .env
    networks:
      - webnet


Solution 1:[1]

Alright, it turns out that I should have used the ip of the eth0 interface. Now I can access the services form the host/win.

Just type ipconfig in the docker container:

(if it return "command not found" you can install it with: sudo apt install net-tools)

ipconfig results

Solution 2:[2]

Your may also try to expose wsl ports to windows, using this script https://gist.github.com/xmeng1/aae4b223e9ccc089911ee764928f5486

I have troubles while connecting to wsl via ssh, and manage to do it after using this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 4wk_
Solution 2 Alloky Fant