Docker, link container's files to host - docker

I have a React App built with Docker :
FROM node:latest
WORKDIR /home/node/app
# Build
COPY package*.json ./
COPY src ./src
COPY public ./public
COPY .env* ./
RUN npm install
RUN npm run build
# Remove build material
RUN rm -f package.json
RUN rm -f package-lock.json
RUN rm -rf src
RUN rm -rf public
RUN rm -f .env
RUN rm -f .env.local
# Update host
CMD rm -rf /home/node/app/www/*;cp build/* /home/node/app/www
I'm building this image through a Gitlab CI / CD pipeline and launching it on my remote host with docker-compose :
version: '3'
services:
app:
image: registry.gitlab.com/myproject/www:production
volumes:
- /var/www:/home/node/app/www
I have Nginx running on my remote host that serves /var/www on port 80.
What I would like to do is my container to be bind to /var/www
ie: Each time I'm building and launching my image, /var/www gets updated by it.
My setup kinda work but I ran into issues, it doesn't refresh all the time for whatever reason.
Is there any better way to do so ?

Related

Docker build is using cache for COPY command even if my files have changed

I have a Dockerfile that is as follow:
FROM node:14-alpine as frontend-builder
WORKDIR /app/frontend
COPY ./frontend .
ENV PATH ./node_modules/.bin/:$PATH
RUN set -ex; \
yarn install --frozen-lockfile --production; \
yarn cache clean; \
yarn run build
CMD ["tail", "-f", "/dev/null"]
I have changed one file in frontend folder and re-run the build and docker is using the cache...I know i can force to build with --no-cache but how can i tweak docker so it detects changes in my files instead of no-cache option ?

docker buildx not copying files when in production

I am having an issue where the docker buildx tool doesn't properly copy files to the production container. For example:
FROM --platform=$BUILDPLATFORM node:16.14.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY yarn.lock ./
RUN apk update && apk add python3 g++ make && rm -rf /var/cache/apk/*
RUN yarn install
COPY . ./
RUN yarn run build
# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html/
COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
# Default port exposure
EXPOSE 80
# Copy .env file and shell script to container
COPY ./env.sh /usr/share/nginx/html/env.sh
# Start Nginx server
CMD ["/bin/sh", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]
All of the files from build will copy properly. However, the nginx.conf and env.sh will report as not found. Any ideas?
P.S: This seems to be an issue with nginx:alpine. No clue

Why can't I build and then run my container locally

I have a multi stage Dockerfile
# Base Build
FROM alpine:3.7 AS base
RUN apk add --no-cache nodejs
WORKDIR /root/app
COPY . .
ARG TARGET_ENV
COPY .env.$TARGET_ENV .env
RUN rm .env.*
RUN npm set progress=false && npm config set depth 0
RUN npm install --only=production
RUN cp -R node_modules prod_node_modules
RUN npm install
RUN npm run build
# Prod Build
FROM base AS release
COPY --from=base /root/app/prod_node_modules ./node_modules
COPY --from=base /root/app/package.json .
COPY --from=base /root/app/package-lock.json .
COPY --from=base /root/app/dist .
CMD npm start
EXPOSE 3000
I'd like to build my container and then run it locally.
It builds just fine but when I run it a hash is output, but the container is not running.
docker build --build-arg TARGET_ENV=local -t express-app .
docker run -d -p 3000:3000 -it express-app
Your container could be crashing on start.
Check the output of $ docker run -p 3000:3000 -it express-app for error messages.

Dockerfile is caching an old version of a generated file

I'm working on a Dockerfile with a multi-stage build. The general idea is to build the binary for the backend, build the javascript bundle for the frontend, and then put these two things in a final container for the app.
Here's the docker file:
# go binary
FROM golang:alpine as build-go
RUN apk --no-cache add git bzr mercurial
ENV D=/go/src/github.com/tamuhack-org/quack
RUN go get -d -v golang.org/x/net/html
RUN go get -d -v github.com/gorilla/handlers
RUN go get -d -v github.com/gorilla/mux
COPY ./main.go $D/main.go
COPY ./frontend/dist $D/frontend/dist
RUN rm -rf $D/frontend/dist/index.html
RUN rm -rf $D/frontend/dist/index.js
RUN cd $D && go build -o main && cp main /tmp/
# ui
FROM node:alpine AS build-node
RUN mkdir -p /src/ui
COPY ./frontend/package.json /src/ui/
RUN cd /src/ui && yarn install
COPY ./frontend /src/ui
# Replace the dev instance of index.html with the prod version.
RUN rm -rf /src/ui/dist/index.html
RUN mv /src/ui/dist/index-prod.html /src/ui/dist/index.html
RUN cd /src/ui && yarn build
# final
FROM alpine
RUN apk --no-cache add ca-certificates
WORKDIR /app/server/
COPY --from=build-go /tmp/main /app/server/
COPY --from=build-node /src/ui/dist /app/server/frontend/dist
EXPOSE 8080
CMD ["./main"]
What I've noticed is that when I update the frontend source code and build the docker container, the new version of the container doesn't update with the new bundle. Are there any obvious errors in the Dockerfile that may be the reason for why I'm not seeing any file changes? If I run yarn build locally, the bundle is accurate, but the docker container seems to be caching an older version. Thoughts?

How can I debug dist/ is not generating on Docker Cloud Build?

I’m trying to automate build on dockercloud to build a docker container for my front-end.
I have
web.dockerfile
### STAGE 1: Build ###
FROM node:9.3.0-alpine as builder
COPY package.json ./
RUN npm set progress=false && npm config set depth 0 && npm cache clean --force
## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i
RUN mkdir /web
RUN cp -R ./node_modules ./web
WORKDIR /web
COPY . .
## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod --build-optimizer
### STAGE 2: Setup ###
FROM nginx:1.13.8-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY site.conf /etc/nginx/conf.d/default.conf
RUN rm -rf /usr/share/nginx/html/*
COPY dist /usr/share/nginx/html/
RUN touch /var/run/nginx.pid && \
chown -R nginx:nginx /var/run/nginx.pid && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /usr/share/nginx/html
USER nginx
as you see I already did this line :
RUN $(npm bin)/ng build --prod --build-optimizer
Which it should generate the dist/ folder for me.
I got
This long logs in my docker-cloud
What I don't understand is, it is working fine, when I used that Dockerfile locally.
Should I look into my webpack settings or my Dockerfile syntax ?
Use the --from argument to COPY to set the source location to a previous build stage.
COPY --from=builder /web/dist /usr/share/nginx/html/
For your local build you probably have a dist/ folder for your local development which is then included in the build context sent to Docker. The local copy is what ends up in the image.

Resources