deno inspect with docker compose - docker

I am trying to inspect a deno app that is ran inside a docker container with docker-compose.
docker-compose configuration is as follows:
services:
api_bo:
image: denoland/deno:debian-1.23.4
volumes:
- type: bind
source: .
target: /usr/src
ports:
- 9229:9229
- 6005:3000
command: bash -c "cd /usr/src/packages/api_bo && deno task inspect"
depends_on:
- mongo_db
environment:
- MONGO_URL=mongodb://mongo_db:27017/academy_db
- DB_NAME=academy_db
- PORT=3000
deno.json is as follows:
{
"compilerOptions": {
"allowJs": false,
"strict": true
},
"lint": {
"files": {
"include": ["src/"],
"exclude": ["src/types.ts"]
},
"rules": {
"tags": ["recommended"],
"include": [
"ban-untagged-todo",
"no-explicit-any",
"no-implicit-any",
"explicit-function-return-type"
],
"exclude": ["no-debugger", "no-console"]
}
},
"tasks": {
"start": "deno run -A --watch src/app.ts",
"inspect": "deno run -A --inspect src/app.ts"
},
"importMap": "../../import_map.json"
}
Chrome with chrome://inspect does not detect the running process.
When running out of docker with deno run, it works just fine.
It seems that deno only listens to connections from 0.0.0.0 and thus it cannot be inspected using docker port forwarding.

Deno and NodeJS share the same Inspector Protocol from V8, for more see V8 / Docs / Inspector.
And also (lucky) the same parameter "--inspect=[HOST:PORT]" and "--inspect-brk=[HOST:PORT]" so on,
for more see NodeJS / API / Inspect or NodeJS / API / Inspect Brk (THIS is the documentation from NODEJS, so be careful!)
The main "problem" (security reasons) is that NodeJS and Deno Inspector Protocol listen only to localhost / 127.0.0.1 and Docker can't and won't forward these ports. But with the parameter "--inspect" you can change the Host and Port.
Deno CLI
deno run --inspect=0.0.0.0:9229 ./src/my-big-cool-file.ts
Dockerfile
# ...
EXPOSE 9229
# ...
CMD ["run", "--inspect=0.0.0.0:9229", "...", "./src/main.ts"]
TLDR;
your deno.json
//...
"tasks": {
"start": "deno run -A --watch src/app.ts",
"inspect": "deno run -A --inspect=0.0.0.0:9229 src/app.ts"
},
//...
your docker-compose.yml
services:
# ...
api_bo:
# ...
ports:
# THIS IS IMPORTANT, FORWARD DEBUG PROTOCOLS ONLY TO YOUR LOCALHOST! **1
- 127.0.0.1:9229:9229
**1 except: when you need it, and you know what you are doing!!!

Related

Dockerize Vue.js: hot-reload is not working for vue/cli#4.5 or later versions

I greatly appreciate your effort and the time to solve unresponsive hot-reload function when trying to run Vue.js app on Docker container using Docker engine on Windows 10 while WSL2 active, please take a look at below configurations:
Vue.Setup.Dockerfile
FROM node:17-alpine
EXPOSE 8080
WORKDIR /app/frontend
RUN npm --force install -g #vue/cli#4.5.15
COPY /frontend /app/frontend
ENV PATH /app/frontend/node_modules/.bin:$PATH
CMD [ "npm", "run", "serve" ]
docker-compose.yml
version: "3.8"
services:
vue:
build:
context: .
dockerfile: dockerfiles/Vue.Setup.Dockerfile
restart: always
ports:
- "127.0.0.1:8080:8080"
container_name: vue_ui
volumes:
- ./frontend/:/app/frontend/
- /app/frontend/node_modules
environment:
- CHOKIDAR_USEPOLLING=true
vue.config.js
module.exports = {
publicPath:
process.env.NODE_ENV === "production"
? "/static/dist/"
: "http://127.0.0.1:8080",
pages: {
index: {
entry: 'src/main.js',
template: 'public/index.html',
filename: 'index.html',
title: 'QuestionTime',
chunks: ['chunk-vendors', 'chunk-common', 'index']
},
},
// Webpack configuration
devServer: {
host: "0.0.0.0",
port: "8080",
hot: true,
headers: {"Access-Control-Allow-Origin": "*"},
devMiddleware: {
publicPath: "http://127.0.0.1:8080",
writeToDisk: (filePath) => filePath.endsWith("index.html"),
},
static: {
watch: {
ignored: "/node_modules/",
usePolling: true,
},
},
client: {
webSocketURL: {
/* You need to config this option, otherwise the below error will occur
in your browser console when trying to connect to development server
from another Docker container:
WebSocket connection to 'ws://127.0.0.1:<port-number>/ws' failed
*/
hostname: "0.0.0.0",
pathname: "/ws",
port: 8080,
},
},
},
};
Note: When run the command:
docker-compose up
The below message will show:
It seems you are running Vue CLI inside a container.
Since you are using a non-root publicPath, the hot-reload socket
will not be able to infer the correct URL to connect. You should
explicitly specify the URL via devServer.public.
Access the dev server via http://localhost:<your container's
external mapped port>
FYI: the option:
devServer.public
is no longer available in Vue/cli#4 or later versions.
WORKAROUND
solution
Thanks,

