In a project I have this Dockerfile:
FROM node:6.9.4
RUN npm install -g cordova#4.2.0 ionic#2.2.1
ENV DOCKER_CONTAINER_APP=/web-app
RUN mkdir -p $DOCKER_CONTAINER_APP
ADD . $DOCKER_CONTAINER_APP
WORKDIR $DOCKER_CONTAINER_APP
EXPOSE 8100 35729
RUN echo "ready to go!"
I am using docker-compose, and this is the docker-compose yml file I use in my project:
version: '2'
services:
web:
build:
context: .
environment:
- NODE_ENV=development
- DEBUG='true'
ports:
- 8100:8100
- 35729:35729
volumes:
- .:/web-app
- ./node_modules:/web-app/node_modules
command: sh -c 'npm install; ionic serve --all'
stdin_open: true
All works well, this is the output of a docker-compose run web command:
[10:53:11] ionic-app-scripts 1.0.0
[10:53:18] watch started ...
[10:53:18] build dev started ...
[10:53:18] clean started ...
[10:53:18] clean finished in 57 ms
[10:53:18] copy started ...
[10:53:18] transpile started ...
[10:53:36] transpile finished in 17.96 s
[10:53:36] webpack started ...
[10:53:37] copy finished in 19.39 s
[10:53:51] webpack finished in 15.10 s
[10:53:51] sass started ...
[10:53:56] sass finished in 4.90 s
[10:53:56] build dev finished in 38.18 s
[10:53:57] watch ready in 39.27 s
[10:53:57] dev server running: http://localhost:8100/
But the native ionic livereload does not work. How can I use the Livereload with this ionic docker image ?
When I had similar issue I'd noticed in browser failed attempts to contact port 53703. Here is screenshot:
Chrome developer tools window
Container I used at that moment had been created with command
docker run -i -t -d --name ionic-dev -v /home/timur/Work/:/Work/ \
-p 8100:8100 -p 35729:35729 ionic-dev
So I stopped and deleted it
docker stop ionic-dev
docker rm ionic-dev
And created another container with command (notice published port 53703)
docker run -i -t -d --name ionic-dev -v /home/timur/Work/:/Work/ \
-p 8100:8100 -p 35729:35729 -p 53703:53703 ionic-dev
After that livereload started to work for me.
Related
I have an api in a docker container mounted in EBS. Suddenly after a new deploy I started getting this error in EBS even after rolling back to a stable deploy (it does work in local):
yarn run v1.22.19
backend | $ sequelize db:migrate
backend | /bin/sh: sequelize: not found
This is my DockerFile:
FROM node:16-alpine
WORKDIR /app
COPY /app/package.json .
COPY /app/yarn.lock .
RUN yarn install
COPY /app .
COPY ./docker/production/node/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
CMD [ "/start" ]
The start script:
#!/bin/sh
set -o errexit
set -o pipefail
set -o nounset
yarn db:migrate
yarn start
and the docker-compose.yml file:
version: '3.9'
volumes:
node_modules: {}
services:
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
volumes:
- ./docker/production/nginx:/etc/nginx/conf.d
depends_on:
- app
links:
- app
app:
build:
context: .
dockerfile: ./docker/production/node/Dockerfile
container_name: tralud_backend
restart: on-failure
volumes:
- ./app:/app
- node_modules:/app/node_modules
env_file:
- .env
ports:
- "3000:3000"
I tried running the same code version locally and it does work, it seems that the build it's being done properly.
I tried creating an new EBS env with the the old stable deploy and it does work, but then I start getting the same error once I try to install new dependencies (cors module).
I am pretty new to Docker and I want to dockerize my react app, the index.html file is under my public folder in the react project. When I run the docker image, it fails and gives me an error stating that the index.html file is missing.
The error:
flash#0.1.0 start
react-scripts start
Could not find a required file. Name: index.html Searched in:
/app/public npm notice npm notice New minor version of npm available!
8.11.0 -> 8.19.2 npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.19.2 npm notice Run npm install -g npm#8.19.2 to update! npm notice
Below is the code of my Dockerfile:
FROM node:lts-alpine3.14 as build
RUN apk update && \
apk upgrade && \
apk add --no-cache bash git openssh
RUN mkdir /app
WORKDIR /app
COPY package.json .
RUN npm install -g --force npm#latest typescript#latest yarn#latest
RUN npm install
COPY . ./
RUN npm run build
# ---------------
FROM node:lts-alpine3.14
RUN mkdir -p /app/build
RUN apk update && \
apk upgrade && \
apk add git
WORKDIR /app
COPY --from=build /app/package.json .
RUN yarn install --production
COPY --from=build . .
EXPOSE 3000
EXPOSE 3001
ENV SERVER_PORT=3000
ENV API_PORT=3001
ENV NODE_ENV production
CMD ["npm", "start"]
Try to attach the shell to the container with
docker exec -it CONTAINER_NAME bash
and see where is index.html file and where you need to copy it
I have reactjs project that need to be run on the linux server. Whenever I run the app on server using "sudo npm run lin_stage_server" it seems to be working fine on 80 port.
when I tried to run app through pm2 with command "sudo pm2 start npm -- lin_stage_server" its not working for me.
In addition to the above command i also tried
"sudo pm2 start npm -- BRANCH_ENV=stage NODE_ENV=production PORT=80 node appserverstart.js
"
Please help !
----- Error details ------
Error details
----- Package.json -----
Package.json
----- appserverstart.js -----
appserverstart.js
-------Additional info-------
server: CentOS Linux version 7 core,
npm version: 6.4.1,
pm2 version: 3.5.1,
node version: 8.11.1,
You are not passing run to npm:
pm2 start npm -- run lin_stage_server
pm2 start npm -- lin_stage_server is effectively npm lin_stage_server.
pm2 start npm -- run lin_stage_server is effectively npm run lin_stage_server
I am trying to setup an API and using docker/docker-compose at the same time but I am having a problem with nodemon(https://nodemon.io/).
At the moment, I can run the command: docker-compose build && docker-compose up and access API, but the problem is when I go to the code and change something and save, my nodemon don't reload/restart.
I was looking into the stackoverflow about this problem and some topics recommend to add "--legacy-watch" on the command to run, for me this isn't working properly.
Any idea how to fix this?
I can provide a github access with the simple API.
Some setting:
Folder structure:
docker-compose.yml -> file
weather-api -> nodeJS application
Dockerfile
src
...
Docker compose configuration:
version: '3'
services:
weather:
build:
context: ./weather-api
dockerfile: Dockerfile
command: npm run dev
ports:
- '3000:3000'
volumes: ['./weather-api/', '/usr/src/app/']
Docker file configuration:
FROM node:10.13-alpine
WORKDIR /usr/src/app
COPY ["package.json", "tsconfig.json", "./"]
RUN npm install
COPY . .
EXPOSE 3000
CMD npm run dev
Package json config
...
"scripts": {
"start": "node dist/app.js",
"lint": "tslint -c tslint.json -p tsconfig.json --fix",
"dev": "nodemon --legacy-watch src/server.ts",
"build": "tsc -b"
}
...
I created a docker container to run tasks with gulp.
All tasks are running, the problem is I can't enable livrereload in Chrome although I exposed the 35729 port in my container.
Here is the Dockerfile :
FROM ubuntu:latest
MAINTAINER jiboulex
EXPOSE 80 8080 3000 35729
RUN apt-get update
RUN apt-get install curl -y
RUN apt-get install software-properties-common -y
RUN add-apt-repository ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get install nodejs -y
RUN curl -L https://www.npmjs.com/install.sh | sh
RUN npm install --global gulp -y
# overwrite this with 'CMD []' in a dependent Dockerfile
CMD ["/bin/bash"]
I create the image with the following command :
docker build -t gulp_image .
I create a container :
docker run --name=gulp_container -i -t --rm -v /var/www/my_app:/var/www/my_app:rw gulp_image bash
then in my container
cd /var/www/my_app
gulp
Here is my Gulpfile.js
var gulp = require('gulp'),
livereload = require('gulp-livereload'),
exec = require('child_process').exec;
gulp.task('js', function() {
gulp.src([
'./src/js/*.js'
]).pipe(livereload());
});
gulp.task('watch', function(){
var onChange = function (event) {
console.log('File '+event.path+' has been '+event.type);
};
livereload.listen();
gulp.watch([
'./src/js/*.js'
], ['js'])
.on('change', onChange);
});
gulp.task('default', ['watch', 'js']);
When I edit a js file, I can see in my container that the files are processed but when I try to enable live reload in my browser (Chrome), I got the following message : "Could not connect to LiveReload server.."
Anyone got a clue about what I missed or didn't do ?
Thanks for reading !
Exposing ports in a container does not imply that the ports will be opened on the docker host. You should be using the docker run -p option. The documentation says:
-p=[] : Publish a container᾿s port or a range of ports to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
Both hostPort and containerPort can be specified as a range of ports.
When specifying ranges for both, the number of container ports in the range must match the number > of host ports in the range. (e.g., -p 1234-1236:1234-1236/tcp)
(use 'docker port' to see the actual mapping)
Since you tried the -p containerPort form, the actual port opened on your host (Linux mint) was randomly chosen by docker when you run the docker run command. To figure out what port was chosen, you have to use the docker port command.
Since this is not convenient, you should use the -p hostPort:containerPort form, and specify that hostPort is 35729. (I also assume you expect ports 80, 8080 and 3000 to be accessible in the same manner)
The command to run your container would then be:
docker run --name=gulp_container -i -t --rm \
-v /var/www/my_app:/var/www/my_app:rw \
-p 35729:35729 \
-p 80:80 \
-p 8080:8080 \
-p 3000:3000 \
gulp_image bash
An easier way to deal with ports is to run your docker container in host networking mode. In this mode, any port opened on the container is in fact opened on the host network interface (they are actually both sharing the same interface).
You would then start your container with:
docker run --name=gulp_container -i -t --rm \
-v /var/www/my_app:/var/www/my_app:rw \
--net=host \
gulp_image bash