2

My aim is to dockerize my simple angular website and test it locally. Given below is the Dockerfile.

# STEP 1 build static website
FROM node:alpine as builder
RUN apk update && apk add --no-cache make git

# Create app directory
WORKDIR /app

# Install app dependencies
COPY package.json package-lock.json  /app/
RUN cd /app && npm set progress=false && npm install

# Copy project files into the docker image
COPY .  /app
RUN cd /app && npm run build

# STEP 2 build a small nginx image with static website
FROM nginx:alpine

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

## From 'builder' copy website to default nginx public folder
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

I'm executing following commands on top of this Dockerfile.

docker build .
docker tag a585f0653f3b abameerdeen/website:latest
docker run -p 80:8000 abameerdeen/website:latest

docker ps

Gives following result.

enter image description here

Still, I;m unable to access the website on http://localhost:8000.

I tried following solutions, but result won't change. I'm on an ubuntu machine which has nginx installed locally too.

EXPOSE 80 in Dockerfile and docker run -p 80:8000 abameerdeen/website:latest

EXPOSE 80 in Dockerfile and docker run abameerdeen/website:latest

This is the status of ports in my local machine. enter image description here

Edit :Updated port numbers.

6
  • Nginx serves on port 80 by default. Map port 8000 to 80 and it would work. Commented Dec 30, 2018 at 12:37
  • docker run -p 8000:80 -d abameerdeen/website:latest Commented Dec 30, 2018 at 12:37
  • @SaqibAhmed, tried it. I'm getting this error. docker: Error response from daemon: driver failed programming external connectivity on endpoint zealous_lalande (c3f908e6a0c287bcfde63b8eec09f026b58f070613b4af981ecb2d4a2cd2d607): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use. Commented Dec 30, 2018 at 12:53
  • I change the EXPOSE 8000 line into EXPOSE 80. Then tried below command.. Commented Dec 30, 2018 at 12:55
  • docker run -p 80:8000 abameerdeen/website:latest , and also docker run abameerdeen/website:latest. Commented Dec 30, 2018 at 12:57

2 Answers 2

3

I kept changing the Dockerfile and different base images, following worked finally. I removed the whole nginx thing and accomplished this on node. But this isn't the ideal solution because I wanted a lightweight image. If someone can suggest the ideal solution, would accept it as the answer.

# base image
FROM node:9.6.1

# install chrome for protractor tests
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install -g @angular/[email protected]

# add app
COPY . /usr/src/app

#expose the port
EXPOSE 5000

# start app
CMD ng serve --port 5000 --host 0.0.0.0

Then,

docker build . -t my_image_tag
docker run -p 5000:5000 my_image_tag

Actually, @Saddam Pojeee's comment triggered me to bash into the container and see if the files are really there. Files weren't there, so I changed the image.

Sign up to request clarification or add additional context in comments.

Comments

0

the nginx image exposes port 80 by default so the EXPOSE 8000 is not necessary... but you will need to map your local port 8000 to the port 80 of the container.

So the correct command for running it is: docker run -p 8000:80 abameerdeen/website:latest this will map the container's port 80 to your local port 8000.

Now you should be able to access http://localhost:8000... If you copied your files to the right location inside the container you will see the webpage. If not you should receive a http 404 from nginx.

4 Comments

Tried this already. I'm getting "docker: Error response from daemon: driver failed programming external connectivity on endpoint compassionate_ride (41504c9455fdd45f065c7a892b1429278ecbf3aee511127def29cc360ed43681): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use. ERRO[0002] error waiting for container: context canceled :"
I already have nginix installed and it's listening on port 80.
I tried stopping nginx and running the app, didn't work.
are you sure you're not running anything else in your container? Do you have the port mapping in the right order... i.e. -p 8000:80

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.