I am trying to build a dockerfile in which i am just starting a http-server.
My dockerfile is:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y nodejs && apt-get install -y npm
COPY testing /testing
RUN cd testing
RUN npm install
ENTRYPOINT npm start
Here testing is the project directory which consist of an index.html file and package.json.
My package.json is:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "http-server -a 0.0.0.0 -p 5500"
},
"author": "",
"license": "ISC",
"dependencies": {
"concurrently": "^5.0.2",
"http-server": "^0.12.1"
}
}
In my local machine the server is working properly. I think the cd testing command isn't appropriate and npm install is not taking the package.json file.
The docker image is formed under the Image ID - 6260786586cc
and the error while running an image through command is:
Use WORKDIR instruction instead of cd.
WORKDIR testing
Each instruction in Dockerfile is executed in a separate container during build. So running cd to change directory during build doesn't work.
Related
I'm trying to deploy my Sveltekit App in Railway
Whenever I try to run
docker run --publish 8000:8000 my_project
I get back
> my-project#0.0.1 build
> vite build
sh: vite: not found
This is my dockerfile
# syntax=docker/dockerfile:1
FROM node:19.4.0-alpine3.16
ENV NODE_ENV=production
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm install
RUN npm install dotenv
RUN npm install #directus/sdk
RUN npm install vite
COPY . .
CMD [ "npm", "run", "build" ]
This is my package.json file
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
"test": "playwright test",
"test:unit": "vitest",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
"dependencies": {
"#directus/sdk": "^10.3.1",
"dotenv": "^16.0.3"
}
}
Script and dependencies above
The devDependencies are the following, at least the ones I found competent
"devDependencies": {
"#sveltejs/adapter-auto": "^1.0.0",
"#sveltejs/kit": "^1.0.0",
"svelte": "^3.54.0",
"svelte-check": "^2.9.2",
"vite": "^4.0.0",
"vitest": "^0.25.3"
},
this is my Dockerfile code
FROM --platform=Linux/X86_64 node:16
RUN apt update
RUN apt -y install build-essential
RUN apt -y install wget
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get -y install google-chrome-stable
# COPY --from=CHORMEBUILDER /usr/bin/google-chrome-stable /usr/bin/google-chrome-stable
# Create app directory
COPY $PWD /crm-connector
WORKDIR /crm-connector
# RUN npm install --location=global npm#8.12.1
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN node -v
RUN npm uninstall chromedriver
RUN npm install
RUN npm install chromedriver --chromedriver-force-download --detect_chromedriver_version
# If you are building your code for production
#RUN npm ci --only=production
# Bundle app source
COPY . .
RUN npm run build
EXPOSE 8081
CMD [ "npm", "start" ]
I'm trying to run this on ECS
I've hosted my code(Docker images) ECR.
I'm getting following error.
selenium error ex Error: Server terminated early with status 2
at /crm-connector/node_modules/selenium-webdriver/remote/index.js:248:24
Build taken machine is: Mac M1
Trying to deploy it on Operating system/Architecture
Linux/X86_64
Task Definition is:
{
"containerDefinitions": [
{
"image":"image-name",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "all-log",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
"name":"task-name",
"portMappings": [
{
"containerPort": 8081,
"hostPort": 8081,
"protocol": "tcp",
}
]
}
],
"cpu": "8192",
"executionRoleArn": "arn:aws:iam::accountId:role/ecsTaskExecutionRole",
"family":"task-family",
"memory": "32768",
"networkMode": "awsvpc",
"runtimePlatform": {
"operatingSystemFamily": "LINUX"
},
"requiresCompatibilities": [
"EC2",
"FARGATE"
]
}
advanced thanks for any help you guys do. :)
It was the architecture difference issue. Building machine was arm64 and ECS X86.
if you build from different architecture, remove node_modules and add FROM platform=dest_architecture node:16 in Dockerfile
Thank you very much for the help
#Mark B
I will try to be as descriptive as I can but I have an react app built with creat-react-app from this app I build a docker image.
dockerfile
FROM node:17-alpine
# set working directory
WORKDIR /app
# install app dependencies
#copies package.json and package-lock.json to Docker environment
COPY package.json ./
RUN apk add --no-cache --virtual .gyp python3 make g++ \
&& yarn install \
&& apk del .gyp
# Copies everything over to Docker environment
COPY . ./
RUN yarn build-docker
#install serve package
RUN yarn global add serve
EXPOSE 3000 5000
ENTRYPOINT ["serve", "-s", "build"]
Which works fine locally with the .env file included in the project. For our production and development we use docker-compose and include the env_file in our docker-compose.yaml
docker-compse.yaml sample
app:
container_name: app
image: {image_source}
restart: always
env_file:
- env/app.env
api:
container_name: api
image: {image_source}
restart: always
env_file:
- env/global.env
- env/db.env
The api works fine since it's using ENTRYPOINT ["npm", "start"] and reload the .env file every time it start, but since the app is built before the docker-compose up -d,
Is their a way to rebuild using yarn build-docker with docker-compose so my new build get the right .env file?
I finally found out how to solve the issue.
Instead of building the app within the container I chained both action in package.json and simply run the build before the serve.
package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build-docker": "react-scripts --openssl-legacy-provider build",
"build-start": "react-scripts --openssl-legacy-provider build && serve -s build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"build-start": "react-scripts --openssl-legacy-provider build && serve -s build"
And in dockerfile i removed the yarn build-docker and changed the entrypoint to ENTRYPOINT ["npm", "run", "build-start"]
dockerfile
FROM node:17-alpine
# set working directory
WORKDIR /app
# install app dependencies
#copies package.json and package-lock.json to Docker environment
COPY package.json ./
RUN apk add --no-cache --virtual .gyp python3 make g++ \
&& yarn install \
&& apk del .gyp
# Copies everything over to Docker environment
COPY . ./
##### REMOVED THE FIRST BUILD
#RUN yarn build-docker
#install serve package
RUN yarn global add serve
EXPOSE 3000 5000
##### CHANGED THE ENTRYPOINT WITH THE NEW SCRIPT
ENTRYPOINT ["npm", "run", "build-start"]
So everytime it goes up with docker-compose the .env is used during the build.
Hope this could help someone!
npm install is taking (30+) minutes in server which runs on debian 9.8.
I want to know what might cause this?
I've already tried npm set progress=false and npm config set registry http://registry.npmjs.org/ but none of it helped.
I'm running it in docker container.
this is my package.json
{
"name": "name",
"version": "1.0.0",
"main": "server/server.js",
"engines": {
"node": ">=6"
},
"scripts": {
"lint": "eslint .",
"start": "nodemon .",
"client": "cd client && npm start",
"test": "mocha"
},
"dependencies": {
"babel-eslint": "^10.0.1",
"compression": "^1.0.3",
"cors": "^2.5.2",
"eslint-config-react-app": "^4.0.0",
"eslint-plugin-flowtype": "^3.7.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.13.0",
"eslint-plugin-react-hooks": "^1.6.0",
"express-jwt": "^5.3.1",
"express-winston": "^3.2.0",
"helmet": "^3.10.0",
"html-element": "^2.3.0",
"jwks-rsa": "^1.5.1",
"libxmljs": "^0.19.5",
"loopback": "^3.22.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^6.2.0",
"loopback-connector-mongodb": "^4.2.0",
"loopback-connector-rest": "^3.4.1",
"node": "8.16.0",
"node-blockly": "^1.0.36",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^3.0.0",
"winston": "^3.2.1"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^3.17.1",
"eslint-config-loopback": "^8.0.0",
"loopback-supertest-models": "^3.5.0",
"mocha": "^6.1.4",
"nodemon": "^1.19.0",
"supertest": "^4.0.2"
},
"repository": {
"type": "",
"url": ""
},
"license": "UNLICENSED",
"description": "description"
}
and this is my Dockerfile
FROM node:10.9.0-alpine as builder
RUN mkdir -p /app/server
WORKDIR /app/server
COPY package*.json /app/server/
#COPY yarn* /app/server/
# Disables npm progress bar
RUN npm set progress=false
# Set npm registry to http://registry.npmjs.org
RUN npm config set registry http://registry.npmjs.org/
#! Install the build requirements for bcrypt
RUN apk update && apk upgrade \
&& apk --no-cache add --virtual git builds-deps build-base \
&& apk --no-cache add make gcc g++ python \
&& npm install --save node-gyp node-pre-gyp \
&& rm -rf /var/cache/apk/*
# && yarn add node-gyp node-pre-gyp
# Install dependencies
# RUN yarn install --production=true
RUN npm install --production
# Copy the server files over
COPY . /app/server/
RUN apk del make gcc g++ python
FROM node:10.9.0-alpine
# Create and set the working directory
RUN mkdir -p /app/server
WORKDIR /app/server
# Copy the server from the build container
COPY --from=builder /app/server /app/server
CMD ["node", "."]
I have simple node.app, with this package.json:
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "xxxxx",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1",
"myuser-deploy": "git+ssh://git#github.com:myuser/deploy.git"
}
}
Normally when I run "npm i", it installs all dependencies, and also the one form the github. I have SSH that I previously setup with the github. All works fine so far.
Now I added Docker support for my application, like this
Dockerfile:
FROM node:carbon
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
docker-compose.yml:
version: "3"
services:
test-service:
build: .
image: test-service
container_name: test-service
working_dir: /usr/src/app
user: node
restart: always
ports:
- '3001:3000'
When I run docker-compose up, it gives me this error:
> npm ERR! Error while executing: npm ERR! /usr/bin/git ls-remote -h -t
> ssh://git#github.com/myuser/deploy.git npm ERR! npm ERR! Host key
> verification failed. npm ERR! fatal: Could not read from remote
> repository. npm ERR! npm ERR! Please make sure you have the correct
> access rights npm ERR! and the repository exists. npm ERR! npm ERR!
> exited with error code: 128
>
> npm ERR! A complete log of this run can be found in:
What can I do with the docker, so it uses my SSH keys?