Jupytor in Docker
Category : DevOps Tag : 101series shellscript jupytor
Feb. 2, 2021, 7:38 a.m.

Short Description :
How to deploy jupytor in docker container
source : datak

<p>Putting jupyter in a docker gives us couple of benefits;</p><ul><li>avoid environmental issue and compatibility issue. same concept with virtual environment</li><li>is portable sharing same docker file with multiple OS, like main OS and OS in virtual machine</li><li>push to remote instance at cloud like Google Cloud, AZURE, AWS, and use powerful GPU with same environmental settings</li></ul><p>data file can be still connected to local through docker command (will be specified later in the article)</p><p><br></p><h3>Step1 : Setting up Docker file</h3><p>Create directory and dockerfile there</p><pre>#shellscript mkdir &lt;your docker directory&gt; subl &lt;dockerfile&gt;</pre><p>Full dockerfile script is below;</p><pre>#dockerfile FROM ubuntu:latest RUN apt-get update &amp;&amp; apt-get install -y \ sudo \ wget \ vim WORKDIR /opt RUN wget https://repo.continuum.io/archive/Anaconda3-2019.10-Linux-x86_64.sh &amp;&amp; \ sh /opt/Anaconda3-2019.10-Linux-x86_64.sh -b -p /opt/anaconda3 &amp;&amp; \ rm -f Anaconda3-2019.10-Linux-x86_64.sh ENV PATH /opt/anaconda3/bin:$PATH RUN pip install --upgrade pip RUN pip install numpy WORKDIR / CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]<br></pre><p><br></p><h4>Dockerfile;</h4><p>Select latest ubuntu (or v18.X verified), and update existing libraries and install 3 application. sudo is to enable command by root privilege, wget is to enable downloading files from the web, and vim is text editor application in the terminal.</p><pre style="line-height: 1.42857;">FROM ubuntu:latest RUN apt-get update &amp;&amp; apt-get install -y \ sudo \ wget \ vim</pre><p><br></p><p>Then move the directory under 'opt', download anaconda from web and execute installation. Once installation is completed remove files&nbsp;</p><pre>WORKDIR /opt RUN wget https://repo.continuum.io/archive/Anaconda3-2019.10-Linux-x86_64.sh &amp;&amp; \ sh /opt/Anaconda3-2019.10-Linux-x86_64.sh -b -p /opt/anaconda3 &amp;&amp; \ rm -f Anaconda3-2019.10-Linux-x86_64.sh</pre><p><br></p><p>Set up environmental path to enable CLI for anaconda, then upgrade 'pip' and install 'numpy'. If we know other data science libraries to be used by default you can put other libraries here.</p><pre>ENV PATH /opt/anaconda3/bin:$PATH RUN pip install --upgrade pip RUN pip install numpy</pre><p><br></p><p>Finally change the directory back to root and set up CMD script to run jupyter in localhost which are codes which will be executed once we run 'docker run ...' at docker CLI later.</p><pre>WORKDIR / CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]</pre><p><br></p><h3>Step2 : Docker Command<br></h3><p>At local docker repository, create docker image by;</p><pre>#shellscript docker build</pre><p><br></p><p>Once docker image is created at local PC, run the docker container pulling script from the image</p><pre>#shellscript docker run -v /xx:/work -p 8888:8888 --name &lt;name&gt; &lt;image id or image name&gt;<br></pre><p>-v is to enable to mount file system between docker container and local PC. Put your local directory at '/xx', while '/work' is temporal directory in docker container.</p><p>-p is to enable jupyter at local browser. We are specifying port 8888 for jupyter, but of course we could change whatever we prefer if 8888 is taken by other application.</p><p><br></p><h3>Step3 : Open Jupyter</h3><p>At your local PC, open browser and type and run</p><pre>localhost:8888</pre><p><img src="/media/django-summernote/2021-02-02/d242892c-b886-49aa-9b9b-9d638d02739f.png" style="width: 100%;"><br></p><p>Looking at file directories at left pane, bottom one is what we've created through CLI scripts. Open 'work' directly and save jupyter scripts then the file will be mounted on your local directory you've specified in CLI scripts.</p><p><br></p><h3>Next Steps:</h3><p>Here's improvement list when I have time, or I'd appreciate if anyone could help teach me through twitter by #datak. Thanks!</p><ul><li>Change jupyter theme to black by default</li><li>Enable jupyter container at other PC/VMs in internal network (How I can specify IP address instead of localhost?)</li></ul>


<< Back to Blog Posts
Back to Home

Related Posts

Docker 101 : Basic Commands
Nov 28 2020
Basic commands for docker operation, such as build, run, and going into container etc.
Python Exploratory Data Analysis 1
Feb 22 2021
Exploratory Data Analysis using Jupyter Python
CICD Pipeline Part1
Jun 1 2021
CICD explanation. Part 1 focuses on how to locally create a docker container using python streamlit
Jupyter 101 : Basic Operation
Feb 22 2021
Basic short cut list to operate jupyter notebook



© DATAK 2024