I have a super simple node.js file that runs perfectly fine outside of docker, but throws the following syntax error when I try to run it in a container:
./app/index.js: line 1: syntax error: unexpected word (expecting ")")
index.js is literally one line:
console.log("HI");
My Docker file is:
FROM node:14-alpine
WORKDIR /usr/app/
COPY . ./
CMD yarn start
I'm running the same command inside and out, same version of node etc. I'm on Windows 10 using the WSL 2 backed docker desktop. Could any of that have something to do with it? I'm going nuts...
Its working fine with me.
index.js
console.log("HI");
Dockerfile
FROM node:14-alpine
WORKDIR /usr/app/
COPY . ./
CMD yarn start
package.json
{
"name": "my-package",
"scripts": {
"start": "node index.js"
}
}
docker-compose.yml
version: '3.7'
services:
node_v14_test:
build:
context: .
dockerfile: Dockerfile
Then build and run docker container with docker compose.
docker-compose up --build
Result
try this
FROM node:14-alpine
WORKDIR /usr/app/
COPY . .
CMD [ "yarn", "start" ]
Seems like you need to convert file from dos to Linux issue or might the case image is using wrong code?
But the simplest way to debug that issue is not with Docker environment is to generate file inside Dcokerfile
FROM node:14-alpine
WORKDIR /usr/app/
COPY package.json .
RUN echo "console.log(\"HI\")" >> index.js
CMD yarn start
Related
I have an index.ts, then this is my script
"scripts": {
"test": "jest",
"start": "ts-node-dev ./index.ts"
},
I tried to dockerize it, what should I do? do I need to add another command for npm build? or generate a .js file?
my dockerfile like this
FROM node:10-alpine
WORKDIR /
# copy configs to /app folder
COPY package*.json ./
COPY tsconfig.json ./
COPY . .
# check files list
RUN ls -a
RUN npm install
EXPOSE 3001
CMD [ "npm", "start"]
I can't access localhost:3001 in my browser after I run
docker build -t testApp .
then
docker run -p 80:3001 testApp
I try to run a container from a built image and get an error /bin/sh: =:. Is there anyone know how to fix it. Below is my Dockerfile. Both the index.js and package.json are in the same folder with the Dockerfile.
package.json
{
"dependencies":{
"express":"*"
},
"scripts":{
"start":"node index.js"
}
}
Dockerfile
FROM node:14.4.0-alpine3.10
COPY ./ ./
RUN npm install
CMD = ['npm', 'start']
The input to CMD is parsed as a JSON array, which means you have to use double quotes and not single quotes. Additionally, get rid of the =
CMD ["npm", "start"]
Need an advice to dockerize and run a node JS static-content app on K8s cluster.
I have a static web-content which I run "npm run build” into the terminal which generates /build and direct my IIS webserver to /build/Index.html.
Now, I started creating a Docker file, how do I point my nodeJS image to invoke /build/Index.html file
FROM node:carbon
WORKDIR /app
COPY /Core/* ./app
npm run build
EXPOSE 8080
CMD [ "node", ".app/build/index.html" ]
Please how can I run this app only on node v8.9.3 and
npm 5.6.0 ?
Any inputs please ?
You can specify the version of node specifically:
FROM node:8.9.3
Assumptions:
package.json is under Code directory.
npm run build will be running outside of the container and a build directory will be created in Code directory.
We will copy the whole Code/build directory under /app directory of the container.
We will copy package.json to /app folder and will run the website through scripts available in package.json file.
Solution:
I would say add a script named start in the package.json and call that script from Dockerfile's CMD command. The script would look like:
"scripts": {
"start": "node ./index.html",
},
And the Dockerfile would look like:
FROM node:8.9.3
# Make app directory in the container.
RUN MKDIR /app
# Copy whole code to app directory.
COPY Code/build/ /app
# Copy package.json app directory.
COPY package.json /app
# make app directory as the working directory.
WORKDIR /app
# Install dependencies.
RUN npm install -only=production
# Expose the port
EXPOSE 8080
# Start the process
CMD ["npm", "start"]
I'm having trouble with running Docker (I'm a total beginner at it) with my React app. This was given as a coding challenge so I was given these Docker commands:
docker build --tag app-name .
docker run -it -p 7357:80 app-name
Here is my dockerfile:
FROM node:6-wheezy
WORKDIR /srv/tenor-frontend-test/
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 80
ENV NODE_JS_PORT=80
CMD [ "npm", "start" ]
My start script in package.json:
"start": "PORT=7357 react-scripts start"
Using "npm start" in my terminal opens up localhost fine but when I run the "docker run" command, it prints exactly what happens with "npm start" but no browser opens and localhost:7357 doesn't work.
I'm hoping this is just a dockerfile issue since "npm start" works fine?
Thanks in advance!
The app I'm making is written in ES6 and other goodies is transpiled by webpack inside a Docker container. At the moment everything works from creating the inner directory, installing dependencies, and creating the compiled bundle file.
When running the container instead, it says that dist/bundle.js does not exist. Except if I create the bundle file in the host directory, it will work.
I've tried creating a volume for the dist directory at it works the first time, but after making changes and rebuilding it does not pick up the new changes.
What I'm trying to achieve is having the container build and run the compiled bundle. I'm not sure if the webpack part should be in the Dockerfile as a build step or at runtime since the CMD ["yarn", "start"] crashes but RUN ["yarn", "start"] works.
Any suggestions ands help is appreciated. Thanks in advance.
|_src
|_index.js
|_dist
|_bundle.js
|_Dockerfile
|_.dockerignore
|_docker-compose.yml
|_webpack.config.js
|_package.json
|_yarn.lock
docker-compose.yml
version: "3.3"
services:
server:
build: .
image: selina-server
volumes:
- ./:/usr/app/selina-server
- /usr/app/selina-server/node_modules
# - /usr/app/selina-server/dist
ports:
- 3000:3000
Dockerfile
FROM node:latest
LABEL version="1.0"
LABEL description="This is the Selina server Docker image."
LABEL maintainer="AJ alvaroo#selina.com"
WORKDIR "/tmp"
COPY ["package.json", "yarn.lock*", "./"]
RUN ["yarn"]
WORKDIR "/usr/app/selina-server"
RUN ["ln", "-s", "/tmp/node_modules"]
COPY [".", "./"]
RUN ["yarn", "run", "build"]
EXPOSE 3000
CMD ["yarn", "start"]
.dockerignore
.git
.gitignore
node_modules
npm-debug.log
dist
package.json
{
"scripts": {
"build": "webpack",
"start": "node dist/bundle.js"
}
}
I was able to get a docker service in the browser with webpack by adding the following lines to webpack.config.js:
module.exports = {
//...
devServer: {
host: '0.0.0.0',
port: 3000
},
};
Docker seems to want the internal container address to be 0.0.0.0 and not localhost, which is the default string for webpack. Changing webpack.config.js specification and copying that into the container when it is being built allowed the correct port to be recognized on `http://localhost:3000' on the host machine. It worked for my project; hope it works for yours.
I haven't included my src tree structure but its basically identical to yours,
I use the following docker setup to get it to run and its how we dev stuff every day.
In package.json we have
"scripts": {
"start": "npm run lint-ts && npm run lint-scss && webpack-dev-server --inline --progress --port 6868",
}
dockerfile
FROM node:8.11.3-alpine
WORKDIR /usr/app
COPY package.json .npmrc ./
RUN mkdir -p /home/node/.cache/yarn && \
chmod -R 0755 /home/node/.cache && \
chown -R node:node /home/node && \
apk --no-cache add \
g++ gcc libgcc libstdc++ make python
COPY . .
EXPOSE 6868
ENTRYPOINT [ "/bin/ash" ]
docker-compose.yml
version: "3"
volumes:
yarn:
services:
web:
user: "1000:1000"
build:
context: .
args:
- http_proxy
- https_proxy
- no_proxy
container_name: "some-app"
command: -c "npm config set proxy=$http_proxy && npm run start"
volumes:
- .:/usr/app/
ports:
- "6868:6868"
Please note this Dockerfile is not suitable for production it's for a dev environment as its running stuff as root.
With this docker file there its a gotcha.
Because alpine is on musl and we are on glib if we install node modules on the host the compiled natives won't work on the docker container, Once the container is up if you get an error we run this to fix it (its a bit of a sticking plaster right now)
docker-compose exec container_name_goes_here /bin/ash -c "npm rebuild node-sass --force"
ikky but it works.
Try changing your start script in the package.json to perform the build first (doing this, you won't need the RUN command to perform the build in your Dockerfile:
{
"scripts": {
"build": "webpack",
"start": "webpack && node dist/bundle.js"
}
}