Не удается проверить контейнер docker | Каковы различия в сети docker в Ubuntu Desktop и Windows 10 WSL2

#docker #ubuntu #networking #docker-compose #dockerfile

Вопрос:

У меня довольно простая настройка docker-compose, которая отлично работает на рабочем столе Ubuntu 20, но не работает одинаково в Ubuntu 20 WSL2 в Windows 10:

 version: "3.8"
services:
  webserver_awesome:
    container_name: myawesomesite.xyz
    hostname: myawesomesite.xyz
    build: ./webserver
    volumes:
      - './app/:/var/www/html'
    depends_on:
      - db_awesome
    networks:
      - internal_myawesomesite
  db_awesome:
    image: mysql:5.7
    ports:
      - '3310:3306'
    environment:
      MYSQL_ROOT_PASSWORD: 'secret'
      MYSQL_DATABASE: 'myawesomesite'
      MYSQL_USER: 'myawesomesite'
      MYSQL_PASSWORD: 'secret'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    networks:
      - internal_myawesomesite
    volumes:
      - './mysql:/var/lib/mysql'
  redis_awesome:
    image: 'redis:alpine'
    ports:
      - '6381:6379'
    volumes:
      - './redis/:/data'
    networks:
      - internal_myawesomesite
networks:
  internal_myawesomesite:
    driver: bridge
 

Мой файл настройки ./веб-сервер-это ubuntu с nginx, php7.4, xdebug и выглядит так:

 FROM ubuntu:20.04

LABEL maintainer="Cristian E."

WORKDIR /var/www/html

ENV TZ=UTC

RUN apt-get update 
    amp;amp; apt-get install -y iputils-ping 
    amp;amp; apt-get install -y nginx 
    amp;amp; apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 
    amp;amp; mkdir -p ~/.gnupg 
    amp;amp; chmod 600 ~/.gnupg 
    amp;amp; echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf 
    amp;amp; apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C 
    amp;amp; apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C 
    amp;amp; echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list 
    amp;amp; apt-get update 
    amp;amp; apt-get install -y php7.4-cli php7.4-dev 
       php7.4-pgsql php7.4-sqlite3 php7.4-gd 
       php7.4-curl php7.4-memcached 
       php7.4-imap php7.4-mysql php7.4-mbstring 
       php7.4-xml php7.4-zip php7.4-bcmath php7.4-soap 
       php7.4-intl php7.4-readline 
       php7.4-msgpack php7.4-igbinary php7.4-ldap 
       php7.4-redis 
       php7.4-fpm 
       nano 
    amp;amp; pecl install xdebug-3.0.0 
    amp;amp; php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer 
    amp;amp; curl -sL https://deb.nodesource.com/setup_15.x | bash - 
    amp;amp; apt-get install -y nodejs 
    amp;amp; apt-get -y autoremove 
    amp;amp; apt-get clean 
    amp;amp; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# DELETE DEFAULT NGINX SITE amp; REPLACE WITH OUR ONE
RUN rm -rf /etc/nginx/sites-available/default

RUN npm install -g laravel-echo-server

# Turn off daemon mode, so we can control nginx via supervisor
# supervisord can only handle processes in foreground. The default for nginx is running in background as daemon. To ensure that your nginx is running with supervisord you have to set 'daemon off' in your nginx.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

RUN mkdir /etc/nginx/ssl
COPY ./ssl /etc/nginx/ssl

COPY ./php7.4/nginx/default.conf /etc/nginx/sites-available/default

COPY ./run.sh ./

COPY ./php7.4/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

COPY ./php7.4/php.ini /etc/php/7.4/fpm/conf.d/custom-php.ini

RUN sed -i 's/user = www-data/user = 1000/g' /etc/php/7.4/fpm/pool.d/www.conf
RUN sed -i 's/group = www-data/group = 1000/g' /etc/php/7.4/fpm/pool.d/www.conf

#RUN chmod -R 775 /var/www/html/storage

# Make permissions play nice
RUN usermod -u 1000 www-data
RUN chown -R 1000:1000 /var/www
 

What works in Ubuntu 20 Desktop:

I run docker-compose up, the webserver_awesome container goes up and it gets an IP address automatically (as it should);

if I inspect that container and put that container’s ip address inside /etc/hosts like so:

 xxx.xxx.xx.xx    myawesomesite.xyz
 

then I can access myawesomesite.xyz in the browser and it works fine. I can access it via port 80 or 443 or any port that may be configured inside my nginx sites configs (see Dockerfile)

If you look at docker-compose you will see that I am not publishing any ports for webserver_awesome container and yet it is accessible from the host OS via the container’s ip

The reason why I like this is because I can have many of these docker-compose instances, one for each php project that I’m working on, and I can then map the ips of those webserver containers inside /etc/hosts to top level domains like myawesomesite.xyz, anothersite.xyz, yetanother.xyz and I can access all sites at the same time on port 80 or 443 without conflicts.

Side note:
What usually is shown as general practice with local docker dev enviroments is that they publish port 8080 to the host and access the app via localhost:8080, and that is just not very good if you want to work on multiple projects at the same time and use port 443 for each one, and also many third party apis don’t accept localhost as a domain or any other port except 443.

What doesn’t work in Ubuntu 20 WSL2:

Если я запущу docker-compose, как на рабочем столе Ubuntu 20, я не смогу проверить IP-адрес контейнера изнутри Ubuntu 20 WSL (хотя команда docker-compose тоже была запущена изнутри Ubuntu 20 WSL.

Кроме того, если я введу ip-адрес в файл /etc/hosts Ubuntu 20 WSL, я не смогу получить доступ к сайту. Это просто висит вечно.

Поэтому мой вопрос в том, почему сеть работает одним способом на родном рабочем столе Ubuntu 20 и почему она работает по-разному на Ubuntu 20 через WSL (хотя тесты, которые я проводил, в обоих случаях выполнялись из командной строки внутри Ubuntu).