I know that many Python developers use Jupyter. I have just started learning Python, but I use Jupyter to check the operation because it is useful.
The sample in this repository is a sample of running Jupyter in a Docker container and accessing and using that environment from VS Code.
There are three files used to configure this environment.
- Dockerfile
- compose.yaml
- devcontainer.json
Place them under the .devcontainer
directory.
.devcontainer/
├── Dockerfile
├── compose.yaml
└── devcontainer.json
If there is a directory named .devcontainer
in the workspaceRoot
, VSCode's RemoteDevelopment extension will use the .devcontainer.json
and other container-related files inside the directory for It does docker build
, container creation, etc.
FROM python:3.12.1-slim-bullseye
USER root
RUN apt-get update && \
apt-get -y install --reinstall ca-certificates && \
apt-get -y install software-properties-common && \
pip install --upgrade pip
RUN pip install ipykernel jupyter
L1
: A container image of the latest Python version as of Jan-11-2024 is specified.L4-L7
: I have installed the minimum required packages and upgradedpip
to provide a base Python environment.L9
: Two modules required for Jupyter are installed.
version: "3"
services:
jupyter:
build:
context: ..
dockerfile: .devcontainer/Dockerfile
environment:
PYTHONPATH: /workspace
volumes:
- ..:/workspace
ports:
- 8888:8888
command: sleep infinity
L6
: Specify the PATH of DockerfileL10
: Define the path of the workspace on the container
{
"name": "devcontainr_python3",
"dockerComposeFile": "compose.yaml",
"service": "jupyter",
"workspaceFolder": "/workspace",
"shutdownAction": "stopCompose",
"forwardPorts": [8888],
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.jupyter"
]
}
}
}
L2
: Container name visible on VS CodeL3
: Specify by relative path the docker-comopse file used to create the containerL4
: Specify the service name of the container you want to open in VScode among the services in compose.yamlL5
: Specify the root folder when the container is openedL6
: Setting what to do with containers when a container screen is closed in VScode.stopCompose
stops the container when the screen is closedL11-L13
: Specify VScode extensions to be installed with container creation
Follow the steps below to execute Dev Containers.
- Open Command Pallette by
F1
- Select
> Dev Containers: Reopen in Container
- Open Command Pallette by
F1
on the Remote side - Select
> Jupyter: Import Jupyter Notebook
- Select
Interactive-1.ipynb
- Select
Run Cell
- feature:1
- feature:2
Released under the MIT license
- github: https://github.com/shinyay
- twitter: https://twitter.com/yanashin18618
- mastodon: https://mastodon.social/@yanashin