Docker not running dist folder - docker

I'm trying to make a container of my builded angular app.
I have already build my app. so inside actual folder I have
dist/ src/ and root files
Here's my docker folder
FROM node:alpine AS builder
WORKDIR /app
COPY . .
FROM nginx:alpine
COPY --from=builder /app/dist/* /usr/share/nginx/html/
built is ok
docker build -t ngapp:1.0.9 .
Step 1/5 : FROM node:alpine AS builder
---> 2d8f48ba52b1
Step 2/5 : WORKDIR /app
---> Using cache
---> d8d0d9d93f72
Step 3/5 : COPY . .
---> Using cache
---> 87c12f8c2ccf
Step 4/5 : FROM nginx:alpine
---> 36189e6707f4
Step 5/5 : COPY --from=builder /app/dist/* /usr/share/nginx/html/
---> Using cache
---> e39f00401242
Successfully built e39f00401242
I run my app
sudo docker run -p 8956:8080 ngapp:1.0.9
But when I go on the web page adresse the site is not running I get message : this site is not accessible

The nginx container doesn't listen on port 8080 (by default), so simply map to the valid port:
ports:
- 8956:80

Related

Docker and Angular prod `file does not exist` Error on stage build COPY command

i do not understand why docker cannot get my angular build folder in container.
Can you see that to help me?
If i build with docker compose command i have this error.
Below are all the steps to build my image and launch my container until the error.
WARNING: The Docker Engine you're using is running in swarm mode.
Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
To deploy your application across the swarm, use `docker stack deploy`.
Building linking-front
Sending build context to Docker daemon 425.6MB
Step 1/9 : FROM node:16.19.0 AS build
---> b22f8aab05da
Step 2/9 : WORKDIR /usr/src/app
---> Using cache
---> 5e2431455b65
Step 3/9 : COPY package.json package-lock.json ./
---> Using cache
---> 11d677269b0e
Step 4/9 : RUN npm install
---> Using cache
---> b5544be9159b
Step 5/9 : COPY . .
---> Using cache
---> 3403bfda57ca
Step 6/9 : RUN npm run build
---> Using cache
---> ae8e7960ac33
Step 7/9 : FROM nginx:1.23.3-alpine
---> 2bc7edbc3cf2
Step 8/9 : COPY nginx.conf /etc/nginx/nginx.conf
---> Using cache
---> beca38c7be94
Step 9/9 : COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
COPY failed: stat usr/src/app/dist/linkingEducationSecurity-front: file does not exist
ERROR: Service 'linking-front' failed to build : Build failed
### STAGE 1: Build ###
FROM node:16.19.0 AS build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build
### STAGE 2: Run ###
FROM nginx:1.23.3-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
I use also docker-compose
version: '3.9'
services:
linking-front:
build: ./linkingEducationSecurity-front/
ports:
- "8080:80"
volumes:
- type: bind
source: ./linkingEducationSecurity-front/src/
target: /app/src
since in the comments you tried to do RUN cd dist && ls which gave you this output :
Step 7/10 : RUN cd dist && ls ---> Running in e8f002e82f3a linking-education-security-front
The steps and dockerfile are perfect. the COPY command from build folder is missing its spell
update this line :
COPY --from=build /usr/src/app/dist/linkingEducationSecurity-front /usr/share/nginx/html
to this :
COPY --from=build /usr/src/app/dist/linking-education-security-front /usr/share/nginx/html
and try rebuilding , this might work.

Golang - problem with creating docker image

I try to make docker image, but when I run cmd in terminal.
sudo docker build testapi .
I get an error:
=> ERROR [6/6] RUN go build -o /app/testapi/cmd/test-api 0.3s
------
> [6/6] RUN go build -o /app/testapi/cmd/test-api:
#14 0.231 no Go files in /app
------
executor failed running [/bin/sh -c go build -o /app/testapi/cmd/test-api]: exit code: 1
File structure
/testapi
/cmd
/test-api
maing.go
/pkg
/...
Dockerfile
Dockerfile:
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod ./
RUN go mod download
COPY . ./
RUN go build -o /app/testapi/cmd/test-api
EXPOSE 8080
CMD [ "/testapi/cmd/test-api" ]
Try this way.
Also do not forget to add go.sum in Dockerfile if you have. COPY go.sum .
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod .
COPY . .
RUN go build -o main ./cmd/<FOLDER_NAME>
WORKDIR /dist
RUN cp /app/main .
EXPOSE 3000
CMD ["/dist/main"]
Works fine
Status: Downloaded newer image for golang:1.16-alpine
---> 7642119cd161
Step 2/9 : WORKDIR /app
---> Running in f8ad2994262c
Removing intermediate container f8ad2994262c
---> 6e079707cc3e
Step 3/9 : COPY go.mod .
---> c79a0cc04ff5
Step 4/9 : COPY . .
---> 897027112e73
Step 5/9 : RUN go build -o main ./cmd/api
---> Running in eb4579f1c339
Removing intermediate container eb4579f1c339
---> e41f6e5542a4
Step 6/9 : WORKDIR /dist
---> Running in 92115865d1ec
Removing intermediate container 92115865d1ec
---> 8152a0ebe4bc
Step 7/9 : RUN cp /app/main .
---> Running in 5c68cb195826
Removing intermediate container 5c68cb195826
---> 6f2e1fb8a611
Step 8/9 : EXPOSE 3000
---> Running in fba77820c1ec
Removing intermediate container fba77820c1ec
---> 182a624d807a
Step 9/9 : CMD ["/dist/main"]
---> Running in 164ba25694bd
Removing intermediate container 164ba25694bd
---> 7cf22cffc472
Successfully built 7cf22cffc472
Successfully tagged bla:latest
go build [-o output] [build flags] [packages]

WORKDIR does not create a directory

Can you tell me why a directory has not been created in the container?
Here is the DockerFile
FROM node:16.13-alpine AS base
WORKDIR /usr/app/test/
RUN npm config set unsafe-perm true
COPY ./test/ .
FROM nginx:1.20-alpine
LABEL version="1.0"
COPY nginx.conf /etc/nginx/nginx.conf
WORKDIR /usr/share/nginx/html
COPY ./test/dist/my-app/ .
I created an image, launched it, I go into it, but there is no directory there
/usr/app/test
There are no errors when creating an image:
Step 1/9 : FROM node:16.13-alpine AS base
---> 710c8aa630d5
Step 2/9 : WORKDIR /usr/app/test/
---> Running in dbe3994e667a
Removing intermediate container dbe3994e667a
---> 54b36120290d
Step 3/9 : RUN npm config set unsafe-perm true
---> Running in e301f90c084f
Removing intermediate container e301f90c084f
---> ca6dc8541ba5
Step 4/9 : COPY ./test/ .
---> e00bf919a630
Here's what's in the container:
Your Dockerfile is defining a multi-stage build, and what you are seeing is expected behavior.
Everything before the second FROM, so in this case FROM nginx:1.20-alpine, is discarded in the final image unless it's copied over.
If you want the first WORKDIR to persist in the final image, you need to explicitly copy over:
FROM node:16.13-alpine AS base
WORKDIR /usr/app/test/
FROM nginx:1.20-alpine
COPY --from=0 /usr/app/test /usr/app/test
WORKDIR /usr/share/nginx/html

docker - unable to change directory of volume

I have had no luck with searching for a solution to this.
I have 2 docker containers. frontend, and api. Both need a folder and its contents models to build.
All of these files live in a folder named Website, so the tree would look like:
Website:
-models
-api
-dockerfile
-frontend
-dockerfile
-docker-compose.yml
I am very new to docker, and believe this is how this should be setup, my dockerfile inside of the api directory is like so:
FROM node:latest
RUN mkdir -p /usr/src/app/models
WORKDIR /usr/src/app
COPY ./api/package*.json ./
RUN npm install
COPY ./api/ /usr/src/app
EXPOSE 3000
EXPOSE 3001
CMD [ "npm", "start" ]
This results in 2 models folders being created in side of the docker container, one containing no files located at path (this is where I want my contents):
root#0aa4496c9077:/usr/src/app/models#
and another folder that contains the content that I want it to have, however it is located at:
root#0aa4496c9077:/usr/src/app/src/models#
My question is: how do I map the folder and contents from the Website directory, to the docker container directory /usr/src/app/models?
my actual docker-compose.yml file is as so:
version: "3"
services:
api:
container_name: api
restart: always
build:
context: ./
dockerfile: ./api/dockerfile
ports:
- "3000:3000"
- "3001:3001"
volumes:
- ./models:./models
links:
- mongo
I have attempted changing the volumes path to be absolute, such as:
volumes:
- $PWD/models:/usr/src/app/models
with no luck :(
The build output of running docker-compose build is:
mongo uses an image, skipping
Building api
Step 1/8 : FROM node:latest
latest: Pulling from library/node
Digest: sha256:521df806339e2e60dfdee6e00e75656e69798c141bd2cff88c0c9a9c50ad4de5
Status: Downloaded newer image for node:latest
---> 4495f296c63b
Step 2/8 : WORKDIR /usr/src/app
---> Using cache
---> 94f87a509559
Step 3/8 : COPY ./api/package*.json ./
---> Using cache
---> 3c2e8c17ebf5
Step 4/8 : RUN npm install
---> Using cache
---> 90dc7d21af18
Step 5/8 : COPY ./api/ /usr/src/app
---> 5fa3a2b219ef
Step 6/8 : EXPOSE 3000
---> Running in 6ad5fbea1ed8
Removing intermediate container 6ad5fbea1ed8
---> b2f8f2f9129c
Step 7/8 : EXPOSE 3001
---> Running in 2342665c8da3
Removing intermediate container 2342665c8da3
---> 9f1162670b55
Step 8/8 : CMD [ "npm", "start" ]
---> Running in fc9d766bd5c1
Removing intermediate container fc9d766bd5c1
---> a7267b99b3c2
Successfully built a7267b99b3c2
Successfully tagged website_api:latest
Thank you very much for any help
Use ./models:./models without $PWD , and it's supposed to work.
./modelsis interpreted as relative to the location of the Compose file.
No need to supply absolute path, just relative path and docker-compose will figure it out.

.NET Core web app won't run in Docker container

I have a vanilla .NET Core 2 web app that I setup in JetBrains Rider and I immediately started working on a Docker environment for it. I followed this guide to get started:
https://docs.docker.com/engine/examples/dotnetcore/
I altered it slightly to come up with this:
FROM microsoft/dotnet:latest AS packager
RUN mkdir -p /opt/build
WORKDIR /opt/build
# Copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin
# --
# Build runtime image
FROM microsoft/dotnet:runtime AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/. .
ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
The image builds and when I go to run the container I get the following output:
dan#mycomputer ~/Desktop/coreapi (master)
$ docker build -t myapp .
Sending build context to Docker daemon 25.09kB
Step 1/12 : FROM microsoft/dotnet:latest AS packager
---> e1a56dca783e
Step 2/12 : RUN mkdir -p /opt/build
---> Using cache
---> 95f9c936d0d1
Step 3/12 : WORKDIR /opt/build
---> Using cache
---> 64f26c356fd7
Step 4/12 : COPY *.csproj .
---> Using cache
---> 38a2fb7ca6bb
Step 5/12 : RUN dotnet restore
---> Using cache
---> 70dbc44d98ae
Step 6/12 : COPY . .
---> Using cache
---> b1019d53a861
Step 7/12 : RUN dotnet publish -c Release -o bin
---> Using cache
---> 8e112606633a
Step 8/12 : FROM microsoft/dotnet:runtime AS runtime
---> cc240a7fd027
Step 9/12 : RUN mkdir -p /opt/app
---> Using cache
---> 954f494febc4
Step 10/12 : WORKDIR /opt/app
---> Using cache
---> b74be941e7dc
Step 11/12 : COPY --from=packager /opt/build/bin/. .
---> Using cache
---> 4c229192d99b
Step 12/12 : ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
---> Using cache
---> fb6ef4015fba
Successfully built fb6ef4015fba
Successfully tagged myapp: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.
dan#mycomputer ~/Desktop/coreapi (master)
$ docker run -p 5001:5001 myapp:latest
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
The app doesn't run and I get a message that says I need to install the SDK. What's up with that? Shouldn't the runtime Docker image have everything neede to run the app?
I was able to find the solution by modifying the ENTRYPOINT to run tail -f /dev/null. From there, I entered the container and saw that the name of the binary adjusts based on your project name which the Docker documentation didn't make clear to me.
I also updated my base images and this solved my problem. Here is my latest Dockerfile below:
FROM microsoft/dotnet:2.1-sdk AS packager
RUN mkdir -p /opt/build
WORKDIR /opt/build
# Copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin
# --
# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
RUN mkdir -p /opt/app
WORKDIR /opt/app
COPY --from=packager /opt/build/bin/. .
EXPOSE 80
ENTRYPOINT ["dotnet", "/opt/app/coreapi.dll"]

Resources