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

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/"
}

Related

Nestjs Docker Debug

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

Docker compose conditional detach on npm script

I want to run my node application with only one command : npm start and based on the NODE_ENV, it will either run nodemon app.js if NODE_ENV=dev or node app.js if NODE_ENV=production but in DETACHED MODE.
What I have so far :
package.json
"start": "docker-compose up --build --force-recreate api",
"api:dev": "nodemon app.js",
"api:prod": "node app.js"
Dockerfile
FROM node:16-alpine
WORKDIR /app
COPY package.json .
RUN npm install
ENTRYPOINT ["/bin/sh"]
CMD ["-c", "if [ \"$NODE_ENV\" = \"dev\" ]; then npm run api:dev; else npm run api:prod; fi"]
And finally the docker-compose file
api:
restart: always
container_name: my_api
build:
context: .
dockerfile: ./docker/api/Dockerfile
depends_on:
- postgres
ports:
- "${API_PORT}:${API_PORT}"
env_file: .env
networks:
- back
volumes:
- .:/app
- node_modules:/app/node_modules
logging:
options:
max-file: "10"
max-size: "10m"
Where/how can I setup the conditional detach mode? I know I should use -d for the detach docker-compose up but I want it conditional based on the NODE_ENV.

Multi-stage build fail to find file

