I have a pretty simple docker-compose setup working fine in Ubuntu 20 Desktop, but not working the same in Ubuntu 20 WSL2 in 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
My ./webserver Dockerfile is an ubuntu with an nginx, php7.4, xdebug, and looks like so:
FROM ubuntu:20.04
LABEL maintainer="Cristian E."
WORKDIR /var/www/html
ENV TZ=UTC
RUN apt-get update \
&& apt-get install -y iputils-ping \
&& apt-get install -y nginx \
&& apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
&& mkdir -p ~/.gnupg \
&& chmod 600 ~/.gnupg \
&& echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C \
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C \
&& echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
&& apt-get update \
&& 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 \
&& pecl install xdebug-3.0.0 \
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& curl -sL https://deb.nodesource.com/setup_15.x | bash - \
&& apt-get install -y nodejs \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# DELETE DEFAULT NGINX SITE & 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:
If I run docker-compose up just like on Ubuntu 20 Desktop, I can't ping the container's ip from inside Ubuntu 20 WSL (even though the docker-compose up command was ran from inside Ubuntu 20 WSL too.
Also, if I put the ip in the /etc/hosts file of Ubuntu 20 WSL, I can't access the site. It just hangs forever.
So my question is, why is networking working in one way on native Ubuntu 20 Desktop and why is it working differently on Ubuntu 20 over WSL (even though the tests I did were done from the command line from inside Ubuntu in both cases)