1

I am attempting to get a Vue app running locally inside a Docker container and having an issue publishing to a specified port.

Here is my Dockerfile

FROM node:lts-alpine

RUN mkdir -p /app
COPY . /app
WORKDIR /app

RUN npm install
RUN npm run build

EXPOSE 3000

CMD ["npm", "start"]

In the root directory of my project I run

docker build --tag projectname .

This successfully creates an image which I should be able to run in a container.
However, whenever I run the following command I am unable to access the container from my browser at any port.

docker run -p 3000:3000 --name projectname projectname

The output shows some recommendations on splitting code to reduce size, but there are no errors, and it states that I should be able to access the app from http://localhost:8080, but that page provides a connection refused error.

I am under the impression that the publish option should listen on the exposed port 3000 and forward traffic to local port 3000.
However, this doesn't appear to be happening.

I am running Docker for Windows which might also be part of the problem.

  • I don't understand why do you use `http://localhost:8080` to access port 3000. Are there two apps? Otherwise, why don't access `http://localhost:3000` in your browser? Third, if you execute your node code in your host, does it work? – Alejandro Galera Aug 18 '20 at 19:58
  • the exposed port is 3000, so the -p 3000:3000 flag should cause the container to listen on http://localhost:3000. I am not wanting to use 8080, it just what is printed in the output. Running the code locally (outside of Docker) it works just fine. – Brad Chandler Aug 18 '20 at 20:00

1 Answers1

1

Try running this command docker run -p 3000:8080 --name projectname projectname and then access the app at localhost:3000 on the docker host machine.

If this works, then either update in your Dockerfile the EXPOSE 3000 into EXPOSE 8080, or start your http server on port 3000 instead of 8080 inside your app. This second step is optional, but it will help other folks understand that the containers launched from that image are supposed to be listening on the port that is mentioned in the Dockerfile.

Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • this definitely worked! One question, when I expose 3000 in the Dockerfile, shouldn't using the -p 3000:3000 tell the docker run to listen on port 3000? It seems like the container will use 8080 as the default no matter what. – Brad Chandler Aug 18 '20 at 20:05
  • EXPOSE 3000 is just an info. In other words, just a way of telling other developers who will use that image that it is supposed to listen on that port. – Neo Anderson Aug 18 '20 at 20:06