I'm getting this value on the PORTS
column of a docker container ls
entry (a container for a react app served by Nginx):
PORTS
80/tcp, 0.0.0.0:40000->40000/tcp, :::40000->40000/tcp
I know the second part is IPv4 mapping and the third is IPv6. I don't understand the meaning of the 80/tcp
, but I think it's what really makes the app accessible from the internet, because if I use mapping "80:80" it works, but now with "40000:40000" it doesn't.
My project has a structure like this, so it can build multiple projects at once with compose:
|
|- client (a React app)
| |- Dockerfile-client
|- .env.prod
|- docker-compose.yml
The docker-compose.yml
looks like this:
version: '3.7'
services:
client:
build:
dockerfile: ./client/Dockerfile-client
context: ./ # so the .env.prod file can be used by React
container_name: client
env_file:
- .env.prod # enabled to apply CLIENT_PORT var to Dockerfile-client
ports:
- "${CLIENT_PORT}:${CLIENT_PORT}"
# others
All the variables are defined in .env.prod
(CLIENT_PORT
is 40000), and I run compose like `docker compose --env-file .env.prod up", and it doesn't bring errors.
Here's the Dockerfile that builds the client container:
# build env
FROM node:13.13-alpine as build
WORKDIR /app
COPY ./client/package*.json ./
RUN npm ci
COPY ./client/ ./
COPY ./.env.prod ./
RUN mv ./.env.prod ./.env
RUN npm run build
# production env
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE ${CLIENT_PORT}
CMD ["nginx", "-g", "daemon off;"]
It would all work fine if I mapped "80:80", but my problem is how is that 80/tcp
there in the ls
output when there's no "80" to be seen in the files? Might it be because of Nginx?
CodePudding user response:
80/tcp
is from nginx which listens by default on this port.
Correct port mapping in this case will be 4000:80
OR
If you want nginx to listen on other port like 4000
update listen
parameter in nginx.conf file to that port
http {
server {
listen 4000;
}
}
And then use port mapping as 4000:4000