Nestjs Docker Debug - docker

I have a simple Dockerfile
FROM node:12.13-alpine As development
WORKDIR /usr/src/app
COPY package*.json ./
RUN apk add --no-cache bash && npm install --only=development
COPY . .
RUN npm run build
FROM node:12.13-alpine as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/dist ./dist
CMD ["node", "dist/main"]
my docker-compose is:
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- 9229:9229
command: npm run start:debug
env_file:
- .env
Inside my package.json and script, I have:
"start:debug": "nest start --debug 0.0.0.0:9229 --watch"
I added the conf in my visual Studio Code (.vscode/launch.json)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug",
"address": "127.0.0.1",
"port": 9229,
"sourceMaps": true,
"restart": true,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src/app",
"skipFiles": ["<node_internals>/**"]
}
]
}
After that I can run docker-compose up --build and start my debugger but the code doesn't stop at my breakpoint.
I use Mac 12.6 and Docker version 20.10.13

Related

Code Not Shown in VS code while debugging asp.net docker container

I am learning docker with asp.net core. Create a mvc application, dockerfile and docker-compose.yml file. I have docker file as below:
FROM mcr.microsoft.com/dotnet/sdk:7.0 as debug
#install debugger for NET Core
RUN apt-get update
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
RUN mkdir /app/
WORKDIR /app/
COPY ./src/testapp.csproj /app/testapp.csproj
RUN dotnet restore
COPY ./src/ /app/
RUN mkdir /out/
RUN dotnet publish --no-restore --output /out/ --configuration Release
EXPOSE 80
CMD dotnet run --urls "http://0.0.0.0:80"
Also, I have docker compose file as below:
version: "3.0"
services:
db:
image: postgres
restart: always
ports:
- "5432"
volumes:
- postgres:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: sachin
pg_admin:
image: dpage/pgadmin4
restart: always
ports:
- "5555:80"
volumes:
- pg_admin:/var/lib/pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: sachin.maharjan#dishhome.com.np
PGADMIN_DEFAULT_PASSWORD: password
web:
container_name: csharp
build:
context: .
target: debug
ports:
- "5000:80"
volumes:
- ./src:/app/
depends_on:
- db
volumes:
postgres:
pg_admin:
I have installed docker extension and C# extension in my vs code. I have lunch.js file inside .vscode folder.
The content of lunch.js:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Docker Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeProgram": "docker",
"pipeArgs": [ "exec", "-i", "csharp" ],
"debuggerPath": "/root/vsdbg/vsdbg",
"pipeCwd": "${workspaceRoot}",
"quoteArgs": false
},
"sourceFileMap": {
"/work": "${workspaceRoot}/src/"
}
},
]
}
The debugger stop at break point but it shows error like this.
Is this error with volume mapping or did I set up lunch.json wrong?
I noticed in lunch.json in the source map section, I mapped the wrong folder in local file system with file system with docker.
Previously, MapSetting section was like below:
"sourceFileMap":
{
"/work": "${workspaceRoot}/src/"
}
Then I change folder mapping with right one and erorr is solved.
"sourceFileMap": {
"/src": "${workspaceRoot}/src/"
}

Dockerfile CMD does not interpolate ENV

I have tried every combination I can think of.
docker-compose.yml
version: "3.1"
services:
node-server:
build:
context: .
dockerfile: Dockerfile
args:
- MYVAR=${MYVAR}
environment:
- ENV_MYVAR=${MYVAR}
ports:
- 8000:8000
env_file:
- .env
# command: ["npm", "run", "_${MYVAR}_"] This works, but for the sake of testing CMD in Dockerfile, I can't get that to work
Dockerfile
FROM node:14
ARG MYVAR
ENV ENV_MYVAR="${MYVAR}"
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE ${port}
RUN echo "1"
RUN echo $MYVAR # Works
CMD [ "npm", "run", "_${ENV_MYVAR}_"] # OUTPUTS as _${ENV_MYVAR}_". Does not interpolate!!
.env
MYVAR=boot
package.json
{
"name": "node-server-ts",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"test": "echo 'test'",
"boot": "echo 'boot'",
"_boot_": "echo '_boot_'"
}
}
terminal
docker-compose up
Expect:
npm run _boot_
error - node-server_1 | ${ENV_MYVAR}
Try removing the square brackets.
CMD npm run "_${ENV_MYVAR}_"
See this StackOverflow question about the differences