In VSCode, 'Python: Remote Attach' fails to connect to a running Docker Container

Good evening,
I have a container which is running and ready to connect. In VSCode I've tried to 'Attach Visual Studio Code' to open a new Dev Container, select the sources, hoping I can debug.
I'm unable to select breakpoints and the code isn't running.
Nothing happens.
I've also tried 'Python: Attach Remote'.
Nothing happens and there's no errors.
Launch.json:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 3000
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
},
]
}
Docker Compose.yml
services:
sfunc:
image: sfunc
build:
context: .
dockerfile: ./Dockerfile
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --log-to-stderr --wait-for-client --listen 127.0.0.1:3000 home/site/wwwroot/TimerTrigger/__init__.py "]
ports:
- 3000:3000
How could I troubleshoot this ?
Thank you
Those hostnames didn't work for me. Using localhost in the launch.json and 0.0.0.0 as the host in the --listen option worked.

connect from inside a docker container to IP address on host network

Question: (the short version)
When using Docker for application development, how do I configure a Docker-compose file to let me access the IP address of a server on the host network from inside the docker container? I don't want to access the host itself -- but instead want to access another server on the same network as the host. I also need to have various docker containers be able to talk to each other.
What is the cleanest way to allow access to host-network IPv4 resources from inside of a docker container and still allow inter-container communication?
All the background and details are below (aka the long version).
Setup:
I'm writing an application that polls a Modbus server to collect measured data at a given interval. The Modbus server is at 192.168.1.50 port 502 on my local network. I can successfully ping that server from my computer.
Here's my development approach:
application is inside a docker container
developing in a Mac environment
using Django-3.1/postgres to build the UI and store the data (for now)
using the pymodbus library to manage communication between the application and the remote Modbus server
Here's the Docker-compose file that creates the application environment:
version: '3.8'
services:
db:
container_name: db
image: postgres:10.7
environment:
POSTGRES_PASSWORD: pwd
POSTGRES_DB: app_db
volumes:
- ./data/postgres:/var/lib/postgresql/data
ports:
- 5432:5432
web:
container_name: web
build:
context: ../.
dockerfile: ./_app/dev/Dockerfile
volumes:
- ../.:/app
ports:
- 8000:8000
depends_on:
- db
env_file:
- app.env
command: ./manage.py runserver 0.0.0.0:8000
And here's the Dockerfile:
FROM python:3.8.5-buster
RUN mkdir /app
WORKDIR /app
RUN apt-get update -y
RUN apt-get install inetutils-traceroute -y
COPY ./_app/dev/python_reqs.txt /app/python_reqs.txt
RUN pip install --upgrade pip
RUN pip install -r python_reqs.txt
# Install wait-for-it
COPY ./_app/wait-for-it.sh /usr/local/bin/
RUN chmod u+x /usr/local/bin/wait-for-it.sh
# Configure custom entrypoint to wait for postgres and run webserver
COPY ./_app/dev/custom-entrypoint /usr/local/bin/
RUN chmod u+x /usr/local/bin/custom-entrypoint
ENTRYPOINT ["custom-entrypoint"]
EXPOSE 8000
Problem:
From inside the 'web' docker container I cannot access the Modbus server on the host LAN. I cannot ping its IP address (192.168.1.50).
traceroute 192.168.1.50 provides no useful info. It just fails.
When I run docker-compose up a docker network called '_app_default' is created. Here's what I get when I run docker network inspect _app_default:
[
{
"Name": "_app_default",
"Id": "59f0836c2f3017c537f973a38ab3a55ad96e2ed737c4ac111fc0e911fa0c8a67",
"Created": "2020-09-13T18:51:14.6395366Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.112.0/20",
"Gateway": "192.168.112.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"28af77ef2d6f99812cb392c8f9eca045b150256c464befc3bb249c9de9e653c1": {
"Name": "db",
"EndpointID": "d657f2f29699ada859447c919ad2c94195a6e9a7ae7eeb35989560d2717183c5",
"MacAddress": "02:42:c0:a8:70:02",
"IPv4Address": "192.168.112.2/20",
"IPv6Address": ""
},
"a8a619d664a27286e276614e549514dff2c15c5c3c44c39e127fec54ec2b5c6b": {
"Name": "web",
"EndpointID": "8f1661c6fa24104a70171d1f830e1fe34ed8d89bacd55c9b9d05dc62f64112c0",
"MacAddress": "02:42:c0:a8:70:03",
"IPv4Address": "192.168.112.3/20",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "_app",
"com.docker.compose.version": "1.27.0"
}
}
]
What I Already Tried:
I tried using network_mode: "host" in the 'web' service definition. That did not help.
I did my best to read through the Docker networking documentation but I can't find a straightforward solution to this situation and I get all turned around -- I'm unfortunately not good with networking topics -- yet :) . Do I need an 'overlay' network? Or a 'macvlan'? Help!
Any assistance that you StackOverflow superstars can send would be greatly appreciated. Thank you!!
-Allan
It looks like you have overlapping ip ranges of docker network and local network.
To solve this you need to override docker network address pool e.g.
edit docker daemon config
$ cat ~/.docker/daemon.json
{
"debug" : true,
"default-address-pools" : [
{
"base" : "172.31.0.0/16",
"size" : 24
}
]
}

