So, I'm trying to dockerize an AngularJS app for some practice. Here is my repo https://github.com/Nigel33/angularJS_docker_test:
(Its from the official Angular-phonecat repo but i added Dockerfile and docker-compose)
Here's my Dockerfile:
FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
COPY . .
RUN npm install && \
npm run start
EXPOSE 8000
CMD [ "http-server", "dist" ]
Here's my docker-compose.yml:
version: '3'
services:
front:
build: .
command: bash -c "npm start"
volumes:
- .:/angular-phonecat
ports:
- "8000:8000"
When I do docker-compose build then docker-compose up, it seems to run based on the logs:
Building front
Step 1/8 : FROM node:lts-alpine
---> b95baba1cfdb
Step 2/8 : RUN npm install -g http-server
---> Using cache
---> 082793f64510
Step 3/8 : WORKDIR /app
---> Using cache
---> e76e23fa5a08
Step 4/8 : COPY package*.json ./
---> Using cache
---> 9960651e0929
Step 5/8 : COPY . .
---> c06eaebcfb38
Step 6/8 : RUN npm install && npm run start
---> Running in e2622118028d
npm WARN lifecycle angular-phonecat#0.0.0~postinstall: cannot run in wd angular-phonecat#0.0.0 npm run copy-libs (wd=/app)
audited 3777 packages in 6.409s
found 57 vulnerabilities (2 low, 2 moderate, 53 high)
run `npm audit fix` to fix them, or `npm audit` for details
> angular-phonecat#0.0.0 prestart /app
> npm install
npm WARN lifecycle angular-phonecat#0.0.0~postinstall: cannot run in wd angular-phonecat#0.0.0 npm run copy-libs (wd=/app)
audited 3777 packages in 5.338s
found 57 vulnerabilities (2 low, 2 moderate, 53 high)
run `npm audit fix` to fix them, or `npm audit` for details
> angular-phonecat#0.0.0 start /app
> http-server ./app -a localhost -p 8000 -c-1
Starting up http-server, serving ./app
Available on:
http://localhost:8000
Hit CTRL-C to stop the server
But when I open up my browser and navigate to localhost:8000, I get the "this site can't be reached" page. Any idea whats wrong?? Thank you!
UPDATES
Dockerfile
FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
COPY . .
RUN npm install
EXPOSE 8000
CMD ["npm", "run", "start"]
Docker-compose:
version: '3'
services:
front:
build: .
ports:
- "8000:8000"
Terminal
Building front
Step 1/8 : FROM node:lts-alpine
---> b95baba1cfdb
Step 2/8 : RUN npm install -g http-server
---> Using cache
---> 082793f64510
Step 3/8 : WORKDIR /app
---> Using cache
---> e76e23fa5a08
Step 4/8 : COPY package*.json ./
---> Using cache
---> 9960651e0929
Step 5/8 : COPY . .
---> Using cache
---> 35b3086dc43d
Step 6/8 : RUN npm install
---> Using cache
---> 961eddc4df33
Step 7/8 : EXPOSE 8000
---> Using cache
---> ede5977a2a0e
Step 8/8 : CMD ["npm", "run", "start"]
---> Using cache
---> efc2186c9bf2
Successfully built efc2186c9bf2
Successfully tagged angular-phonecat_front:latest
Chriss-Air:angular-phonecat chrisshopline$ docker-compose up
Creating network "angular-phonecat_default" with the default driver
Creating angular-phonecat_front_1 ... done
Attaching to angular-phonecat_front_1
front_1 |
front_1 | > angular-phonecat#0.0.0 prestart /app
front_1 | > npm install
front_1 |
front_1 | npm WARN lifecycle angular-phonecat#0.0.0~postinstall: cannot run in wd angular-phonecat#0.0.0 npm run copy-libs (wd=/app)
front_1 | audited 3777 packages in 4.622s
front_1 | found 57 vulnerabilities (2 low, 2 moderate, 53 high)
front_1 | run `npm audit fix` to fix them, or `npm audit` for details
front_1 |
front_1 | > angular-phonecat#0.0.0 start /app
front_1 | > http-server ./app -a localhost -p 8000 -c-1
front_1 |
front_1 | Starting up http-server, serving ./app
front_1 | Available on:
front_1 | http://localhost:8000
front_1 | Hit CTRL-C to stop the server
Still having the same issue though :-/
The above logs from the Docker build, not from the container. As the build process stuck at
RUN npm install && \
npm run start
You do not need to start application at Dockerfile RUN command. It should be in CMD or Entrypoint
Update your Dockerfile and it should work.
RUN npm install
Also, Something in Docker-compose should be clear.
command: bash -c "npm start"
volumes:
- .:/angular-phonecat
It will override the CMD that is defined in Dockerfile CMD [ "http-server", "dist" ].
It will also override - .:/angular-phonecat. you do not need volume if you already copied the app in the build stage.
update:
I see that you are setting address in your package.json to localhost. It should be 0.0.0.0 or you can set CMD http-server . or http-server -a 0.0.0.0 -p 8000 -c-1. or http-server -d /app/dist/ -a 0.0.0.0 -p 8000 -c-1.
Also, the dist does not exist in your repo, I assume it exist in your build directory where you copy during docker build.
Related
I am trying to dockerize nestjs application. I have to use approach of our devops, so I can't give all details of configuration.
Scripts in package.json typical for any nestjs application.
I have Dockerfile.backend:
FROM some.registry.net/docker/node16 as builder
WORKDIR /opt/app
COPY --chown=app:app ./nestjs/nest-project .
RUN yarn install --non-interactive --production --frozen-lockfile
FROM some.registry.net/docker/node16 as serve
WORKDIR /opt/app
ENV NODE_ENV=production
ENV APP_CONFIG=/opt/app/config/config.yaml
COPY --chown=app:app ./build/Procfile /opt/startup/Procfile
COPY --chown=app:app ./build/config.yaml ./config/config.yaml
COPY --chown=app:app --from=builder /opt/app/ ./
COPY --chown=app:app --from=builder /opt/app/node_modules ./node_modules
USER root
##RUN npm install pm2 -g
##RUN npm install -g nodemon
RUN npm run build
CMD ["/opt/startup/entrypoint.sh"]
And I have docker-compose.yml file:
version: "2"
services:
backend:
build:
context: .
dockerfile: ./build/Dockerfile.backend
command: npm run start
##volumes:
##- ./nestjs/nest-project:/app
##- /app/node_modules
ports:
- 4001:4001
- 9229:9229
environment:
- NODE_ENV=development
- PORT=4001
- REACT_APP_PROD=0
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001
- FRONTEND_URL=http://localhost:4000
- APP_CONFIG=/opt/app/config/config.yaml
frontend:
build: ./frontend
command: npm start
##volumes:
##- ./frontend:/app
##- /app/node_modules
environment:
- NODE_ENV=development
- DISABLE_ESLINT_PLUGIN=true
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001/backend
- PORT=4000
- REACT_APP_PROD=0
ports:
- 4000:4000
So the most interesting point lies in backend command part. I am able to start container only with npm run start:dev command. When I enter there npm start or npm run start:prod
container executes (I see in logs that nestjs app starts, successfully connects to database) and exits without any errors. I tried node dist/main and got the same result. I tried nodemon, with nodemon dist/main nodemon, even with verbose flag shows red line something like app crashed... and gives no more information. I tried pm2 with this command pm2 --name nestjs start npm -- start pm2 successfully starts and container exits without any information.
So far I see problem lies somewhere in my configuration, but I have no clue where to seek. Thanks in advance.
background:
I'm on Ubuntu 20.04.
I'm trying to build images using docker-compose.
This is my docker-compose file
version: "3.8"
services:
web:
build: ./frontend
network_mode: "host"
ports:
- 3000:3000
api:
build: ./backend
network_mode: "host"
ports:
- 3001:3001
environment:
DB_URL: mongodb://db/foo-app
db:
image: mongo:4.0-xenial
ports:
- 27017:27017
volumes:
- foo-app-data:/data/db
volumes:
foo-app-data:
And below are my two Dockerfile files:
# ./backend file
FROM node:16.3.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
USER root
COPY package*.json ./
# --- debug try
RUN npm cache clean --force
RUN npm config set registry https://registry.npmjs.org/
RUN npm install -g #angular/cli
# ---
RUN npm install
COPY . .
RUN chown app:app /app
USER app
EXPOSE 3001
CMD ["npm", "start"]
# ./frontend file
FROM node:16.3.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
USER root
COPY package*.json ./
# --- debug try
RUN npm cache clean --force
RUN npm config set registry https://registry.npmjs.org/
# ---
RUN npm install
COPY . .
RUN chown app:app /app
USER app
EXPOSE 3000
CMD ["npm", "start"]
error:
When I run docker-compose build, the below error is thrown:
Step 8/14 : RUN npm install -g #angular/cli
---> Running in ce221adb18f6
npm ERR! code EAI_AGAIN
npm ERR! syscall getaddrinfo
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/#angular%2fcli failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-06-13T10_19_13_600Z-debug.log
The command '/bin/sh -c npm install -g #angular/cli' returned a non-zero code: 1
ERROR: Service 'web' failed to build : Build failed
what i have tried so far:
when I was building each docker file manually, I was getting the same error, until I used the --network=host. It means, when I build the images with docker build --network=host, it works fine. So I tried to add network_mode= "host" to my docker-compose file but it doesn't solve the issue.
and for God's sake, read this before marking this question as duplicate:
this post here propose a solution for docker and not docker compose. When I ping registry.npmjs.org, the connection works fine.
this post here propose to docker-compose up, which will throw the exact same error as I have here.
this post here doens't work, i have already restarted docker multiple times. And on top of that, I clean all docker images after the error is thrown to make sure the next time I build, nothing is used from the cache.
this post here doesn't work either. I tried to (1) remove the proxies from the npm config, and also add the additional lines npm cache clean --force and npm config set registry https://registry.npmjs.org/ in my Dockerfile. Nothing works.
this post here not only doesn't solve the problem, but also it doesn't really explain well the reason why the solution is being proposed.
and this post here i don't even know how this answer is allowed on StackOverflow.
If you use network_mode with host value, you can't mix it with port mapping: https://docs.docker.com/compose/compose-file/compose-file-v3/#ports
Then, or you change your network_mode to bridge or you drop your port mapping for each service.
Add following Variable to .zshrc file in mac
export NODE_OPTIONS=--openssl-legacy-provider
I am trying to make a simple react app in docker-compose. I am using this reference What I have done is run npx create-react-app frontend to generate the default react app. Then I added the Dockerfile. This is all in a directory frontend.
#Dockerfile
FROM node:14.9
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts#3.4.1 -g --silent
# add app
COPY . ./
# start app
CMD ["npm", "start"]
In the directory above, I have my docker-compose
#docker-compose.yml
version: '3.7'
services:
frontend:
container_name: frontend
build: ./frontend
volumes:
- './frontend:/app'
- '/app/node_modules'
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
I run docker-compose up --build and after the normal build process I get this message.
docker-compose up --build
Building frontend
Step 1/9 : FROM node:14.9
---> 1b2c72215052
Step 2/9 : WORKDIR /app
---> Using cache
---> 2bab04404275
Step 3/9 : ENV PATH /app/node_modules/.bin:$PATH
---> Using cache
---> ff4ce5f5ec47
Step 4/9 : COPY package.json ./
---> Using cache
---> b1d25c0b6c05
Step 5/9 : COPY package-lock.json ./
---> Using cache
---> 5b829feaf00d
Step 6/9 : RUN npm install --silent
---> Using cache
---> 835367c47253
Step 7/9 : RUN npm install react-scripts#3.4.1 -g --silent
---> Using cache
---> 015ccb2db237
Step 8/9 : COPY . ./
---> Using cache
---> e4a5285339b5
Step 9/9 : CMD ["npm", "start"]
---> Using cache
---> 3f91b16d34d6
Successfully built 3f91b16d34d6
Successfully tagged projectname_frontend:latest
Recreating frontend ... done
Attaching to frontend
frontend |
frontend | > frontend#0.1.0 start /app
frontend | > react-scripts start
frontend |
frontend | ℹ 「wds」: Project is running at http://172.29.0.2/
frontend | ℹ 「wds」: webpack output is served from
frontend | ℹ 「wds」: Content not from webpack is served from /app/public
frontend | ℹ 「wds」: 404s will fallback to /
frontend | Starting the development server...
frontend |
frontend exited with code 0
It seems the container just exits without an error message. I have no idea what is causing this.
Docker version 19.03.12, build 48a66213fe
It is worth noting that I have been able to build my react apps successfully without the solutions below for the past few months. The need for the commands given in the solution has only recently become an issue for me.
I think I solved the issue. Adding stdin_open: true to the docker-compose.yml was half the solution. I also added command: npm start. After this, the container stopped exiting. I thought that the command in the Dockerfile would be sufficient. It seems to be working now.
docker-compose.yml
version: '3.7'
services:
frontend:
container_name: frontend
build: ./frontend
volumes:
- './:/app'
- '/app/node_modules'
ports:
- 3000:3000
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
command: npm start
Dockerfile
FROM node:14.9
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]
This simple Dockerfile always works for me:
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8083
CMD [ "npm", "start" ]
I also like to add a stdin_open: true to my container in docker-compose.yml.
version: "3.8"
services:
frontend:
build: .
container_name: your-container-name
ports: 4300:4300
volumes:
- ./src:/app/src
- ./public/assets:/app/public/assets
- /app/node_modules
stdin_open: true
tty: true
env-file:
- location/to/your/env-file
I can't comment to Seth Faulkner's solution because of lacking reputation.
I added the stdin_open: true to the docker-compose at first and it still gives the exit with code 0 issue.
However, adding the command: yarn run start in the docker-compose solve the problem for me
stdin_open: true
command: yarn run start
I was getting started with Docker, created couple of tiny Express(NodeJS) services.
Plan is to run the microservices inside Docker containers and then establish a inter-communication between them using Docker Compose service names.
Here is the Github repo of this simple project. Am able to build images with below commands :
cd books
docker build -t node-micro/books .
cd auth
docker build -t node-micro/auth .
Commands to start containers :
docker run -d -p 6677:6677 node-micro/auth
docker run -d -p 7766:7766 node-micro/books
But when i hit below URL's there is no response, which was working fine couple of day's before :
http://localhost:6677/
http://localhost:7766/
And have no clue what's happening with docker compose. No luck on accessing same URL's as mentioned above after stoping all containers, delete all images & ran this command :
docker-compose up -d
Need some help on bringing up the containers individually and also through docker-compose.
I can see in each of your micro-service, your application is running on ports 3000 in the container but you are exposing 7766 and 6677 in your docker-compose.yml
Please check the below docker-compose.yml
version: '3'
services:
books:
build: './books'
ports:
- "7766:3000"
depends_on:
- auth
auth:
build: './auth'
ports:
- "6677:3005"
and then run the below command
docker-compose up --build
--build will build the images as well.
Then, you should be able to access the service
http://localhost:6677/
http://localhost:7766/
Output
docker-compose up --build
Creating network "node_microservices_default" with the default driver
Building auth
Step 1/7 : FROM node:10-alpine
---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
---> Running in a1dc67b70538
Removing intermediate container a1dc67b70538
---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
---> 454f1b7aba87
Step 4/7 : RUN npm install
---> Running in a24eea8b79d4
npm WARN auth#1.0.0 No description
npm WARN auth#1.0.0 No repository field.
added 50 packages from 37 contributors and audited 50 packages in 8.58s
found 0 vulnerabilities
Removing intermediate container a24eea8b79d4
---> 31b31ff4516e
Step 5/7 : COPY . .
---> 1eeaa8e70300
Step 6/7 : EXPOSE 3000
---> Running in fc798167dbcd
Removing intermediate container fc798167dbcd
---> 4d964d25c099
Step 7/7 : CMD ["npm", "start"]
---> Running in 3c28d92f9ef6
Removing intermediate container 3c28d92f9ef6
---> 514f68d11d7c
Successfully built 514f68d11d7c
Successfully tagged node_microservices_auth:latest
Building books
Step 1/7 : FROM node:10-alpine
---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
---> Using cache
---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
---> 56addb6c75a5
Step 4/7 : RUN npm install
---> Running in 4864fb7a171c
npm WARN books#1.0.0 No description
npm WARN books#1.0.0 No repository field.
added 50 packages from 37 contributors and audited 50 packages in 5.111s
found 0 vulnerabilities
Removing intermediate container 4864fb7a171c
---> 82bb2cd54357
Step 5/7 : COPY . .
---> 12893a93e82e
Step 6/7 : EXPOSE 3000
---> Running in 1301e29dbd52
Removing intermediate container 1301e29dbd52
---> c26948ebcb3b
Step 7/7 : CMD ["npm", "start"]
---> Running in db948866a121
Removing intermediate container db948866a121
---> 703b901d7bc4
Successfully built 703b901d7bc4
Successfully tagged node_microservices_books:latest
Creating node_microservices_auth_1 ... done
Creating node_microservices_books_1 ... done
Attaching to node_microservices_auth_1, node_microservices_books_1
auth_1 |
auth_1 | > auth#1.0.0 start /usr
auth_1 | > node index.js
auth_1 |
auth_1 | Running on port 3005
auth_1 | --------------------------
books_1 |
books_1 | > books#1.0.0 start /usr
books_1 | > node index.js
books_1 |
books_1 | Running on port 3000
books_1 | --------------------------
You are exposing from both Dockerfiles the port 3000. Replace the port for each microservice in docker-compose.yml file.
- "7766:3000"
- "6677:3000"
The mapping for ports is wrong. In both Dockerfile you are exposing the port 3000.
So you must to map the ports 6677 and 7766 to the exposed port on the Dockerfile.
To fix this, on your docker-compose.yml you must to config ports like this:
version: '3'
services:
books:
build: './books'
ports:
- "7766:3000"
depends_on:
- auth
auth:
build: './auth'
ports:
- "6677:3000"
The build folder is ready to be deployed.
You may serve it with a static server:
serve -s build
---> b252a9088991
Removing intermediate container cb5c1e2629c9
Step 16/16 : RUN serve -s build
---> Running in c27b54b31108
serve: Running on port 5000
created and dockerized react application using react-app and getting output like above on "docker-compose up" command
but nothing is showing on http://0.0.0.0:5000/ or http://localhost:5000/
version: '3'
services:
web:
build: .
image: react-cli
container_name: react-cli
volumes:
- .:/app
ports:
- '3000:3000'
above my docker-compose.yml file
FROM scratch
FROM mhart/alpine-node:6.12.0
RUN npm install -g npm --prefix=/usr/local
RUN ln -s -f /usr/local/bin/npm /usr/bin/npm
CMD [ "/bin/sh" ]
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install -g serve
CMD serve -s build
EXPOSE 3000
COPY package.json package.json
COPY semantic.json semantic.json
COPY npm-shrinkwrap.json npm-shrinkwrap.json
RUN npm install gulp-header --save-dev
RUN npm install --no-optional
COPY . .
RUN npm run build --production
RUN serve -s build
and this is my Dockerfile
Most likely you are not exposing container port on host. I don't see how your compose file looks, but you probably need to add
ports:
- "5000:5000"
To your container definition in compose file.