skip to Main Content

I have a node project and it has Dockerfile and docker-compose.yml as well.

Dockerfile

FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml

version: '3'

services:
  my-service-name:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:80"
    restart: unless-stopped

I uploaded the image to Docker Hub. When I tried to pull the image and run it, I needed to specify the port like this docker run -p 8080:80 my-username/my-image-name so I can open the project in localhost:8080 from NGINX expose 80.

What I want to do is run the image without specifying the port since I already specified the port in Dockerfile and docker-compose. I’ve been confused with how to achieve this. Does this mean my docker-compose is not uploaded to the Docker Hub and I should do so? Or is my current way is already correct?

2

Answers


  1. When you use a docker-compose file, you have to run it with the docker-compose executable. What you are doing is bypassing the compose file altogether.

    Login or Signup to reply.
  2. You are misinterpreting the meaning of EXPOSE in the Dockerfile. From the documentation:

    The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

    So feel free to run containers without specifying exposed ports on the docker command line or in Docker-Compose or anywhere else The containers will run but it’s like they are behind a firewall.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search