--Create a state where ** python code can be run ** without polluting the local environment -** Time required 5 minutes **
--The article assumes that it will be built on a macbook, but if you want to start it on a server and access it with a browser, replace localhost
with any server and execute it (and set a port that can be accessed by a browser).
https://docs.docker.com/docker-for-mac/install/#download-docker-for-mac
--Click Get Docker for mac
to get Docker.dmg
--Start Docker.dmg
and install it on your mac
Get your favorite docker image, here we will use the image that contains anaconda3 https://hub.docker.com/r/continuumio/anaconda3/~/dockerfile/
$ docker pull continuumio/anaconda3
Using default tag: latest
latest: Pulling from continuumio/anaconda3
8ad8b3f87b37: Pull complete
e8b8d30f7444: Pull complete
...
Status: Downloaded newer image for continuumio/anaconda3:latest
$ mkdir -p $HOME/jupyter-notebook/notebooks
$ docker run \
-d -i -t \
-p 8888:8888 \
--name jupyter-notebook \
-v $HOME/jupyter-notebook/notebooks:/opt/notebooks \
continuumio/anaconda3 \
/bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --no-browser --allow-root"
-p 8888: xxxx
If the Jupyter Notebook default 8888 is not convenient, replace it with another port xxxx
.
Checking the status of the container
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96f9690341ae continuumio/anaconda3 "/usr/bin/tini -- ..." 6 seconds ago Up 5 seconds 0.0.0.0:8888->8888/tcp jupyter-notebook
In the log obtained by the docker logs
command, the URL with the token for accessing JupyterNotebook is recorded, so get it.
$ docker logs jupyter-notebook
Fetching package metadata .........
Solving package specifications: .
...
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=a5b5211197b4d9b2f8f93ee73c085786039486369c5bcbec <<<<---- here
Open the obtained URL in a browser (if you have changed the port, replace it with the specified port)
http://localhost:8888/?token=a5b5211197b4d9b2f8f93ee73c085786039486369c5bcbec
Opened No code yet
Place your favorite code in your working directory
As an example, git clone
Deep Learning repository from scratch.
$ cd $HOME/jupyter-notebook/notebooks
$ git clone https://github.com/oreilly-japan/deep-learning-from-scratch.git
Cloning into 'deep-learning-from-scratch'...
remote: Counting objects: 322, done.
remote: Total 322 (delta 0), reused 0 (delta 0), pack-reused 322
Receiving objects: 100% (322/322), 4.87 MiB | 944.00 KiB/s, done.
Resolving deltas: 100% (160/160), done.
Checking connectivity... done.
Immediately reflected on Jupyter Notebook
(The acquired file is a text file, so it cannot be executed as it is)
Create an executable file such as Python3
from the New
pull-down on the Jupyter screen, and cut and paste the code you want to execute to move the code freely.
that's all! : D
Let's destroy the container with docker stop
, docker rm
:)
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96f9690341ae continuumio/anaconda3 "/usr/bin/tini -- ..." 6 seconds ago Up 5 seconds 0.0.0.0:8888->8888/tcp jupyter-notebook
$ docker stop 96f9690341ae
$ docker rm 96f9690341ae
Recommended Posts