I am a docker beginner and the first thing i did was download nginx and tried to mount it on 80:80 port but Apache is already sitting there.
docker container run --publish 80:80 nginx
and docker container run --publish 3000:3000 nginx
I tried doing it like this 3000:3000 to use it on port 3000 but it doesn't work .And it doesn't log anything either which i could use for referance.
When you're starting with Docker you may find helpful information about images at DockerHub. For example with nginx you have a section about how to expose public ports.
You can just use:
Port 3000 in your localhost will be forwarded to port 80 which is the port that nginx images use to wait for http connections.
I also recommend you to read these official docs about networking in Docker.
You wrote you are a beginner, so first of all I'll just mention that the default configuration for the nginx image (I'll assume you're using a standard image) is to listen in port
80
.This is why you can't map to port
3000
inside the container because there is no process that listen to this port.Now if I understood you correctly and with the fact that you're using nginx with docker I guess you want to be able to configure the container's port (and not the host port because this is quiet trivial).
@mancini0 started a good direction, but I'll show how to do it in a more dynamic fashion.
We'll use the envsubst command which substitutes environment variables in shell format strings.
This command is available with the offical nginx image and also with the alpine version.
Now for the solution.
Step #1
write your nginx configuration in a template file - let's call it:
site.template
:Notice the PORT placeholder.
Step #2 - with docker-compose
Mount that inside the
/etc/nginx/conf.d
directory and then execute theenvsubst
command to use the template as a reference fordefault.conf
which is the file which will be used to setup the port configuration inside the container:Notice that:
1. You need to execute the nginx daemon after that.
2. I used
/bin/sh
and not/bin/bash
because my base image is alpine.Step #2 (Another option) - inline docker run
If, for some reason you don't want to work with docker-compose you can use the following bash script:
(*) You can also write it inside your Dockerfile with the
CMD
command, but I won't recommend you doing that.The accepted answer does not change the actual port that nginx is starting up on.
If you want to change the port nginx starts up on inside the container, you have to modify the /etc/nginx/nginx.conf file inside the container.
For example, to start on port 9080:
Dockerfile
nginx.conf
Now to access the server from your computer:
navigating to localhost:3333 in a browser, you'll see your content.
There is probably a way to include the default nginx.conf, and override only the server.listen = PORT property, but I'm not too familiar with nginx config, so I just overwrote the entire default configuration.