How to expose nginx docker image to port other than 80? - docker

I'm having the .Dockerfile (from the source):
# build stage
FROM node:9.11.1-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:1.13.12-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Where at the end the application is exposed to port 80. I'm then having another different .Dockerfile and for building both of them I'm using the following docker-compose.yml file:
version: "3"
services:
service-name-one:
image: dockerImageFromAbove
ports:
- "8080:80"
service-name-two:
image: someOtherImage
ports:
- "3000:3001"
And this is the example that is actually working. But I would need to change the port from nginx docker image and instead of port 80 I would need to have port 8081. By simple changing this in both files from above, it's not working and in my research I found that the only working example is when exposing to port 80 from nginx.
I tried replacing the line
EXPOSE 8081
with
RUN -P 80:8081
EXPOSE 8081
but seems as -P flag is not supported here. So how can I do such a mapping, before exposing nginx to port 80?
I found this post, but I can't figure out how to use the answers in my docker files.
I also found this post (part for environment variables), but also not sure how to integrate it with my docker-compose file.

The second file is not a Dockerfile but a docker-compose.yml, you have to change in the docker-compose.yml the ports and it will be ok.
The option -p "hostport:containerport" expose the port when you use command docker run.
Anyway i suggest you to use the supported and official image before change too much the Image in the dockerfile.
Anyway if you really need 8081 try something like this
version: "3"
services:
service-name-one:
image: yournginxOrSomethingelse
ports:
- "8080:80"
- "8085:8081"

I believe -P needs to be lower case: -p (this is for command-line command, not Dockerfile) The syntax is:
Dockerfile:
....
EXPOSE 80
....
Command line:
docker run -d -p 8081:80 --name my-service my-service:latest

Related

Meaning of PORTS column of Docker container ls

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?
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

How to put an existing container in docker-compose?

I already have a vue application containerized and running, but how do I put it in docker compose?
Dockerfile:
FROM node:14
WORKDIR /app
RUN npm install #babel/core #babel/node #babel/preset-env nodemon express axios cors mongodb
COPY . .
EXPOSE 4200
CMD ["npm", "run", "serve"]
So when I try to put port "4200" it says that I have already the same port running, so how do I put that container inside whole app which will store multiple containers?
This is my docker-compose try:
version: '3.8'
services:
posts:
build: ./posts
ports:
- "4200:4200"
So this is the visualisation of something that I want to do:
Change host port in compose
For example:
ports:
- "4201:4200"

replicate call to start nginx in Dockerfile from docker-compose

I have the following Dockerfile that does work:
FROM nginx:1.15.2-alpine
COPY ./build /var/www
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
ENTRYPOINT ["nginx","-g","daemon off;"]
I need to replicate this in docker-compose.
I'd like to specify the same image as the FROM instruction above.
I don't know where to put the COPY commands in docker-compose and I don't think ENTRYPOINT is what I am after in docker-compose
You don't need to redefine the entrypoint when using docker-compose, it will be taken from the base nginx image. You can use bind mounts instead of COPY. E.g. like this (not tested):
nginx:
image: "nginx:1.15.2-alpine"
container_name: nginx
volumes:
- ./build:/var/www
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- "80:80"
Of course, the ports part will already go beyond the Dockerfile and actually publish the port, you can leave that out.

How do I configure docker compose to expose ports correctly?

I'm using docker and docker compose to run a clojure and a node app, alongside postgres.
The project is contained in the following folder structure.
project/
-- app/
-- -- Dockerfile
-- frontend/
-- -- /Dockerfile
-- docker-compose.yml
The app/Dockerfile looks like so...
FROM clojure:latest
COPY . /usr/src/app
WORKDIR /usr/src/app
EXPOSE 9000
CMD ["lein", "run", "migrate", "&&","lein", "run"]
The frontend/Dockerfile looks like so ...
FROM node:5
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm install
EXPOSE 8080
CMD ["npm", "start"]
And lastly the docker-compose.yml looks like...
frontend:
image: bradcypert/node
volumes:
- ./frontend:/usr/src/frontend
ports:
- "8080:8080"
backend:
image: bradcypert/clojure
volumes:
- ./app:/usr/src/backend
ports:
- "9000:9000"
links:
- postgres
postgres:
image: postgres
ports:
- "5432:5432"
backend is failing for a separate reason, but the frontend seems to be running successfully, that being said, I'm unable to hit localhost:8080 and see the app. What do I need to do make this happen?
Thanks in advance.
Just to clarify, the command being run is docker-compose up
With boot2docker (on Mac or Windows), to access any port from localhost, you have to configure your VirtualBox VM in order to port-forward that port from the VM into the host.
Your port mappings are correct, but you still need to make visible to your host (Mac) the one port you want to access from localhost (your Mac).
See for instance "Using boot2docker to run Docker on a Mac or Windows" from Andrew Odewahn:
That way, you don't have to find out what the IP of your machine is.
(Which you can see with docker-machine ls followed by docker-machine ip <name>)

Docker compose ignores my Dockerfile when I use the build command

I have this folder structure:
/home/me/composetest
/home/me/composetest/mywildflyimage
Inside composites I have this docker-compose.yml:
web:
image: test/mywildfly
container_name: wildfly
ports:
- "8080:8080"
- "9990:9990"
Inside mywildflyimage I have this docker image:
FROM jboss/wildfly
EXPOSE 8080 9990
ADD standalone.xml /opt/jboss/wildfly/standalone/configuration/
RUN /opt/jboss/wildfly/bin/add-user.sh admin admin --silent
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]
If i run
docker built -t test/mywildfly .
docker-compose up
Everything works great, and the management part is minded to 0.0.0.0 (-bmanagement 0.0.0.0 part of the CMD command).
If I change my docker-compose.yml:
web:
build: mywildflyimage
container_name: wildfly
ports:
- "8080:8080"
- "9990:9990"
and run
docker-compose up
It still boots, but the admin part is not bound to 0.0.0.0 anymore (this is the default behaviour for the image I inherited from).
Why does it stop working when I use the build command in the docker-compose.ml?
EDIT: It seems that it is ignoring all my docker file commands.
run docker-compose build after changing docker-comopse.yml and then docker-compose up
Before you type docker-compose up, you should build images with docker-compose build [options] [SERVICE...].
Options:
--force-rm Always remove intermediate containers.
--no-cache Do not use cache when building the image.
--pull Always attempt to pull a newer version of the image.
In your case, ex: docker-compose build --no-cache web

Resources