Works in the container:
$ sudo docker-compose run --rm nginx bash -c 'nginx && curl localhost'
Creating tmppkmps4k6s5_nginx_run ... done
127.0.0.1 - - [21/Aug/2021:15:24:02 +0000] "GET / HTTP/1.1" 200 3 "-" "curl/7.64.0"
Hi
Can't be reached from outside the docker container:
$ sudo docker-compose up nginx
Creating network "tmppkmps4k6s5_default" with the default driver
Creating tmppkmps4k6s5_nginx_1 ... done
Attaching to tmppkmps4k6s5_nginx_1
nginx_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1 | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
$ curl localhost
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused
Same thing happens if the command is sudo docker-compose up --force-recreate -d nginx
.