vscode Docker debugging csharp.listRemoteProcess failed

I have a .Net 5 WebApi that i want to host and debug in Docker.
when i run the docker-compose the project launches correctly.
When attaching the debugger i get following error:
Running the contributed command csharp.listRemoteProcess failed
my docker file:
FROM mcr.microsoft.com/dotnet/sdk:5.0 as debug
ENV DOTNET_USE_POLLING_FILE_WATCHER=true
ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000/tcp
WORKDIR /app
COPY . /app
RUN apt-get update
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
ENTRYPOINT ["dotnet", "watch", "run", "--project myproject.csproj"]
the docker-compose file
version: '3.4'
services:
server:
build:
context: ./myproject
target: debug
ports:
- "5000:5000"
container_name: myproject
volumes:
- ./myproject:/app
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Docker Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeProgram": "docker",
"pipeArgs": [ "exec", "-i", "csharp" ],
"debuggerPath": "/root/vsdbg/vsdbg",
"pipeCwd": "${workspaceRoot}",
"quoteArgs": false
},
"sourceFileMap": {
"/work": "${workspaceRoot}/myproject/"
}
}
]
}

Switch environment files in vue with docker compose

I want to either pass Vue environment variables to my Vue client with docker compose or tell my client to use a diffenret .env file.
The problem is i cannot access the VUE_APP_ROOT_URL variable nor can I switch between the .env files.
services:
client_test:
build: ./papasmuenzenfrontend/.
container_name: vue_test
restart: always
environment:
- NODE_ENV:test
- VUE_APP_ROOT_URL=localhost:50598
ports:
- '8081:8080'
Dockerfile
FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 8080
CMD [ "http-server", "dist" ]
I figured it out how to do it:
In my package.json I have the following configuration
"test_build": "vue-cli-service build --mode test",
"production": "vue-cli-service build --mode production",
"development": "vue-cli-service build --mode development"
docker-compose file:
client_test:
args:
- myenv=development
docker file:
ARG myenv RUN npm run $myenv
When building the project the $myenv gets passed to the build in the package.json where it then switches to the different environment files.

Docker Compose build command using Cache and not picking up changed files while copying to docker

I have a docker-compose.yml file comprising of two services (both based on a DockerFile). I have build the images once (using command: docker-compose build) and they were up and running once I ran this command (docker-compose up).
I had to change the source code used for one of the services, however, when I rebuilt the images (docker-compose build), the code changes were not reflected once I ran the services (docker-compose up).
docker-compose.yml
version: '2'
services:
serviceOne:
build:
context: ./ServerOne
args:
PORT: 4000
ports:
- "4000:4000"
env_file:
- ./ServerOne/.env
environment:
- PORT=4000
serviceTwo:
build:
context: ./serviceTwo
args:
PORT: 3000
ports:
- "3000:3000"
env_file:
- ./serviceTwo/.env
environment:
- PORT=3000
- serviceOne_URL=http://serviceOne:4000/
depends_on:
- serviceOne
serviceOne/DockerFile
FROM node:8.10.0
RUN mkdir -p /app
WORKDIR /app
ADD package.json package-lock.json /app/
RUN npm install
COPY . /app/
RUN npm build
EXPOSE ${ACC_PORT}
CMD [ "npm", "start" ]
serviceTwo/DockerFile
FROM node:8.10.0
RUN mkdir -p /app
WORKDIR /app
ADD package.json package-lock.json /app/
RUN npm install
COPY . /app/
RUN npm build
EXPOSE ${ACC_PORT}
CMD [ "npm", "start" ]
Following is the output of the docker-compose when it is ran for the second time.
It is some how using the cached images again when COPY and npm build command are ran.
How could the DockerFile or docker-compose file be changed so that the new source code is deployed?
You can force the build to ignore the cache by adding on the --no-cache option to the docker-compose build

Resources