Since I was able to build the following environment with Docker, I summarized the flow from Docker image build to JupyterLab startup
--Easy to switch Python versions (Python3 ⇄ Python2) --Easy to start JupyterLab [^ 1] --You can delete the environment as soon as you no longer need it. --I want to easily build an environment on Mac and Windows
Account login is required to obtain it. Please also install Docker Desktop. In addition, this time I used python of Docker Official Images [^ 2]
Store Docker files in any directory
Docker image | Settings |
---|---|
Python3 | python:3 |
Python2 | python:2 |
Dockerfile
FROM python:3
WORKDIR /usr/src/app
RUN pip install --upgrade pip
RUN pip install numpy scipy matplotlib ipython scikit-learn pandas pillow mglearn
RUN pip install jupyterlab
RUN jupyter serverextension enable --py jupyterlab
docker-compose.yml
Store docker-compose file in any directory
Setting items | Settings |
---|---|
Host side shared folder | /Users/User_Name/Docker/JupyterLab/codes |
Shared folder on the container side | /home/JupyterLab/codes |
JupyterLab login password[^3] | --NotebookApp.notebook_dir='/home/JupyterLab/codes' |
JupyterLab home directory | --NotebookApp.notebook_dir='/home/JupyterLab/codes' |
Docker container name | jupyterlab |
docker-compose.yml
version: '3'
services:
jupyterlab:
container_name: 'jupyterlab'
image: jupyterlab
build:
context: .
dockerfile: "Dockerfile"
user: root
volumes:
- jupyterlab-dir:/home/JupyterLab/codes
ports:
- "8888:8888"
environment:
TZ: Asia/Tokyo
command: jupyter lab --ip=0.0.0.0 --allow-root --no-browser --NotebookApp.notebook_dir='/home/JupyterLab/codes' --NotebookApp.token='○○○'
volumes:
jupyterlab-dir:
driver_opts:
type: none
device: /Users/MacLover/Docker/JupyterLab/codes
o: bind
Build the image with the following command
Terminal or PowerShell
docker-compose build
Check the image
Terminal or PowerShell
docker images
It's okay if the image jupyterlab is displayed
The next time you use Jupyter Lab, start it with the following command
Terminal or PowerShell
docker-compose up
When stopping, ctrl + c on the terminal
Access the following URL and enter the password to display the Jupyter Lab screen. The code created on JupyterLab (ipynb file, etc.) is output to the shared folder on the host side, so even if you delete the container, the created code will not be deleted.
[^ 1]: Instead of adding a long option with docker run, I described it in the configuration file (Dockerfile and docker-compose.yml) and devised to start it only with ** docker-compose up **
[^ 2]: Initially, I was thinking of "jupyter / datascience-notebook", but when I tried it on Windows, a kernel error occurred when starting Jupyter Lab, so I simply used a Python-only image.
[^ 3]: When writing a hashed password instead of plain text, change "--NotebookApp.token ='○○○'" to "--NotebookApp.password ='sha1: ○○○'". Please change
Recommended Posts