Docker multi target build failed on Docker Hub - docker

For unknown reason build failed on Docker Hub when it trying to copy from a just built target into the same Dockerfile. When, I try on local machine (Fedora 27, Docker CE 17.12), the build succeed.
Here the failed build log : https://hub.docker.com/r/emmanuelgautier/react-app/builds/bsygsbahuzdxfbsqr5r9er4/

Folder /usr/src/app/build doesn't exist in the second image because according to documentation:
CMD does not execute anything at build time, but specifies the
intended command for the image.
RUN should be used instead of CMD where yarn build command executes.
The correct Dockerfile is:
## Development environment target
FROM node as dev-env
WORKDIR /usr/src/app
COPY [ "package*.json", "yarn.lock", "./" ]
RUN yarn install
COPY . .
EXPOSE 3000
ENTRYPOINT [ "./docker-entrypoint.sh" ]
## Build environment target
FROM node as build-env
WORKDIR /usr/src/app
COPY [ "package*.json", "yarn.lock", "./" ]
RUN yarn install --production
COPY . .
RUN yarn build
## Production environement target
FROM nginx as production-env
LABEL MAINTAINER Emmanuel Gautier <docker#emmanuelgautier.fr>
COPY --from=1 /usr/src/app/build /usr/share/nginx/html
EXPOSE 443 80

Related

when I modify a file and rebuild on docker the changes are not applied

i have a problem on docker, i use docker during the developpement of my project to see how it works. But now, when i want to rebuild with my new code, nothing append, it's litteraly the same execution.
here is my dockerFile :
### STAGE 1: Build ###
FROM node as build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
COPY . .
RUN npm install -g #angular/cli
RUN npm install
RUN ng build --prod
### STAGE 2: Run ###
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html
and here is the command i use to build and run my container :
docker build -t edtmaker .
docker run --name edtmaker -d -p 8888:80 edtmaker
Ok, i receive a comment who just says to clear the cache on the browser, and this worked

Docker build not using cache for reactjs project using yarn

I have the following Dockerfile
# Base on offical Node.js Alpine image
FROM node:16-alpine3.14
# Set working directory
WORKDIR /usr/app
# Install PM2 globally
RUN npm install --global pm2
# Copy package.json and package-lock.json before other files
# Utilise Docker cache to save re-installing dependencies if unchanged
COPY ./package.json ./
COPY ./yarn.lock ./
# Install dependencies
RUN yarn install
# Copy all files
COPY ./ ./
# Build app
RUN yarn build
# Expose the listening port
EXPOSE 3000
# Run container as non-root (unprivileged) user
# The node user is provided in the Node.js Alpine base image
USER node
# Run npm start script when container starts
# CMD [ "npm", "start" ]
CMD [ "pm2-runtime", "npm", "--", "start" ]
I am trying to build the image
docker build -t test .
After first time building I am trying to build again. without any change in the context.
I see it uses cache till some extent only. and and after that it does not use cache
# Install dependencies
RUN yarn install
HOw to make it use cache if there is no changes in the code.

unable to run a net core docker container

After refactoring from docker-compose into separate Docker file for each project, I'm Unable to run the container, it simply exits. I'm using both docker and docker-compose since I'll have more projects down the road.
My docker files are as follows.
docker-compose.yml
version: '3'
services:
customer:
image: customer
container_name: customer
build:
context: ./Customer
dockerfile: Dockerfile
Customer/Dockerfile
FROM mcr.microsoft.com/dotnet/core/runtime:2.2
WORKDIR /Customer
EXPOSE 80
COPY ./bin/Release/netcoreapp2.2/ service/
ENTRYPOINT ["dotnet", "service/Customer.dll"]
Also I had this within docker-compose file before. How do I map 6001 to 80 within Dockerfile?
ports:
- 6001:80
Attempt 2
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
WORKDIR /Customer
ENV DATABASE_HOST=database
ENV DATABASE_NAME=db
ENV DATABASE_USER=sa
ENV DATABASE_PASSWORD=Password
EXPOSE 80
COPY . .
CMD dotnet build
ENTRYPOINT ["dotnet", "Customer/bin/Release/netcoreapp2.2/Department.dll"]
Attempt 3
copied from main site.
Copied from here https://docs.docker.com/engine/examples/dotnetcore/
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Did you mean to run dotnet SDK commands? Please install dotnet SDK
from: https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
Here is a lazy and basic docker file. I added some comments and some helpful build/debug options, e.g: "RUN ls -al" to list the current directory. Just like a linux VM.
# step 1 - building you app with SDK as base image
FROM microsoft/dotnet:2.2-sdk AS build-env # call the environment build
WORKDIR /build # create a work dir
COPY . . # you don't need copy everything to build your app, but this is for simplisity
RUN ls -al # linux command to list dir content
RUN cd /Customer && dotnet publish -o out # actually building the app and publishing to /out dir
RUN cd /Customer && ls -al # navigate to the folder you copied and list dir
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime # step 2, runtime env (slimmed down container)
WORKDIR /app # create workdir
COPY --from=build-env /<YOUR_BULD_PATH>/out ./ # copy from prev container build output
RUN ls -al # list again
ENTRYPOINT ["dotnet", "Department.dll", "--urls", "http://*:6001"] # example from .NET Core 2.2 webapi with port 6005, this might not be your case
Now to run the docker-compose, just point out the docker file like you have already done. But both docker/docker-compose should work just fine now. Ofcourse you need to tweak the docker file a bit, i dont know your app or folder structure.
And just a tip, if you want to run your docker file as stand alone, dont forget the args when you start it to map ports --> -p 6001:80

