Jupyter Lab as a container
SAKSHAM TRIVEDI
Security (SOC) Analyst || Microsoft Certified Security Operation Canter Analyst
This article is focused on containerizing Jupyter Notebook inside the docker container with a fully-fledged develop environment for data science students.
But... why?
The answer is why not? I mean being a data science student you will be working on multiple projects or maybe the one who needs his custom development environment ready on the fly... without worrying about the development environment that can take your precious time to set it up locally...
...Imagine if you have a program that can do the same thing for you with only just few commands
Disclaimer: This article is primarily focused for the folks who have little to no knowledge on docker but they want to isolate the development by containerizing it.
Let's start...
Prerequisites:
So we will start by creating a working directory. For that we will start by typing...
$ mkdir jupyter-lab && cd jupyter-lab && pwd
By above command we are creating a directory called 'jupyter-lab', then navigating to the directory using 'cd' command and to verify we are printing the current working directory using 'pwd' command.
Now, inside the directory create a file named 'Dockerfile' S using any text editor you like for my sake I am using vim. to do that enter the following command and hit enter.
$ vi Dockerfile
Append the following content to the Dockerfile and save it.
Now, for development requirement let's create a requirements.txt file that later will be used by pip to install all of your library dependencies at once
Again, open a text editor by typing the command
$ vi requirements.txt
You can use a different text editor but keep in mind if you're using the same configuration for the Dockerfile as mentioned above, then place the requirements.txt file in the same directory or you have to define the whole directory path where the file lives and the path where you want the file inside the docker container. In my case I am putting the file in root directory for demonstration purposes.
List all the dependencies as per your requirements. For my case I used basic libraries. Since I am not learning Data Science just for demontration purposes
Note: Don't forget to list notebook inside the reqirements file it will give error while running the jupyter-lab
Now save the files. If you're following along with me then your directory will look like this:
Now, all the configurations are defined let's start by building our container image for that type the command mentioned below:
$ sudo docker build -t jupyter-lab .
And press enter key. Now if you started getting output like below then it indicates that the docker has started building the image.
Now grab a cup of coffee or tea or maybe you can finish your work or if you're working for a long time you can go for a walk and take a breadth as well, just say yourself
I utilized the time by framing this blog in the meantime.
But remember one thing...
Why...? Because the job isn't finished yet.
So after a while you will see the output like below:
领英推荐
It indicates that the docker has built the image successfully...
Now, to run the docker type command:
$ sudo docker run -it -p 8888:8888 jupyter-lab
It will run the container in terminal mode and maps the default port used by jupyter (i.e. 8888) container to the host port (8888 in this case) you can verify if you're inside the container simply by looking at the shell prompt
Now just enter the command to run Jupyter Notebook
$ notebook
And you will see the output as usual when you start the Notebook inside the terminal.
Now copy the token highlighted (with white background)
You'll see the page like below
Now paste the Token copied.
Or it's better to set a new password now by pasting the token to the field after scrolling the page a bit so next time you don't have to copy paste the token again. For that paste the token and set the password as below:
Now, click on 'log in and set new password button.
You will see the Jupyter Notebook default directory page
What are you waiting for... Go and start developing your ideas into reality.
Wait...
Hold on...
You didn't told me what the heck is going on...? My Development Environment don't look like that
Actually, the answer to your question will be in my upcoming blog where I will be explaining the basics of Dockerfile with a little example of continuous development.
Till then overflow your stack
Or... You can check out my other Docker related blogs too
Other writeups
Thanks,
- SK -