This Python script, docker-run-n-attach.py
, provides a convenient way to run a Docker container and automatically attach to it using Visual Studio Code.
- Python 3
- Docker
- Visual Studio Code with the Remote - Containers extension installed
-
Make sure the script is executable:
chmod +x docker-run-n-attach.py
-
Run the script with the same arguments you would use for
docker run
:./docker-run-n-attach.py [docker run arguments]
For example:
./docker-run-n-attach.py --name my-container -v $(pwd):/workspace -it ubuntu:latest
-
The script will:
- Run the Docker container with the provided arguments
- Automatically open Visual Studio Code, connected to the newly created container
- The script passes all provided arguments to
docker run
. - It extracts the container name from the
--name
argument. - It then uses the Visual Studio Code command-line interface to open a new window connected to the container.
- Make sure to include the
--name
argument when running the script, as it's used to identify the container for VS Code attachment. - The script assumes that the container has a
/workspace
directory. Ensure your Docker image or run command sets this up correctly.
If you encounter any issues:
- Ensure Docker is running and you have the necessary permissions.
- Check that the Remote - Containers extension is installed in VS Code.
- Verify that the container name is unique and not already in use.
For more information on using VS Code with containers, refer to the official documentation.