Docker environment/PythonPath issue - docker

I'm creating the following Dockerfile:
FROM node:current-alpine as builder
COPY /webapp/public/* public/
COPY /webapp/src/* src/
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN set NODE_OPTIONS=--openssl-legacy-provider
ENV PYTHONPATH="/webapp"
RUN npm install
CMD ["npm","start"]
EXPOSE 80
based on this directory.
I'm getting the following output from docker logs: /src/App.js Module not found: Can't resolve './Configure/ConfigurationPage' in '/src'. Configuration page is a file located here.
Notice this line in my dockerfile:
ENV PYTHONPATH="/webapp"
I added this line based on a post with someone having a similar issue:
ModuleNotFoundError in Docker.
As per the post, this issue most likely has to do with the environment not being configured correctly, specifically ENV PYTHONPATH not being correctly defined. The ENV PYTHONPATH="/webapp" line in my Dockerfile didn't fix the issue. What else can I try to fix this?

Related

Dockerizing python project Dockerfile creation

This question is asked before yet After reviewing the answers I am still not able to copy the solution.
I am still new to docker and after watching tutorials and following articles I was able to create a Dockerfile for an existing GitHub repository.
I started by using the nearest available image as a base then adding what I need.
from what I read the problem is in WORKDIR and CMD commands
This is error message:
python: can't open file 'save_model.py': [Errno 2] No such file or directory*
This is my Dockerfile:
# syntax=docker/dockerfile:1
FROM tensorflow/serving:2.3.0-rc0-devel-gpu
WORKDIR app
COPY requirements-gpu.txt .
# install dependencies
RUN pip install -r requirements-gpu.txt
# copy the content of the local src directory to the working directory
COPY /home/pc/Desktop/yolo4_deep .
# command to run on container start
CMD ["python","./app/save_model.py","./app/object_tracker.py" ]
src
save_model.py
object_tracker.py
...
requirements.txt
Dockerfile
I tried WORKDIR command to set the absolute path: WORKDIR /home/pc/Desktop/yolo4_Deep_sort_nojupitor the result was Same Error.
I see multiple issues in your Dockerfile.
COPY /home/pc/Desktop/yolo4_deep .
The COPY command copies files from your local machine to the container. The path on your local machine must be path relative to your build context. The build context is the path you pass in when you run docker build . — in this case the . (the current directory) is the build context. Also the local machine path can only reference files located under the build context — i.e. paths containing .. (parent directory) or / (root directory) are not allowed.
WORKDIR app
WORKDIR sets the path inside the container not on your local machine. So WORKDIR /app means that all commands — RUN, CMD, ENTRYPOINT — will be executed from the /app directory.
CMD ["python","./app/save_model.py","./app/object_tracker.py" ]
As mentioned above WORKDIR /app causes all operations to be executed from the /app directory. So ./app/save_model.py is actually translated as /app/app/save_model.py.
Thanks for help Everyone.
As I mentioned earlier I'm beginner in the docker world. I solved the issue by editing the copy command.
# syntax=docker/dockerfile:1
FROM tensorflow/serving:2.3.0-rc0-devel-gpu
WORKDIR /home/pc/Desktop/yolo4_deep
COPY requirements-gpu.txt .
# install dependencies
RUN pip install -r requirements-gpu.txt
# copy the content of the local src directory to the working directory
COPY src/ .
# command to run on container start
ENTRYPOINT ["./start.sh"]

Dockerfile copy from build failing for create-react-app

I have a react app I'm trying to dockerize for production. It was based off create-react-app. To run the app locally, I am in the app's root folder and I run npm start. This works. I built the app with npm run build. Then I try to create the docker image with docker build . -t app-name. This is failing for not being able to find the folder I'm trying to copy the built app from (I think).
Here's what's in my Dockerfile:
FROM node:13.12.0-alpine as build
WORKDIR /src
ENV PATH /node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
COPY . ./
RUN npm run build
FROM nginx:alpine
COPY --from=build build /usr/share/nginx/html
EXPOSE 80
CMD ["npm", "start"]
I'm pretty sure I've got something wrong on the COPY --from line.
The app structure is like this, if it matters
-app-name (folder)
-src (folder)
-build (folder)
-dockerfile
-other stuff, but I think I listed what matters
The error I get is failed to compute cache key: "/build" not found: not found
I'm running my commands in windows powershell.
What do I need to change?
You were almost correct,
Just that the path where the build folder is generated is at /src/build and not at /build.
and hence the error you see,
and why the /src coming?
it's due to the WORKDIR /src.
and hence this should work: COPY --from=build /src/build /usr/share/nginx/html
besides, since you are using nginx server to serve the build static files,
you don't need to or you cant run npm start with CMD.
instead, just leave it, and you can access the application at port 80.
so the possible working Dockerfile would be:
FROM node:13.12.0-alpine as build
WORKDIR /src
ENV PATH /node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install --silent
COPY . ./
RUN npm run build
FROM nginx:alpine
COPY --from=build /src/build /usr/share/nginx/html
EXPOSE 80
This is in accordance with the Dockerfile in the above question,
in some specific cases, advanced configuration might be required.

Couldn't find a `pages` directory. Please create one under the project root. while creating docker image

I am trying to create a docker image but I am getting this error(Couldn't find a pages directory. Please create one under the project root) in the "npm run build" step. But I have that directory in my application root folder. In my local server it is running fine and creating .next folder. My folder structure is ex: app/pages/index.js
I don't know why it is failing in the docker build. Can you guys help me with this?
Below is my Dockerfile
FROM node:14-alpine
RUN mkdir -p /usr/src/next-website
WORKDIR /usr/src/next-website
COPY package*.json ./
RUN npm i
RUN npm run build
COPY . .
EXPOSE 94
CMD ["node", "server.js"]
Thanks in advance.

Webpack inside a docker container "can't resolve './src'"

I'm trying to run a one-off webpack from within a docker container to generate a single bundle file. Unfortunately, webpack won't run inside the container based on the image I've configured.
Dockerfile ("DockerfileBuild"):
FROM node:10-alpine
COPY package.json ./
RUN npm install
RUN ["npm", "run", "build"]
docker-compose.yml:
version: "3.7"
services:
dist:
build:
context: .
dockerfile: DockerfileBuild
If I run docker-compose up dist I get ERROR in Entry module not found: Error: Can't resolve './src' in '/'.
I assume I haven't set up my image properly, but at this point I don't know what to do.
Notes:
The npm install seems to run ok beforehand.
The bundling runs ok outside the container.
You need to have your actual source files copied inside of the container before running npm run. I assume you have some reference to ./src in package.json, which would explain such an error.
Try copying everything you need before the RUN command (you can start copying everything with COPY . ., but may want to copy only ./src, that's on you).
FROM node:10-alpine
COPY package.json ./
RUN npm install
COPY . .
RUN ["npm", "run", "build"]
Since version 4.0.0, webpack doesn't need a configuration file and the default entry point is './src/index.js'. Based on the error that you are getting, it looks like you are not copying webpack.config.js into the docker image. It is trying to find the default index.js file in the './src' folder and that is why you are getting this error. If you copy the webpack.config.js then it should work.

Docker run and volume clarification

Below is my dockerfile, I do a copy of js file in copy command and set working directory after that,followed by volume and run command.Below is my dockerfile
1) I understand node_modules(Which is created because of running npm install) IS getting wipedoff when container is first initialized because of create volume in the same location
My quesion why my app.js which i copied in step 3 is not getting wiped of since its also on the same path as volume?
FROM node:latest
ENV NODE_ENV=production
ENV PORT=3000
COPY . /app
WORKDIR /app
VOLUME ["/app"]
RUN npm install
EXPOSE $PORT
ENTRYPOINT ["node","app.js"]
Q: Why is my app.js (which i copied in step 3) is not getting wiped off while node_modules is.
A: As explained in docker's documentation under the volume section.
Quote:
Changing the volume from within the Dockerfile:
If any build steps
change the data within the volume after it has been declared, those
changes will be discarded.
Reference: https://docs.docker.com/engine/reference/builder/#notes-about-specifying-volumes

Resources