If you want to debug in Docker Compose, run the command Docker Compose Up using one of the two Docker Compose files as described in the previous section, and then attach using the appropriate Attach launch configuration. Launching directly using the normal launch configuration does not use Docker Compose.,To use Docker Compose in VS Code using the Docker extension, you should already be familiar with the basics of Docker Compose.,If you already have one or more Dockerfiles, you can add Docker Compose files by opening the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), and using the Docker: Add Docker Compose Files to Workspace command. Follow the prompts.,With the docker-compose files, you can now specify port mappings in the docker-compose files, rather than in the .json configuration files. For examples, see the Docker Compose documentation.
Configure the debugging port in docker-compose.debug.yml
. This is set when you create the file, so you might not need to change it. In the example below, port 9229 is used for debugging on both the host and the container.
version: '3.4'
services:
node - hello:
image: node - hello
build: .
environment:
NODE_ENV: development
ports:
-3000 -
9229: 9229
command: node--inspect = 0.0 .0 .0: 9229 . / bin / www
Here's an example that shows the Node.js launch configuration - Attach:
"configurations": [{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"remoteRoot": "/usr/src/app",
"port": 9229 // Optional; otherwise inferred from the docker-compose.debug.yml.
},
// ...
]
You'll be prompted to choose the host machine (for example, localhost) and port you want to use for debugging. The default debugging port for Python is 5678. If you have multiple apps, you need to change the port for one of them, so that each app has a unique port. You can point to the right debugging port in the launch.json
, and save the file. If you omit this, the port will be chosen automatically.
"configurations": [{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}]
}
Right-click on the docker-compose.debug.yml
file (example shown below) and choose Compose Up.
version: '3.4'
services:
pythonsamplevscodedjangotutorial:
image: pythonsamplevscodedjangotutorial
build:
context: .
dockerfile: . / Dockerfile
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload"]
ports:
-8000: 8000 -
5678: 5678
VS Code tries to copy vsdbg
from the host machine to the target container using a default path. You can also provide a path to an existing instance of vsdbg
in the Attach configuration.
"netCore": {
"debuggerPath": "/remote_debugger/vsdbg"
}
To skip this step, specify the container name in the Attach configuration in launch.json:
"containerName": "Your ContainerName"
For more info see the documentation
app1:
command: python app.py
build: app1 /
net: 'host'
app2:
command: python app.py
build: app2 /
net: 'host'
Additionally you should start your app1 in daemon mode and app2 in the foreground mode for debugging purposes:
docker - compose up - d app1 docker - compose run app2
06/07/2022
(.NET Core 3 and later only) Editing your code and refreshing the running site as described in this section is not enabled in the default templates in .NET Core >= 3.0. To enable it, add the NuGet package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation. In Startup.cs, add a call to the extension method IMvcBuilder.AddRazorRuntimeCompilation
to the code in the ConfigureServices
method. You only need this enabled in DEBUG mode, so code it as follows:
public IWebHostEnvironment Env {
get;
set;
}
public void ConfigureServices(IServiceCollection services) {
IMvcBuilder builder = services.AddRazorPages();
#if DEBUG
if (Env.IsDevelopment()) {
builder.AddRazorRuntimeCompilation();
}
#endif
// code omitted for brevity
}
Modify the Startup
method as follows:
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { Configuration = configuration; Env = webHostEnvironment; }
Add the following HTML content to the end of the file, and then save the changes.
<h1>Hello from a Docker container!</h1>
In the output window, when the .NET build is finished and you see the following lines, switch back to your browser and refresh the page:
Now listening on: http: //*:80
Application started.Press Ctrl + C to shut down.
Replace the contents of the OnGet
method with the following code:
ViewData["Message"] = "Your application description page from within a container";
Replace the contents of the Main
method with the following code:
System.Console.WriteLine("Hello, world!");
The above command would both start your application with the debug profile enabled. Using the docker-compose.yml file above, this would start the services backend, db and phpmyadmin.,The core services of your application should not be assigned profiles so they will always be enabled and automatically started.,Services without a profiles attribute will always be enabled, i.e. in this case running docker compose up would only start backend and db.,This allows one to define additional services in a single docker-compose.yml file that should only be started in specific scenarios, e.g. for debugging or development tasks.
version: "3.9"
services:
frontend:
image: frontend
profiles: ["frontend"]
phpmyadmin:
image: phpmyadmin
depends_on:
-db
profiles:
-debug
backend:
image: backend
db:
image: mysql
$ docker compose--profile debug up $ COMPOSE_PROFILES = debug docker compose up
$ docker compose--profile frontend--profile debug up $ COMPOSE_PROFILES = frontend, debug docker compose up
version: "3.9"
services:
backend:
image: backend
db:
image: mysql
db - migrations:
image: backend
command: myapp migrate
depends_on:
-db
profiles:
-tools
# will only start backend and db $ docker compose up - d # this will run db - migrations(and - if necessary - start db) # by implicitly enabling profile `tools` $ docker compose run db - migrations
version: "3.9"
services:
web:
image: web
mock - backend:
image: backend
profiles: ["dev"]
depends_on:
-db
db:
image: mysql
profiles: ["dev"]
phpmyadmin:
image: phpmyadmin
profiles: ["debug"]
depends_on:
-db