Why can't I remote debug with Docker/VScode?

I'm trying to remotely debug a Go app using Docker/VScode. My files look like:
main.go
package main
import (
"log"
"net"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello, " + message + "!"
w.Write([]byte(message))
})
log.Print("starting web server")
listener, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
log.Printf("Start listening: %v", listener.Addr().String())
if err := http.Serve(listener, nil); err != nil {
log.Fatal(err)
}
}
Dockerfile
FROM golang:1.12
ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
RUN go get github.com/go-delve/delve/cmd/dlv
# set the working directory to /go/src
WORKDIR $GOPATH/src
# Copy everything from the current directory to the working directory inside the container
# (as set by WORKDIR)
COPY . .
# 8080 is for the web application
EXPOSE 8080 2345
docker-compose.yml
version: "3.0"
services:
web:
container_name: go-delve-docker-vscode-example
build: "./"
ports:
- "8080:8080"
- "2345:2345"
security_opt:
- "seccomp:unconfined"
tty: true
stdin_open: true
# command: go run main.go
command: dlv debug --headless --listen=:2345 --api-version=2 --log main.go
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Delve into Docker",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "/go/src/",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"showLog": true
}
]
}
I can see docker logs for my app, I know it's listening on 2345. But for whatever reason, I can't seem to debug the app. The other issue I'm seeing is, I can't see any logs for trying to run the launch.json.
Edit: I'm running this on Mac OSX, using Docker Desktop.
Docker desktop for Mac runs docker inside a Virtual Machine.
Instead of trying to reach 127.0.0.1:2345, you should use your Virtual machine IP.

port number not change for docker-compose

I have specified port-mapping in docker-compose, but it is still not working, i still have to access site using the port no specified in expose
below is my docker-compose.yml
version: '2'
networks:
default:
external:
name: nat
services:
website:
build:
context: '.'
dockerfile: "./iis.dockerfile"
ports:
- 3000:8081
and the corrosponding dockerfile
FROM microsoft/iis
RUN ["powershell.exe", "Install-WindowsFeature NET-Framework-45-ASPNET"]
RUN ["powershell.exe", "Install-WindowsFeature Web-Asp-Net45"]
ADD www/ c:\\webapp
EXPOSE 8081
RUN powershell New-Website -Name 'web-app' -Port 8081 -PhysicalPath 'c:\webapp' -ApplicationPool '.NET v4.5'
I have to access app using: http://192.168.105.33:8081/, if I do it using port 3000 it does not work.
Is there anything missing in my above configuration?? OS: windows server 2016, using windows containers with hyper-v and docker-compose up -d to get containers up and running...
docker-compose inspect gives me below output
"Ports": {
"8081/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3000"
}
]
},
docker ps gives me below output
eb7aa1e74b7f website_website "C:\\ServiceMonitor..." 14 minutes ago Up 14 minutes 0.0.0.0:3000->
8081/tcp website_website_1
dokcer-compose ps gives me below output
Name Command State Ports
--------------------------------------------------------------------------------
website_website_1 C:\ServiceMonitor.exe w3svc Up 0.0.0.0:3000->8081/tcp
So I should be able to access it on host using port 3000.
Maybe you're launching your container with docker-compose run? There is a difference in port mapping between docker-compose run and docker-compose up [-d] [service] In this case the port configuration will be ignored by design
You can use --service-ports flag or manually expose them using -p flag

Resources