We have the nginx running inside a docker container. Therefore, to be able to port forward from a URL to a port running in a different docker container, these containers must be running on the same network. I have setup a docker network called net
, which the nginx, pixel art, and wiki docker containers are all running on. This allows you to reference the port on the container by doing http://[container-name]:[port]
in the nginx (or any other container on net
for that matter). For further examples of this just look at the config files for the wiki and pixel art. Usually, adding the docker network is as simple as just adding
networks:
- net
to each container in the compose (at least each one that needs to be connected to the network), and then adding
networks:
net:
external: true
to the bottom of your docker compose file. Alternatively, if you're using a docker command, by including --network=net
in your docker command.
A side effect of hosting the nginx in a docker container is that it may be difficult to access ports that are not running inside docker containers, so it is likely easiest to just dockerise anything you want to add to the nginx (although I'm not certain, I've never tried add a non-dockerised port forward to the nginx).
There is an internal and external port for a docker container. In the docker container it will look like so:
ports:
- <external>:<internal>
- 1000:2000
The external port (1000) is used outside the container, and has to be unique. For example, if I do ssh -L 1000:127.0.0.1:1000
and then visit localhost:1000
in my browser, I will be able to see the docker application. The internal port (2000) is used within a docker container, and therefore does not have to be unique (and there is usually no reason to need to change it from the default as a result). When referring to a docker container with its name (as you would in nginx), you use the internal port. E.g. you would have proxy_pass http://example:2000
in the nginx config.
If when you add the link to your docker container and the site goes down, and the nginx container refuses to come up, it is likely because it is unable to find your container. You can confirm it with the following command:
# docker compose logs
Following the following troubleshooting tips may lead to a resolution:
# docker ps -a
and looking at the name of the docker container you want to add# docker network inspect net
to check your docker container is on the nginx network. If it is not, it is likely an issue with your docker compose file. Make sure you have specified the network net
for each container and at the very bottom of the file.# docker container exec -it docker-nginx-jekyll-nginx-1 /bin/sh
followed by # ping [name]
. If everything is set up correctly you should receive a response