OS: macOS Big Sur 11.1 Docker: 20.10.0 docker-compose: 1.27.4
Execute the command with administrator privileges. In addition, we will proceed assuming that Docker and docker-compose are already installed.
Docker image is built on Dockerfile. Once you build
the Docker image, a cache will be created. Simply put, the cache speeds up the second and subsequent reads. If you have this cache, it will be used preferentially during build
. Therefore, when you update the Dockerfile, use docker-compose build --no-cache
to do a build
that does not use the cache.
Please note that it may take a long time depending on the thing because it is a build
that does not use a cache.
Docker is like a virtual machine in a nutshell. Strictly different, you can use the server kernel to isolate processes and users on a server-by-server basis and make them run as if they were running another machine. Therefore, it is lighter and faster than virtualization. You can easily build an environment by using Docker, and you can easily share your environment without considering the difference in OS.
As described in Caution, it is assumed that Docker and docker-compose are already installed. Then type the following command on the terminal.
$ docker pull jupyter/datascience-notebook:latest
This command pulls the Docker image locally from Docker Hub. If you have decided on the Docker image to use at this time, you can select the one you like. To select a Docker image, please check Jupyter's Official Site and select it. If you get lost, you can use the above datascience-notebook
.
At this stage, you can set a password for your Jupyter. If you work only in the local environment, you do not need to set it in particular, but if necessary, the article here was easy to understand, so please refer to it.
After that, save the following Dockerfile in your own directory.
Dockerfile
FROM jupyter/datascience-notebook
RUN pip install --upgrade pip
RUN pip install jupyterlab
RUN jupyter serverextension enable --py jupyterlab
###Jupyterlab extension
# Variable Inspector
RUN jupyter labextension install @lckr/jupyterlab_variableinspector
# Table of Contents
RUN jupyter labextension install @jupyterlab/toc
### settings
##prepare settings * For this directory, refer to Advanced Setting Editor of Settings of jupyterlab.
RUN mkdir -p /home/jovyan/.jupyter/lab/user-settings/@jupyterlab/apputils-extension
RUN mkdir -p /home/jovyan/.jupyter/lab/user-settings/@jupyterlab/notebook-extension
## user-settings * error if the directory is not created by prepare settings
#Black background
RUN echo '{"theme":"JupyterLab Dark"}' > \
/home/jovyan/.jupyter/lab/user-settings/@jupyterlab/apputils-extension/themes.jupyterlab-settings
#Line number display
RUN echo '{"codeCellConfig": {"lineNumbers": true}}' > \
/home/jovyan/.jupyter/lab/user-settings/@jupyterlab/notebook-extension/tracker.jupyterlab-settings
I will not explain the contents in detail, but this Dockerfile is used to set up the data science environment Jupyterlab. If you read the comment text, you will know what the purpose of the command was, so if you are interested, please take a closer look.
In particular, by describing the following setting
as a command, the default Jupyterlab will be displayed with a black background and line numbers, so it is essential to describe it personally.
Next is docker-compose.
docker-compose makes it easier to build Docker images and start / stop each container for applications that consist of multiple containers.
docker-compose.yml
version: "3"
services:
jupyterlab:
build:
context: .
dockerfile: "Dockerfile"
user: root
container_name: con_jupyterlab
image: jupyterlab
ports:
- "8888:8888"
volumes:
- "/Users/[user]/Documents/DataScience/jupyter:/home/jovyan/work"
environment:
GRANT_SUDO: "yes"
TZ: Asia/Tokyo
command: start.sh jupyter lab --NotebookApp.token=""
I will not explain this in detail either. Enter the Docker image name and container name created by yourself as appropriate. For volumes
, specify the path where your Dockerfile exists for the path before the colon (:). If this part is incorrect, every time you stop the container, your work in the data science environment will be erased. Be sure to make sure that your data is retained before you work.
Use the terminal cd
command to change to the directory where the Dockerfile (docker-compose.yml) resides. Then, execute the following command.
$ docker-compose up -d
After running, wait a moment and then go to http: // localhost: 8888. If you see a screen like this, you are successful.
If you get "Could not open page" or "Cannot connect to server", please try reloading the page again after a while. If that doesn't work, there is a mistake in the Dockerfile or docker-compose, so check the error message on the terminal.
If there are no errors, you're done. Thank you for your hard work.
Python in the initial state does not support Japanese. If you try to display Japanese as it is, it will be so-called tofu characters (garbled characters). Introduce fonts so that Japanese can be displayed. For details, please refer to the article here.
With Docker, you can build a development environment without polluting your environment, and you can share your environment with others. This eliminates any discrepancies between developers due to the environment. There are other features such as light weight and high speed, so please study.
Recommended Posts