Using one NodeJs docker image inside React Container

Step1: I have created a local docker image of one NodeJS app.
Here is the dockerfile for this app -
FROM node:8
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
#EXPOSE 8080
CMD [ "npm", "start" ]
**Step 2:**Then I built a docker image for this Node app. Here is the build command output -
C:\Users\shibathethinker\Documents\GitHub\NodeProjects\API_Proxy_ABN>docker build -t api-proxy .
Sending build context to Docker daemon 8.637MB
Step 1/6 : FROM node:8
---> 0bf36d7ccc1e
Step 2/6 : WORKDIR /usr/src/app
---> Running in 7187d65639f1
Removing intermediate container 7187d65639f1
---> 0e34dc93439c
Step 3/6 : COPY package*.json ./
---> 47c0d0ca8c77
Step 4/6 : RUN npm install
---> Running in d7e5163371df
npm WARN api_proxy#1.0.0 No repository field.
added 98 packages from 91 contributors and audited 194 packages in 8.598s
found 0 vulnerabilities
Removing intermediate container d7e5163371df
---> 72da705ae792
Step 5/6 : COPY . .
---> 0293df6aa27d
Step 6/6 : CMD [ "npm", "start" ]
---> Running in 251e98c0a0ae
Removing intermediate container 251e98c0a0ae
---> a92d8a95b8cd
Successfully built a92d8a95b8cd
Successfully tagged api-proxy:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
**Step 3:**Then, I wanted to use this docker image in another 'React' app.
Here is the Dockerfile of the app -
FROM api-proxy:latest
WORKDIR /app
RUN npm install
CMD [ "npm", "start" ]
# stage: 2 — the production environment
FROM nginx:alpine
#COPY —from=react-build /app/build /usr/share/nginx/html
#COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY /build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Step4: Now I built and ran this docker image generated on step 3.
Question:
It looks like the node app is not running on the newly created docker container.
If I 'ssh' into the docker container I can not see any node server running there.
I also could not find the WORKDIR (/usr/src/app) created in the step1 in this container.
What I am doing wrong?
Please let me know if I can clarify further.
You are doing a multi-stage docker build.You are building your application using nodejs (download dependencies and minification build) and copying and running it on nginx web server.
Nodejs server can be in its own independent container, which i think you have already done. The client would actually be a webserver for e.g. nginx, apache etc. that would serve the build of your react app. In the end, you will have 2 containers running: 1 for nodejs server and 1 for nginx webserver.
To place the build of your react app into this nginx webserver, you will use multi-stage build. In the first stage you will build your react app and in the second stage you will use nginx image and copy the react build from the first stage into the html folder of the nginx image.

will the commands in Dockerfile run as follows?

docker build Dockerfile .//running it correctly.
1.) I have mentioned in the comments each command will execute as written, Is that correct working of this Dockerfile?
2.)These commands will be used to make the image when I ran docker build, so
[ec2-user#ip-xx-xx-xx-xx ~]$cd /project/p1
[ec2-user#ip-xx-xx-xx-xx p1]$ls
Dockerfile a b c d
My Dockerfile consists of following commands.
Dockerfile
node 8.1.0 //puls the image from hub
RUN mkdir -p /etc/x/y //make directory in the host at path /etc/x/y
RUN mkdir /app //make directory in the host at path /app
COPY . /app //copy all the files that is
WORKDIR /app //cd /app; now the working directory will be /app for next commands i.e npm install.
RUN npm install
EXPOSE 3000 //what this will do?
Question 1: how to run docker build?
docker build Dockerfile . # am I running it correctly.
No, you run it with docker build . and docker will automatically look for the Dockerfile in the current directory. Or you use docker build -f Path_to_the_docker_file/DockerFile where you clearly specify the path to the DockerFile.
Question 2: Fixing errors and clarifying commands
There are few mistakes in the Dockerfile, check the edited comments:
# pulls the image from dockerhub : YES
# Needs to be preceeded with FROM
FROM node 8.1.0
# all directories are made inside the docker image
# make directory in the image at path /etc/x/y : YES
RUN mkdir -p /etc/x/y
# make directory in the image at path /app : YES
RUN mkdir /app
COPY . /app # copy all the files that is : YES
WORKDIR /app # cd /app; now the working directory will be /app for next commands i.e npm install. : YES
RUN npm install
EXPOSE 3000 # what this will do? => tells all docker instances of this image to listen on port 3000.

Resources