It's my first I use multi-stage and I have this error when I run the docker-compose up:
worker_1 | Error: Unable to access jarfile worker-jar-with-dependencies.jar
So it looks like it doesn't found the file.
Dockerfile
FROM maven:3.5-jdk-8-alpine AS builder
ADD pom.xml .
RUN mvn dependency:resolve
#RUN /bin/sh -c cd src/
RUN mvn package
FROM openjdk:8-jre-alpine
COPY --from=builder target/worker-jar-with-dependencies.jar .
#RUN java -jar worker-jar-with-dependencies.jar
CMD ["java", "-jar", "worker-jar-with-dependencies.jar"]
and how it looks my docker-compose.yml
version: '3.0'
services:
poll:
build: poll/.
ports:
- "5000:80"
networks:
- poll-tier
redis:
image: "redis"
expose:
- "6379"
networks:
- back-tier
- poll-tier
worker:
build: worker/.
networks:
- back-tier
db:
image: "postgres"
volumes:
- "db-data:/var/lib/postgresql/data"
networks:
- back-tier
- result-tier
environment:
POSTGRES_PASSWORD: popeye
result:
build: result/.
ports:
- "5001:80"
networks:
- result-tier
volumes:
db-data:
networks:
poll-tier: {}
result-tier: {}
back-tier: {}
It doesn't look like you are adding your source code in the Dockerfile. You only have an ADD command for your pom, but there isn't one for your source. I would say adding something like "COPY src /src" before your "RUN mvn package".
Not sure if you are using springboot, but here is an example of a multi stage build for a springboot app:
FROM maven:3.6.3-jdk-11 AS builder
WORKDIR /workdir/server
COPY pom.xml /workdir/server/pom.xml
RUN mvn dependency:go-offline
COPY src /workdir/server/src
RUN mvn install
RUN mkdir -p target/depency
WORKDIR /workdir/server/target/dependency
RUN jar -xf ../*.jar
FROM openjdk:11-jre-slim
EXPOSE 8080
VOLUME /tmp
ARG DEPENDENCY=/workdir/server/target/dependency
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"]
You can find this and other good docker-compose examples in this docker repo: https://github.com/docker/awesome-compose

Docker Error: Cannot cerate container for service <container name>: status code not OK but 500

I used docker-compose to create docker containers in my React, Node.js, and Postgres structured project.
After I created Dockerfile and docker-compose.yml, I did docker up 'docker-compose up --build.'
Then, I wasn't be able to create containers, and get an errors.
I get Error says:
Error 1
Error 2
Error 3
How can I fix it and successfully build containers?
Here is a docker-compose.yml file in './'
version: '3'
services:
server:
container_name: mylivingcity_server
build: ./server
expose:
- 3001
ports:
- 3001:3001
volumes:
- ./server/config:/usr/src/app/server/config
- ./server/controllers:/usr/src/app/server/controllers
- ./server/db:/usr/src/app/server/db
command: npm run start
postgres:
image: postgres:12
container_name: mylivingcity_postgres
ports:
- 5432:5432
volumes:
- ./postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=mylivingcity
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
frontend:
container_name: mylivingcity_frontend
build: ./frontend
expose:
- 3000
ports:
- 3000:3000
volumes:
- ./frontend/src:/usr/src/app/frontend/src
- ./frontend/public:/usr/src/app/frontend/public
command: npm start
stdin_open: true
Here is a Dockerfile in './frontend'
FROM node:12
# Create frontend directory
RUN mkdir -p /usr/src/app/frontend/
WORKDIR /usr/src/app/frontend/
# Install dependencies
COPY package*.json /usr/src/app/frontend/
RUN npm install
COPY . /usr/src/app/frontend/
CMD [ "npm" , "start" ]
Here is a Dockerfile in './server'
FROM node:12
# Create server directory
RUN mkdir -p /usr/src/app/server/
WORKDIR /usr/src/app/server/
# Install dependencies
COPY package*.json /usr/src/app/server/
RUN npm install
COPY . /usr/src/app/server/
CMD [ "npm" , "run" , "start" ]

How to pass environment variable from docker-compose to the project?

I'm using AWS ECS repository for docker images.
My docker-compose.yml file looks like:
version: "3"
services:
my-front-end:
image: myFrontEndImage:myTag
links:
- my-back-end
ports:
- "8080:8080"
logging:
driver: 'json-file'
options:
max-size: "50m"
my-back-end:
image: myBackEndImage:myTag
ports:
- "3000:3000"
logging:
driver: 'json-file'
options:
max-size: "50m"
And what I need is, to be able to pass a environment variable from the docker-compose file, into my docker image.
What I tried was adding the lines for environment (following the example).
version: "3"
services:
my-front-end:
image: myFrontEndImage:myTag
links:
- my-back-end
environment:
- BACKEND_SERVER_PORT=3001
ports:
- "8080:8080"
logging:
driver: 'json-file'
options:
max-size: "50m"
my-back-end:
image: myBackEndImage:myTag
ports:
- "3000:3000"
logging:
driver: 'json-file'
options:
max-size: "50m"
And then in my project (which is a VueJS project) I'm trying to access it by process.env.BACKEND_SERVER_PORT. But I do not see my value and when I tried console.log(process.env); I see that it has only the value {NODE_ENV: "development"}.
So my question here is, how to pass the env variable from the docker-compose to my docker image, so that I will be able to use it inside my project?
Everything in the project works fine, I'm working on this project a long time and docker-compose file works, it's just, now when I have a need of adding this environment variable, I can't make it work.
EDIT: adding few more files, per request in comment.
The .Dockerfile for my-front-end looks like:
FROM node:8.11.1
WORKDIR /app
COPY package*.json ./
RUN npm i npm#latest -g && \
npm install
COPY . .
CMD ["npm", "start"]
As mentioned, this is an VueJS application and here is the part of package.json which you may be interested in:
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
"build": "node build/build.js"
},
While the .Dockerfile for my-back-end looks like:
FROM node:8.11.1
WORKDIR /app/server
COPY package*.json ./
RUN npm i npm#latest -g && \
npm install
COPY . .
CMD ["npm", "start"]
My back-end is actually an express.js app that is listening on a separate port and the app is placed in a folder server under the root of the project.
Here is the part of package.json which you may be interested in:
"scripts": {
"start": "nodemon src/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
I think you are doing everything right in terms of configuring docker-compose. And it seems like there is some nuances on passing an environment variable to VueJS application.
According to answers to this question you need to name your variables with VUE_APP_* to be able to get them from client-side

Resources