Gitflow failed too create docker image - docker

Hi everyone I'm facing a strange issue in the creating of a docker image for a remix.run app, and using it inside a github job.
I have this Dockerfile
FROM node:16-alpine as deps
WORKDIR /app
ADD package.json yarn.lock ./
RUN yarn install
# Build the app
FROM node:16-alpine as build
ENV NODE_ENV=production
WORKDIR /app
COPY --from=deps /app/node_modules /app/node_modules
COPY . .
RUN yarn run build
# Build production image
FROM node:16-alpine as runner
ENV NODE_ENV=production
ENV PORT=80
WORKDIR /app
COPY --from=deps /app/node_modules /app/node_modules
COPY --from=build /app/build /app/build
COPY --from=build /app/public /app/public
COPY --from=build /app/api /app/api
COPY . .
EXPOSE 80
CMD ["npm", "run", "start"]
If build the image on my local machine, everything works fine, and I'm able to run the container and point at it.
I made a github workflow build the same image and push it on my docker hub.
But when the github job runs it always fail with this error
Step 16/21 : COPY --from=build /app/build /app/build
COPY failed: stat app/build: file does not exist
My remix.run config is:
/**
* #type {import('#remix-run/dev/config').AppConfig}
*/
module.exports = {
appDirectory: "app",
assetsBuildDirectory: "public/build",
publicPath: "/build/",
serverBuildDirectory: "api/_build",
devServerPort: 8002,
ignoredRouteFiles: [".*"],
};
Thanks in advance for any help

You are using the config option serverBuildDirectory: "api/_build", so when you run the remix build, your server built files are in api/_build/ and not in build/ directory.
In your docker image, at the last stage you try to copy the content of the build/ directory from the previous stage named build. But that directory does not exist there. The server content is built in api/_build/ instead.
So you just don't need that line:
COPY --from=build /app/build /app/build
One possible reason I see for the fact it works for you locally:
You have a local directory named build/, and which contains your local build for server files. Maybe because you built it before changing the serverBuildDirectory option. And it's probably ignored from your git repository.
When you build your docker image, the stage named build copies
everything from your local directory to its own environment with
COPY . ., and so it gets your local build/ directory.
then during the last stage that directory is copied to the final environment with COPY --from=build /app/build /app/build.
When you do the same thing in a fresh environment like github actions, the build/ directory does not exist in the environment that runs the docker command so it's not copied from stage to stage. And you finally get an error trying to copy something that does not exist.
This artifact copying is probably not what you want. If you do the build in your docker image, you don't want also to copy it from your local environment.
To avoid these unwanted copies, you can add a .dockerignore file with at least the following things:
node_modules
build
api/_build
public/build

Related

Cannot find WORKDIR in the container

I've been building nodejs applications and wanted to investigate projects inside a Docker container.
But when I check the running container via bin/bash I cannot find /app directory in a container
While building image no errors are being displayed
This is my docker image file
FROM node:15.7.0 as build-stage
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY ./ /app/
ARG configuration=production
RUN npm run build -- --output-path=./dist/out --configuration $configuration
FROM nginx:1.18
COPY --from=build-stage /app/dist/out/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf
Sèdjro has the correct answer, but I wanted to expand on it briefly.
You're using what is called a multi-stage build. If you haven't seen them already, the linked docs are a good read.
Each FROM ... line in your Dockerfile starts a new build stage, and only the contents of the final stage are relevant to the generated Docker image.
Other stages in the file are available as sources of the COPY --from=... directive.
In other words, as soon as you add a second FROM ... line, as in...
FROM node:15.7.0 as build-stage
.
.
.
FROM nginx:1.18
...then the contents of the first stage are effectively discarded,
unless you explicitly copy them into the final image using COPY --from=.... Your /app directory only exists in the first stage, since you're copying it into a different location in the final stage.
The /app directory is not inside of your docker image.
It's in your build stage image.
Check the /usr/share/nginx/html directory or don't use build stage.

COPY command not working as expected in multistage Dockerfile

Stage 1:
FROM node:10.23.0-alpine as react
WORKDIR /app
RUN npm run build
Stage2:
FROM python:3.6-alpine3.7 AS release
# Create app directory
WORKDIR /app
# In the below line only the contents of the build folder is been copied and not the folder itself
COPY --from=react /app/build ./
From the above Dockerfile I am trying to copy the build folder from stage 1 to python image in stage 2. Only the contents of the build folder is been copied and not the complete folder
create a folder where you want to copy it
COPY --from=react /app/build ./new_directory

How to point correct path in the Dockerfile

