Connection refused when I try to work with configuration on Docker Host

I’m very new to the entire Docker issue, and am having a lot of problems that I can’t easily explain. I’m running Docker Community engine v19.03.5 on a 4GB RaspberryPi4 and trying hard to come to terms with the concept. So, I’ve downloaded and can successfully run the nginx web-server image in what might be called a ‘base mode’ using the command ‘sudo docker run --name mynginx1 -d -p 8080:80 nginx’. I should add that I’m quite happy that this is in ‘sudo’ mode, and that it is NOT a critical system at all - anything can be discarded, including, if necessary the entire OS (which is Raspbian Buster kernel 4.19.75-v7l+ #1270). I can look at the IP address of the Pi in a browser, using port 8080 and ‘see’ the standard Nginx index file. All good. So then I want to manage the content and configuration files. If I stop and remove the running Nginx, and then create another instance with:

sudo docker run --name mynginx2 -v /var/www:/user/share/nginx/html:ro -d -p 8080:80 nginx

after, of course, creating a directory on the host at /var/www which contains a simple html file, looking at the web server as before, I see the new file. Exactly as I’ve been led to expect.

So now, I stop and remove this instance and try again, except this time - as many websites are encouraging me to do - I use:

sudo docker run --name mynginx3 -v /var/nginx/conf:/etc/nginx:ro -d -p 8080:80 nginx'

with a view that I can modify the configuration file, I always get:

> curl: (7) Failed to connect to localhost port 8080: Connection refused

and this is driving me crazy!!! Why? Why does the first work exactly as expected but the second - which by all accounts should work equally as well - give me this refusal to connect? Oh, and of course, I have created the directory /var/nginx/conf. What am I doing wrong - or am I seriously misunderstanding something? (Yes, and I can see there might be other ways to do this, but why doesn’t this - supposedly simple - approach work?). Any comments from those with far more experience gratefully received!! Mike

I may have discovered the cause of my error. On https://github.com/nginxinc/docker-nginx/issues/360 yosifkit commented on 22 Aug 2019 that:

“When using a bind mounted folder (like var/nginx/conf/:/etc/nginx/ ), it will not be filled in with anything from the image. If there are files in the image in the target folder ( /etc/nginx/ ), they will be hidden and basically impossible to access in that container. In order to supply config to nginx, you need to provide a full config file on your host (and any other needed files like ssl certs) and then bind-mount the containing folders to their correct places in the container. You can get a copy of the initial nginx config from the container but you’ll most likely need to customize it for your setup”

And that was exactly correct. I followed his instructions to get a copy of the default cnfiguration and placed that in my /var/nginx/conf directory. Then running the docker run command again, and everything worked. I guess I had been thinking this may have been the problem - thanks to @yosifkit for confirming it and allowing me to move on.