How to easily create an environment where python code runs on Jupyter without polluting the local environment

Overview

--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).

Install Docker

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

Start container with Anaconda docker image

Launch Terminal

Download the required docker image

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

Create a working directory referenced by jupyter notebook

$ mkdir -p $HOME/jupyter-notebook/notebooks

Start Docker container

$ 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

Open JupyterNotebook from your browser

Get the URL of the Jupyter Notebook running on the container

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 access URL in your browser

Open the obtained URL in a browser (if you have changed the port, replace it with the specified port)

http://localhost:8888/?token=a5b5211197b4d9b2f8f93ee73c085786039486369c5bcbec
notebook

Opened No code yet

Code execution

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

JupyterNote からみえます

(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

If you don't need a Docker container anymore

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

How to easily create an environment where python code runs on Jupyter without polluting the local environment
How to run Python on Windows without polluting the environment as much as possible (Scoop edition)
How to easily switch the virtual environment created by Conda on Jupyter
Create a local scope in Python without polluting the namespace
How to write code to access python dashDB on Bluemix or local
How to use jupyter notebook without polluting your environment with Docker
How to build Python and Jupyter execution environment with VS Code
How to touch Jupyter Notebook without polluting the environment other than Pythonista, or how to touch Ruby with Jupyter Notebook without polluting the environment other than Rubyist
How to create an NVIDIA Docker environment
Create an OpenCV3 + python3 environment on OSX
Steps to create a Python virtual environment with VS Code on Windows
How to create a Python virtual environment (venv)
How to build an environment for using multiple versions of Python on Mac
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to create an article from the command line
Think about how to program Python on the iPad
[Blender x Python] How to create an original object
How to build a Django (python) environment on docker
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
How to create an image uploader in Bottle (Python)
How to use jupyter lab in Windows 10 local environment
How to enjoy Python on Android !! Programming on the go !!
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to build a Python environment on amazon linux 2
How to create a kubernetes pod from python code
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
How to manage Python minor version (build virtual environment) on Windows (without Pyenv or WSL)
A memo on how to easily prepare a Linux exercise environment
How to build a new python virtual environment on Ubuntu
[Hyperledger Iroha] Notes on how to use the Python SDK
How to use VS Code in venv environment on windows
How to deploy the easiest python textbook pybot on Heroku
Code to send an email based on the Excel email list
Building an environment to execute python programs on AWS EC2
How to get into the python development environment with Vagrant
[Python Kivy] How to create an exe file with pyinstaller
How to create an ISO file (CD image) on Linux
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]
Overview of Python virtual environment and how to create it
I tried to build an environment where work in the Docker container on the remote server can be done directly from the local VS Code with SSH connection
Run the output code on the local web server as "A, pretending to be B" in python
How to quickly create a machine learning environment using Jupyter Notebook on macOS Sierra with anaconda
Anyone can understand how to build an initial environment for Python on Mac September 2016 (pyenv + virutalenv)
Make Pandas available on Jupyter in the shortest possible time on your new Mac without polluting the environment
Create a Python environment for professionals in VS Code on Windows
I tried to create a server environment that runs on Windows 10
I tried to create an environment of MkDocs on Amazon Linux
How to rebuild python environment from pyenv on Mac environment (El Capitan)
How to know the internal structure of an object in Python
Try to create a python environment with Visual Studio Code & WSL
How to create a heatmap with an arbitrary domain in Python
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
[Python] Explains how to use the format function with an example
How to use Jupyter on the front end of supercomputer ITO
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
The road to installing Python and Flask on an offline PC
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
How to update the python version of Cloud Shell on GCP
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory