COPY package.json - Dockerfile - json

I did a Dockerfile to a project in Node.js but an error happened.
Here's the Dockerfile:
FROM node:latest
RUN npm install nodemon -g
WORKDIR ./app
COPY package.json ./app
RUN npm install -g
COPY server.js ./app
EXPOSE 3000
CMD ["npm", "start"]
When I tried building this Dockerfile happen an error like this:
Step 4/8 : COPY package.json ./app
COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXXX/package.json: no such file or directory
How can I fix it?
Docker version 17.12.0

Do not ever run nodemon in production (if that's what you tried to do).
You should configure your restart in case if app crashes. Preferably, set it to always in docker-compose.yml
The best way to structure Dockerfile in your case:
FROM node:latest
WORKDIR ./app
# please note, you already declared a WORKDIR,
# therefore your files will be automaticaly pushed to ./app
COPY package.json ./
RUN npm install -g
COPY ./ ./
EXPOSE 3000
CMD ["npm", "start"]
Hope, that helps.

Make sure you have package.json and server.js files in the same directory of your Dockerfile and it should work.
When you build a docker image, the whole content of the directory becomes your docker build context, and docker will find the files you COPY or ADD from there.
You might sometime wants to prevent some of those files to be sent to the build context, in which case you use the .dockerignore file to specify those files. Good luck!

My suggestion, move all files to the WORKDIR than execute your npm install
FROM node:latest
RUN npm install nodemon -g
WORKDIR /app
ADD . /app
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

Related

podman machine init fails with unhelpful output

I am running the command
podman machine init
in the /entrypoint.sh script which I reference in my Dockerfile:
# Container image that runs your code
FROM ubuntu:latest
# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.sh /entrypoint.sh
RUN apt-get update -y
RUN apt-get install -y git gcc python3-dev musl-dev libffi-dev podman
# Code file to execute when the docker container starts up (`entrypoint.sh`)
ENTRYPOINT ["/entrypoint.sh"]
The Dockerfile and the entrypoint.sh scripts are in my github repo where I have github actions configured. The problem I see my my log output from the Github action is this ...
2022-11-29T04:56:58.6855699Z + podman machine init
2022-11-29T04:56:59.1867174Z Downloading VM image: fedora-coreos-37.20221106.2.1-qemu.x8…
2022-11-29T04:56:59.3660034Z [1A[JDownloading VM image: fedora-coreos-37.20221106.2.1-qemu.x8…
...
2022-11-29T04:57:06.5082410Z [1A[JDownloading VM image: fedora-coreos-37.20221106.2.1-qemu.x8…
2022-11-29T04:57:06.9133659Z Extracting compressed file
2022-11-29T04:57:36.0586753Z Error: exit status 1
Extracting compressed file? Did I run out of disk space? What? I am not even sure where to begin to debug this.

How to run PM2 in a distroless image

I want to run node application with pm2 in distroless image.
As it do not have any shell inside it.
Use this docker file for reference, for pm2 shell /bin/sh is required so we are manually copying all the required package from above image.
After passing the absolute path in CMD or ENTRYPOINT it will work fine.
FROM node AS build-env
ADD . /app
RUN apt-get update
WORKDIR /app
RUN npm install --omit=dev
RUN npm --loglevel info install pm2 -g
RUN ls -a
RUN pm2 --version
FROM gcr.io/distroless/nodejs:12
COPY --from=build-env /usr/local/ /usr/local
COPY --from=build-env /usr/local/ /usr/local
COPY --from=build-env /usr/sbin/ /usr/sbin
COPY --from=build-env /usr/share/ /usr/share/
COPY --from=build-env /usr/src/ /usr/src/
COPY --from=build-env /usr/bin/ /usr/bin/
COPY --from=build-env /bin/ /bin/
COPY --from=build-env /app /app
WORKDIR /app
EXPOSE 3000
CMD ["/usr/local/lib/node_modules/pm2/lib/binaries/Runtime4Docker.js","hello_express.js"]

Docker, paths, json-file not found: not found

What is the problem? everytime happens this...
=> CACHED [2/5] WORKDIR ../app 0.0s
=> ERROR [3/5] COPY package.json . 0.0s
------
> [3/5] COPY package.json .:
------
failed to compute cache key: "/package.json" not found: not found
I can`t understand what am I doing wrong.
Dockerfile:
FROM node
WORKDIR /app
COPY ../app/package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
I use docker build . in powershell
You can't include files from outside your context (the current directory you are in) to a docker image
So you can either move your app directory on the host to the folder of the Dockerfile or
You can do the following
docker build -f docker\Dockerfile .
in the parent directory (ie the directory containing the docker and the app folders). And adjust your Dockerfile as follows
COPY app/package.json .
You are updating the WORKDIR, as the document says:
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
I think, saying just
COPY package.json
should be enough.
Dockerfiles don't have access to parent directories, unfortunately. However you can build the dockerfile from the parent directory by using docker build -f path/to/dockerfile .. Keep in mind that your COPY is relative to the directory you run the docker build from, which is the parent directory.
Also, WORKDIR uses/creates a directory inside the container. WORKDIR /app creates the 'app' folder in the root directory of the container, so /../app is unnecessary (although it achieves the same result).
You cannot reference to parent directory in dockerfile for the reason explained in docker documentation:
The path must be inside the context of the build; you cannot ADD
../something /something, because the first step of a docker build is
to send the context directory (and subdirectories) to the docker
daemon.
Source
The easiest walk-around is:
docker build -t <some tag> -f <dir/dir/Dockerfile> .
Another thing you can add is build context. Instead of copying all files in the current directory to Docker Daemon, you can configure the context for example
docker build -t <some tag> -f ./dir/to/Dockerfile ./dir/to/build/context

How to set up Docker for Symfony project with MySQL?

How to set up a new Symfony project with MySQL database using Docker?
I've been trying to set up a new project using Docker for over a week now. I've read trough Docker documentation, found a few tutorials, but nothing really worked for me. And I'm just not able to crack how Docker set up works. Last time I tried I just got a RuntimeException and an ErrorException errors
Project Structure:
-myProject
-bin
-...
-config
-...
-docker
-build
-php
-Dockerfile
-php
-public
-index.php
-src
-...
-var
-...
-vendor
-...
-docker-compose.yaml
-...
My docker-compose.yaml:
version: '3.7'
services:
php:
build:
context: .
dockerfile: docker/build/php/Dockerfile
ports:
- "8100:80"
# Configure the database
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root}
My Dockerfile:
FROM php:7.3-apache
COPY . /var/www/html/
I expected to have "Welcome to Symfony" page but I got an error page.
Errors:
ErrorException
Warning: file_put_contents(/var/www/html/var/cache/dev/srcApp_KernelDevDebugContainerDeprecations.log): failed to open stream: Permission denied
AND
RuntimeException
Unable to write in the cache directory (/var/www/html/var/cache/dev)
What I need is some help to set up my Symfony 4 project with MySQL using Docker
OK so to make it work I just needed to give permision to var folder using chmod in Dockerfile
FROM php:7.3.2-apache
COPY . /var/www/html/
RUN chmod -R 777 /var/www/html/ /var/www/html/
Found this answer in the comments, but the person that left it removed the comment
You actualy have no need to chmod your project root folder to something unnecessary open, like 0777.
In php:* containers php workers run from www-data user. So all you need to do is chown your current project root dir to www-data and verify that www-data user can actualy create folders in it (ls -lah will help you).
Here is my php stage from symfony 4.3 projects:
FROM php:7.3-fpm as runtime
# install php ext/libraries and do other stuff.
WORKDIR /var/www/app
RUN chown -R www-data:www-data /var/www/app
COPY --chown=www-data:www-data --from=composer /app/vendor vendor
COPY --chown=www-data:www-data bin bin
COPY --chown=www-data:www-data config config
COPY --chown=www-data:www-data public public
COPY --chown=www-data:www-data src src
COPY --chown=www-data:www-data .env .env

Docker with Angular 2: cannot get

We're trying to set up an Angular 2 application with Docker following this tutorial: https://scotch.io/tutorials/create-a-mean-app-with-angular-2-and-docker-compose
The application deploys but we get 'Cannot GET /'
This is how we build our app:
sudo docker build -t frontend:dev .
and this is how we run our app
sudo docker run -d --name frontend -p 1014:4200 frontend:dev
Our dockerfile is exactly the same as the tutorial:
# Create image based on the official Node 6 image from dockerhub
FROM node:6
# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app
# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app
# Copy dependency definitions
COPY package.json /usr/src/app
# Install dependecies
RUN npm install
# Get all the code needed to run the app
COPY . /usr/src/app
# Expose the port the app runs in
EXPOSE 4200
# Serve the app
CMD ["npm", "start"]
And this is an excerpt from package.json
{
"name": "courseka",
"version": "0.0.0",
"scripts": {
"start": "ng serve -H 0.0.0.0"
"build": "ng build"
}
}
And as last, something from our index.html file
<html>
<head>
<base href="/">
</head>
</html>
Seems the problem was a Linux-Windows issue. The development op the app happened on Windows where the import folders and classes are case-insensitive, while on Linux they are and two imports were wrongly capitalized.
The problem was detected by running with the
-it
parameter, that logs all the output