<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: eleloi</title>
    <description>The latest articles on DEV Community by eleloi (@eleloi).</description>
    <link>https://dev.to/eleloi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F551762%2F5f128329-6ae2-4c00-aaf3-269aff2350fc.jpg</url>
      <title>DEV Community: eleloi</title>
      <link>https://dev.to/eleloi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eleloi"/>
    <language>en</language>
    <item>
      <title>Serving Jupyter Notebook with Docker</title>
      <dc:creator>eleloi</dc:creator>
      <pubDate>Wed, 06 Sep 2023 23:04:26 +0000</pubDate>
      <link>https://dev.to/eleloi/serving-jupyter-notebook-with-docker-o67</link>
      <guid>https://dev.to/eleloi/serving-jupyter-notebook-with-docker-o67</guid>
      <description>&lt;p&gt;👋 Hi!&lt;/p&gt;

&lt;p&gt;In this article, I'll walk you through the steps I took to revamp my aging Python applications and migrate them to a Docker container on a production server.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Legacy Setup
&lt;/h2&gt;

&lt;p&gt;Several years ago, I had a collection of Python applications that I shared with my coworkers using an old Windows server. This server hosted Jupyter Notebook with remote access enabled.&lt;/p&gt;

&lt;p&gt;Today, to my surprise, I realised that some of these scripts were still being actively used by a group of users.&lt;/p&gt;

&lt;p&gt;I decided to &lt;strong&gt;migrate the jupyter notebook server to a Docker container&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Dockerfile
&lt;/h2&gt;

&lt;p&gt;We need a &lt;code&gt;Dockerfile&lt;/code&gt;, which is essentially a recipe for building a Docker image. This file included all the necessary instructions to set up a Python environment, install the required libraries, and configure Jupyter Notebook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use an official Python 3.11 image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; python:bookworm&lt;/span&gt;

&lt;span class="c"&gt;# Need voila to serve the scripts as applications&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;voila
&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;notebook

&lt;span class="c"&gt;# Create the isolated dir on docker&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; /jupyterbooks
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /jupyterbooks&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 8888&lt;/span&gt;

&lt;span class="c"&gt;# Run jupyter with remote access enabled&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; sh -c "jupyter notebook --no-browser --allow-root --IdentityProvider.token='yourSecretToken' --ServerApp.allow_origin='*' --ServerApp.port=8888 --ServerApp.allow_remote_access=True --ServerApp.ip='0.0.0.0'"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also created a &lt;code&gt;docker-compose.yml&lt;/code&gt; file to configure the container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.3"&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;jupyter-service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jupyter-service&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Dockerfile&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jupyter-service&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;8888:8888&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;jupyter-data:/jupyterbooks&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;jupyter-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jupyter-data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copied the old scripts at the new docker jupyter-data volume with &lt;code&gt;docker cp myNotebooks jupyter-service:/jupyterbooks/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And finally &lt;code&gt;docker compose up -d&lt;/code&gt; does the trick.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I'ts amazing how two short files can configure such a service.&lt;/p&gt;

&lt;p&gt;Now I can access the notebook with my token and the users can consume the applications without password through voila.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>jupyter</category>
      <category>devops</category>
      <category>selfhosted</category>
    </item>
  </channel>
</rss>
