Docker compose is failing when copying npmrc - docker

I am getting this error when trying to run my docker with docker compose
=> ERROR [6/9] COPY .npmrc .npmrc
0.0s => ERROR [7/9] ADD package.json /Users/gs/Documents/Dev/DockerDi 0.0s
version: "3"
services:
gs-service:
build:
context: .
dockerfile: ./gs/Dockerfile
ports:
- 8081:8081
ui-service:
build:
context: .
dockerfile: ./ui/Dockerfile
ports:
- 8083:8083
Here is the detail of the Dockerfile which is failing. This is a subdirectory. For example /user/gs/dockerexperiment/ui
docker-compose file is in parent directory. For example
/user/gs/dockerexperiment
Dockerfile is in subdirectory. For example
/user/gs/dockerexperiment/ui
I know something is going wrong with the class path but I am having a difficult time figuring out what I am missing

A Dockerfile operates on a build context; this is a copy of the directory tree you name as the build: { context: } in the docker-compose.yml file (or, if you're using docker build directly, its directory argument).
build:
context: .
In the Dockerfile, any COPY (or ADD) instructions are interpreted as relative to this directory tree. (If it looks like an absolute path, it's still relative to this directory; any .. path steps that would step outside this directory get ignored.)
COPY package*.json ./
# copies ./package.json, relative to the context directory
But, you clarify in a comment, this isn't actually where your files are. Relative to the docker-compose.yml file, you need ./ui/package.json; the Dockerfile is also in the same subdirectory.
The Dockerfile you show doesn't seem to need any content from outside its immediate tree, so the easiest way to address this is to set the ui directory as the build context:
version: '3.8'
services:
ui:
build: ./ui
# Same as
# build:
# context: ./ui
# dockerfile: Dockerfile # relative to the context directory
ports:
- 8085:8085
In the Dockerfile, you can remove the duplicate ADD package.json line, and you should remove the dangerous RUN npm set config line. You can also set a much shorter WORKDIR, something like /app would be typical.
If you don't want to change the docker-compose.yml file, the other possible change is to adjust the Dockerfile so that its COPY paths are relative to the (parent) build-context directory.
COPY ui/package*.json .
The advantage of doing this is that you can access files from the other application, if you need to, or you can store shared files in that parent directory.
FROM node:14
# Add the public TLS key for our internal repository
# (from the parent directory)
COPY repository.example.com.crt /usr/local/share/ca-certificates
RUN update-ca-certificates
WORKDIR /app
# Also get our local npm configuration (from the parent directory)
COPY .npmrc .
# Install the ./ui application
COPY ui/package*.json .
RUN npm ci
COPY ui/src ./src
EXPOSE 8085
CMD ["npm", "start"]

Related

Dockerfile is copying files from outside of parent directory

I have a simple Dockerfile that is in a directory called /App when I build my docker container using a docker-compose yaml file the dockerfile copies files from one level up, outside the /App folder into the container.
Here is my dockerfile
FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
and here is my docker-compose file
version: '3'
services:
dash:
build:
context: ./App
dockerfile: Dockerfile.dash
container_name: dash_dash
command: ls
volumes:
- .:/code
ports:
- "80:8080"
When I build and run the container the ls command shows that it copied the directory one level above the /App directory, such that the /App directory is included but is not the main directory.
The volumes section of your docker-compose.yml is overriding the working directory:
volumes:
- .:/code
is copying the whole folder where the docker-compose.yml is (so it is copying the /App folder entirely). As a result, your files in your working directory (/code) are overridden.
You should remove the volumes section of your docker-compose.yml. The ls command will then show the contents of the App directory, copied by the COPY . . section of your Dockerfile.

Why docker-compose and dockerfile working this way? [duplicate]

I created a docker file.
FROM node:13.6.0-alpine3.10
WORKDIR /src
RUN apk add --no-cache bash
COPY ./package.json .
COPY ./package-lock.json .
RUN npm install
COPY . .
EXPOSE 8081
CMD npm run start:dev
Structure of my project.
.
└── my-app
└── docker-compose.yml
└── ...
└── server
└── docker
└── Dockerfile
└── src
└── ....
├── package.json
├── package-lock.json
└── ...
When I build the container, I get an error.
ERROR: Service 'server' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder791989542/package.json: no such file or directory
I understand that my files are in a different directory. I tried to solve my problems like this.
COPY ./../package.json .
COPY ./../package-lock.json .
But I also got an error
ERROR: Service 'server' failed to build: COPY failed: Forbidden path outside the build context: ../package.json ()
How to tell the docker so that he is looking for files not in the docker folder where the dockerfile file is located, but in the src folder, where my package-lock.json and package.json files are located
I created a docker-compose.yml file.
version: "3.3"
services:
server:
container_name: server
command:
- npm
- run
- start:dev
build:
context: ./server/docker
dockerfile: dockerfile
environment:
PORT: 8081
ports:
- 8081:8081
restart: on-failure
volumes:
- ./server:/src/
As explained earlier in comments and previous answers, you cannot copy files which are outside of the build context. So you either need to change the context up the directory path or to move the needed files inside the current context.
Looking at your layout, I would go for the first solution and by default use your base directory as context (once you understand the solution, you can adapt context and dockerfile to fit your exact needs).
In your Dockerfile, change the following lines:
COPY ./server/package.json .
COPY ./server/package-lock.json .
Note: the line COPY . . should be removed (or you need to explain a littke better what you actually try to achieve with this)
The build section in your docker-compose.yml file should become:
build:
context: .
dockerfile: server/docker/Dockerfile
The equivalent manual build would be
cd /path/to/my-app
docker build -f server/docker/Dockerfile .
You cannot do that, because first step of build, is to build the context dirs. You need to change the context.
Straight from docker docs,
COPY obeys the following rules:
The path must be inside the context of the build;
you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

Docker: Shared volume when build

I have this files:
docker-compose.yml (shortened):
version: '3.7'
services:
php-fpm:
build:
context: .
dockerfile: docker/php/Dockerfile
target: dev
volumes:
- .:/app
frontend:
build:
context: .
dockerfile: docker/php/Dockerfile
target: frontend
volumes:
- .:/app
docker/php/Dockerfile (shortened):
FROM alpine:3.13 AS frontend
WORKDIR /app
COPY . .
RUN apk add npm
RUN npm install
RUN npx webpack -p --color --progress
FROM php:7.4-fpm AS dev
ENTRYPOINT ["docker-php-entrypoint"]
WORKDIR /app
COPY ./docker/php/www-dev.conf /usr/local/etc/php-fpm.d/www.conf
CMD ["php-fpm"]
I want to use all what building in frontend (as I understood at the stage build at this time volumes are not available) in php-fpm container, but I get something like this: file_get_contents(/app/static/frontend.version): failed to open stream.
How I can do this? I don't understand very well in Docker and the only solution I have is to move build script to php-fpm container.
You need to delete the volumes: in your docker-compose.yml file. They replace the entire contents of the image's /app directory with content from the host, which means everything that gets done in the Dockerfile gets completely ignored.
The Dockerfile you show uses a setup called a multi-stage build. The important thing you can do with this is build the first part of your image using Node, then COPY --from=frontend the static files into the second part. You do not need to declare a second container in docker-compose.yml to run the first stage, the build sequence runs this automatically. This at a minimum looks like
COPY --from=frontend /app/build ./static
You will also need to COPY the rest of your application code into the image.
If you move the Dockerfile up to the top of your project's source tree, then the docker-compose.yml file becomes as simple as
version: '3.8'
services:
php-fpm:
build: . # default Dockerfile, default target (last stage)
# do not overwrite application code with volumes:
# no separate frontend: container
But you've put a little bit more logic in the Dockerfile. I might write:
FROM node:lts AS frontend # use a prebuilt Node image
WORKDIR /app
COPY package*.json . # install dependencies first to save time on rebuild
RUN npm install
COPY . . # (or a more specific subdirectory?)
RUN npx webpack -p --color --progress
FROM php:7.4-fpm AS dev
WORKDIR /app
COPY . . # (or a more specific subdirectory?)
COPY --from=frontend /app/build ./static
COPY ./docker/php/www-dev.conf /usr/local/etc/php-fpm.d/www.conf
# don't need to repeat unmodified ENTRYPOINT/CMD from base image

Docker does not search for a file in a directory

I created a docker file.
FROM node:13.6.0-alpine3.10
WORKDIR /src
RUN apk add --no-cache bash
COPY ./package.json .
COPY ./package-lock.json .
RUN npm install
COPY . .
EXPOSE 8081
CMD npm run start:dev
Structure of my project.
.
└── my-app
└── docker-compose.yml
└── ...
└── server
└── docker
└── Dockerfile
└── src
└── ....
├── package.json
├── package-lock.json
└── ...
When I build the container, I get an error.
ERROR: Service 'server' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder791989542/package.json: no such file or directory
I understand that my files are in a different directory. I tried to solve my problems like this.
COPY ./../package.json .
COPY ./../package-lock.json .
But I also got an error
ERROR: Service 'server' failed to build: COPY failed: Forbidden path outside the build context: ../package.json ()
How to tell the docker so that he is looking for files not in the docker folder where the dockerfile file is located, but in the src folder, where my package-lock.json and package.json files are located
I created a docker-compose.yml file.
version: "3.3"
services:
server:
container_name: server
command:
- npm
- run
- start:dev
build:
context: ./server/docker
dockerfile: dockerfile
environment:
PORT: 8081
ports:
- 8081:8081
restart: on-failure
volumes:
- ./server:/src/
As explained earlier in comments and previous answers, you cannot copy files which are outside of the build context. So you either need to change the context up the directory path or to move the needed files inside the current context.
Looking at your layout, I would go for the first solution and by default use your base directory as context (once you understand the solution, you can adapt context and dockerfile to fit your exact needs).
In your Dockerfile, change the following lines:
COPY ./server/package.json .
COPY ./server/package-lock.json .
Note: the line COPY . . should be removed (or you need to explain a littke better what you actually try to achieve with this)
The build section in your docker-compose.yml file should become:
build:
context: .
dockerfile: server/docker/Dockerfile
The equivalent manual build would be
cd /path/to/my-app
docker build -f server/docker/Dockerfile .
You cannot do that, because first step of build, is to build the context dirs. You need to change the context.
Straight from docker docs,
COPY obeys the following rules:
The path must be inside the context of the build;
you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

.dockerignore not ignoring any files

My .dockerignore file isn't doing anything, and I can't seem to find what I am doing wrong.
I have Dockerfile, docker-compose.yml and .dockerignore all within the root of the project.
.yml:
version: "3.5"
services:
gateway:
build: .
...
Dockerfile:
FROM php:7-fpm-alpine
EXPOSE 10091
WORKDIR /root
COPY . .
.dockerignore:
.ash_history
.env.*
.git
.gitignore
.idea
Dockerfile
docker-compose.yml
client
I have tried specifying a single file in case my formatting is bad, but it still didn't do anything. I have also tried deleting the image and creating it again (even though I am using a docker-compose up -d --build each time).
I suppose that COPY action from the Dockerfile executes after the ignoring of files takes place?

Resources