I have .sln file and Dockerfile at a different location on my local hard drive.
Till now I work on a source code where .sln file and Dockerfile are present at the same location.
I am facing difficulty to configure it correctly.
I have .sln file at the location CompanyCarsDocker\CompanyCarsDocker\CompanyCarsDocker.sln
And My Dockerfile is present inside CompanyCarsDocker\CompanyCarsDocker\CarApi\Dockerfile
-
Below is my Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 54411
FROM microsoft/aspnetcore-build:2.0 AS builder
WORKDIR /src
COPY . ./*.sln ./
ADD ./site /inetpub/wwwroot
COPY ./CarApi.csproj CarApi/
RUN dotnet restore
COPY . .
WORKDIR /src/CarApi
RUN dotnet build -c Release -o /app
FROM builder AS publish
RUN dotnet publish -c Release -o /app
FROM base AS production
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "CarApi.dll"]
I have added ADD ./site /inetpub/wwwroot for testing purpose. I am getting
ADD failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder782713904\site: The system cannot find the file specified.
Same error I am also Getting at COPY ./CarApi.csproj CarApi/
Please let me know if I configured something wrong in the Dockerfile.
You can use the -f flag of docker build to specify the path to the Dockerfile
Make sure you’re current working directory is the one which has the sln file and rest of the project. Run the command docker build -f <path to Dockerfile> .
your .sln file is two directories up so you should use
COPY ../../*.sln ./
instead of
COPY . ./*.sln ./

Multi-stage build cannot copy from previous stage - File not found

I have the docker file as follows:
FROM node:8 as builder
WORKDIR /usr/src/app
COPY ./src/register_form/package*.json .
RUN npm install
COPY ./src/register_form .
RUN yarn build
FROM tensorflow/tensorflow:1.10.0-gpu-py3
COPY --from=builder /usr/src/app/register_form/build/index.html /app/src/
WORKDIR /app
ENTRYPOINT ["python3"]
CMD ["/app/src/main.pyc"]
However, it cannot copy the index.html from the builder stage. Although when I list the folder in the first stage, the files are there.
The error is:
Step 8/22 : COPY --from=builder ./register_form/build/ /app/src/
COPY failed: stat /var/lib/docker/overlay2/5470e05501898502b3aa437639f975ca3e4bfb5a1e897281e62e07ab89866304/merged/register_form/build: no such file or directory
How can I fix this problem - the COPY --from=builder docker command?
I think you are misusing COPY command. As it is told in docs:
If src is a directory, the entire contents of the directory are
copied, including filesystem metadata.
Note: The directory itself is not copied, just its contents.
So your command COPY ./src/register_form . does NOT create register_form folder in container, but instead copies all contents. You can try adding:
RUN ls .
to your Dockerfile to make sure.
As noticed by #BMitch in comments, you can explicitly set destination folder name to achieve expected results:
COPY ./src/register_form/ register_form/

How to copy a csproj file using docker without visual studio and without docker compose?

I just started a new solution with a .NET Core Project (2.1) using visual studio 15.8.8. It can run and debug it by setting the docker compose file as a startup project. It works!
Logically, I should be able to build the docker image with a simple commandline statement. However, it complains that the csproj cannot be found. This is strange. The file exist and as I told, I can run it from visual studio. I tried it from one directory up and the directory that has the dockerfile. Same problem.
How can I solve this? The only thing I want is simply build my image and then run it by just using docker commands.
Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2/TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . .
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Het is the compose file:
version: '3.4'
services:
trynewdocker2:
image: ${DOCKER_REGISTRY}trynewdocker2
build:
context: .
dockerfile: TryNewDocker2/Dockerfile
Logically, I want "docker-compose up" to keep working when fixing this problem.
This is caused by the wrong root folder for the file path in dockerfile.
For launching from Docker, its root folder is C:\Users\...\repos\TryNewDocker2, but while running from command, its root fodler is C:\Users\...\repos\TryNewDocker2\TryNewDocker2, so the path for TryNewDocker2.csproj has changed from TryNewDocker2/TryNewDocker2.csproj to TryNewDocker2.csproj
Try dockerfile below:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 59162
EXPOSE 44342
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TryNewDocker2.csproj", "TryNewDocker2/"]
RUN dotnet restore "TryNewDocker2/TryNewDocker2.csproj"
COPY . ./TryNewDocker2/
WORKDIR "/src/TryNewDocker2"
RUN dotnet build "TryNewDocker2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TryNewDocker2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TryNewDocker2.dll"]
Update
For working in both Docker and command, do not change your dockerfile, and from path below to run your command with specifying the dockerfile path.
C:\Users\...\repos\TryNewDocker2>docker build -t gogo -f TryNewDocker2/Dockerfile .
For those of you who end up here years later like I did, I'll share my experience.
My problem was caused by the auto-generated Dockerfile that came from Visual Studio's "add > Docker Support..." was on the same level as my .csproj file.
The specific line causing me trouble was COPY ["MyApp/MyApp.csproj", "MyApp/"] which should have been just COPY ["MyApp.csproj", "MyApp/"]. Removing the extra MyApp/ in front of the .csproj got the build working fine.
Special thanks to Edward in the answer above for pointing me in the right direction.

Resources