I am trying to use VSCode to debug a jupyter notebook which runs within a docker container.
I am using jupyter/scipy-notebook:python-3.9.7 as base image and running the following command:
CMD ["python", "-m", "ptvsd", "--host", "0.0.0.0", "--port", "49155", "--wait", "--multiprocess", "-m", "jupyter", "lab", "--ip", "0.0.0.0"]
after having installed ptvsd.
The launch.json looks like the following:
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 49155
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}/notebooks",
"remoteRoot": "/home/jovyan"
}
]
where all the relevant code is within the notebooks folder.
After building and "upping" the following docker compose file:
version: '3'
services:
scipy-notebook:
ports:
- '49154:8888'
- '49155:49155'
environment:
- 'JUPYTER_RUNTIME_DIR=/tmp'
- 'JUPYTER_ENABLE_LAB=yes'
build:
context: .
dockerfile: ${dockerfile_src}
image: ${registry}/${repositoryName}:${versionNumber}
the container is correctly waiting for me to attach a visual studio debug session (i.e. "Attaching to <container_name>"), but after I start the debug session, this stops immediately and the jupyter notebook starts normally. Further attempts to attach bring the same result.
I tried to debug a simple .py file within the notebooks folder with the very same structure, and in this case everything works just fine. Here the command:
CMD ["python", "-m", "ptvsd", "--host", "0.0.0.0", "--port", "49155", "--wait", "main.py"]
Related
I'm trying to set up a docker container running a next.js app for development purposes using docker compose, so it can be used along side other containers (such an api or other services), and what I need is the next.js app could compile any change made in real time after an update in the source code is made. So I created a volume for the container mapped to the source code on my host machine. The thing is that the app on the container does not update after some changes are applied on the source code, the container actually updates the files (the volume is mapped correctly) but the application does not applies the changes and does not compile again. The output console for docker compose is this, saying that #next/swc-linux-x64 is not installed:
I believe that there is some issue with the image because when I run npm run dev on my local machine it does not shows that warning and it works well (after some change is made in the source code the app is updated and compiles the changes):
So the questions are: Is it possible to create a development container for a next.js app using Docker? And it'll be able to update/compile after some change is made from the source code on my host machine? What could be the issue that the app does not update any change being made from the source code?
My host OS is Windows 11. And node version: 18.14.0
My project structure is:
The files that I'm using are the following:
Dockerfile.dev:
FROM node:18.14.0
WORKDIR /app
COPY package.json ./
RUN npm install
COPY /public .
COPY /src .
EXPOSE 3000
ENV NODE_ENV=development
CMD ["npm", "run", "dev"]
docker-compose.dev.yml:
services:
frontend:
environment:
NODE_ENV: development
container_name: frontend
build:
context: ./frontend
dockerfile: Dockerfile.dev
restart: always
ports:
- 3000:3000
volumes:
- ./frontend:/app
command: npm run dev
networks:
- my_network
networks:
my_network:
next.config.js:
/**
* #type {import('next').NextConfig}
*/
module.exports = {
output: process.env.NODE_ENV === 'production' ? 'standalone' : 'module',
};
package.json
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"#popperjs/core": "^2.11.6",
"bootstrap": "^5.2.1",
"date-fns": "^2.29.2",
"eslint-config-next": "^13.1.6",
"gray-matter": "^4.0.3",
"next": "^13.1.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"remark": "^14.0.2",
"remark-html": "^15.0.1"
}
}
I want to run a Nx workspace containing a NestJs project in a Docker container, in development mode. The problem is I am unable to configure docker-compose + Dockerfile to make the project reload on save. I'm a bit confused on why this is not working as I configured a small nestjs project(without nx) in docker and it had no issues reloading on save.
Surely I am not mapping the ports corectly or something.
version: "3.4"
services:
nx-app:
container_name: nx-app
build: .
ports:
- 3333:3333
- 9229:9229
volumes:
- .:/workspace
FROM node:14.17.3-alpine
WORKDIR /workspace
COPY . .
RUN ["npm", "i", "-g", "#nrwl/cli"]
RUN ["npm", "i"]
EXPOSE 3333
EXPOSE 9229
ENTRYPOINT ["nx","serve","main"]
Also tried adding a Angular application to the workspace and was able to reload it on save in the container without issues...
Managed to solve it by adding "poll": 500 in project.json of nestJs app/library.
"targets": {
"build": {
"executor": "#nrwl/node:webpack",
...
"options": {
...
"poll": 500
I used this docker-compose file with the Xdebug options that worked for me in a local Xdebug installation, but doesn't work in Docker.
version: '3'
services:
app:
container_name: php7.3-xdebug
image: 'php7.3-xdebug-i'
ports:
- '9090:80'
build:
context: ./
dockerfile: ./dockerfile
volumes:
- ./:/var/www/html
environment:
XDEBUG_CONFIG: zend_extension=xdebug.so xdebug.mode=debug xdebug.start_with_request=yes xdebug.client_host=host.docker.internal
This is the Dockerfile:
FROM php:7.3.28-apache-buster
RUN pecl install xdebug-3.0.4 \
&& docker-php-ext-enable xdebug
COPY . /var/www/html
WORKDIR /usr/src/myapp
and this launch.json in VSCode:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html": "${workspaceFolder}",
},
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9003
}
]
}
I tried a lot of configs but there is no way to make it work.
XDEBUG_CONFIG: zend_extension=xdebug.so xdebug.mode=debug
xdebug.start_with_request=yes xdebug.client_host=host.docker.internal
This line is incorrect:
You can't load extensions through an Xdebug specific environment variable (but the docker-php-ext-enable xdebug line in your Dockerfile should have taken care of this already).
If you want to set settings through XDEBUG_CONFIG, then you should not prefix them with xdebug., which is explained in the documentation.
The documentation also says that only a select set of settings can be set through XDEBUG_CONFIG, and start_with_request is not one of these. It needs to be set in a PHP ini file.
To set Xdebug's mode, you need to use the XDEBUG_MODE environment variable instead, as is explained in the documentation again.
I've created a simple docker with a nodejs server.
FROM node:12.16.1-alpine
WORKDIR /usr/src
COPY ./app/package.json .
RUN yarn
COPY ./app ./app
This works great and the service is running.
Now I'm trying to run the docker with a volume for local development using docker compose:
version: "3.4"
services:
web:
image: my-node-app
volumes:
- ./app:/usr/src/app
ports:
- "8080:8080"
command: ["yarn", "start"]
build:
context: .
dockerfile: ./app/Dockerfile
This is my folder structure in the host:
The service works without the volume. When I add the volume, the /usr/src/app is empty (even though it is full as shown in the folder structure).
Inspecting the docker container I get the following mount config:
"Mounts": [
{
"Type": "bind",
"Source": "/d/development/dockerNCo/app",
"Destination": "/usr/src/app",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
But still, browsing to the folder via shell of vscode show it as empty.
In addition, the command: docker volume ls shows an empty list.
I'm running docker 18.09.3 on windows 10.
Is there anything wrong with the configuration? How is it supposed to work?
Adding the volume to your service will remove all the files /usr/src/app and mount the content of ./app from your host machine. This also means that all files generated by running yarn in the docker image will be lost because they exist only in the docker image. This is the expected behaviour of adding volume in docker and it is not a bug.
volumes:
- ./app:/usr/src/app
Usually and for none development Envs you don't need the volume at all here.
If you would like to see the files on your host, you need to run yarn command from docker-compose (you can use an entry point)
I have a docker-compose used for production which I'm hoping to incorporate with VS Code's dockerized development environment.
./docker-compose.yml
version: "3.6"
services:
django: &django-base
build:
context: .
dockerfile: backend/Dockerfile_local
restart: on-failure
volumes:
- .:/website
depends_on:
- memcached
- postgres
- redis
networks:
- main
ports:
- 8000:8000 # HTTP port
- 3000:3000 # ptvsd debugging port
expose:
- "3000"
env_file:
- variables/django_local.env
...
Note how I'm both forwarding and exposing port 3000 here. This is a result of me playing around to get what I need working. Not sure if I need one or the other or both.
My ./devcontainer then looks like the following:
./devcontainer/devcontainer.json
{
"name": "Dev Container",
"dockerComposeFile": ["../docker-compose.yml", "docker-compose.extend.yml"],
"service": "dev",
"workspaceFolder": "/workspace",
"shutdownAction": "stopCompose",
"settings": {
"terminal.integrated.shell.linux": null,
"python.linting.pylintEnabled": true,
"python.pythonPath": "/usr/local/bin/python3.8"
},
"extensions": [
"ms-python.python"
]
}
.devcontainer/docker-compose.extended.yml
version: '3.6'
services:
dev:
build:
context: .
dockerfile: ./Dockerfile
external_links:
- django
volumes:
- .:/workspace:cached
command: /bin/sh -c "while sleep 1000; do :; done"
The idea is that I want to be able to run VS code attached to the dev service, which from there I want to run the debugger attached to the django service using the following launch.json config:
{
"name": "WP",
"type": "python",
"request": "attach",
"port": 3000,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/website"
}
]
},
I get an error when doing this though, where VS Code says connect ECONNREFUSED 127.0.0.1:3000. How can I get the ports mapped so this will work? Is it even possible?
Edit
Why not just attach directly to the django service?
The dev container simply contains python and node runtimes for linting and intellisense purposes while using VS Code. The idea behind creating a new service devoted specifically to debugging in the dev environment is that ./docker-compose.yml contains more than a few services that some of the devs on my team like to turn off sometimes to keep resource consumption low. By creating a container specifically for dev, it also makes it easier to setup .devcontainer-devcontainer.json to add things like extensions to one container without needing to add them after attaching to the running "non-dev" container. If this were to work, VS Code would be running within the dev container (see this).
I was able to solve this by changing the host in the launch.json from localhost to host.docker.internal. The resulting launch.json configuration then looks like this:
{
"name": "WP",
"type": "python",
"request": "attach",
"port": 3000,
"host": "host.docker.internal",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/web-portal"
}
]
},