⭐️ Local, automated and isolated environment for learning Python 3.
This project automates the creation of a Python 3.10 + Jupyter
local environment, meant to be used for learning and testing purposes.
It creates a VM using Vagrant, that has Docker and Docker Compose installed.
Python3 + Jupyter are installed and executed inside a Docker container, inside the VM.
With this, you don't need to install Docker or Python on your local computer.
You just need to install Vagrant and VirtualBox.All the images and containers are created in an isolated environment that can be deleted at any time.
Vagrant creates an Ubuntu VM that installs Docker, pulls Docker images from DockerHub, and runs containers with their corresponding port mappings.
Jupyter website will be accessible to the host's web browser through port 8888
.
The automation process is specified using the following files:
Vagrantfile
: Tells Vagrant how to create and configure the VMdocker-compose.yml
: Tells Docker Compose which and how containers should be executed
The following diagram shows the architecture:
Note
Execute these steps only if it's the first time that you use Vagrant with VirtualBox.
If not, you can skip them. They only serve to test the Vagrant + VirtualBox installation.
If Vagrant and VirtualBox are installed and configured correctly,
then the environment will work fine (it has already been tested, and is repeatable).
Check that the vagrant
executable was added correctly to the PATH
variable:
vagrant version
Check that vagrant is able to create a VM:
mkdir test-vagrant
cd test-vagrant
vagrant init ubuntu/jammy64
vagrant up
vagrant ssh
pwd
exit
vagrant destroy --force
cd ..
rm -rf test-vagrant
Warning
If the following error appears after executing vagrant up
:
No usable default provider could be found for your system.
- Verify that VirtualBox was installed correctly
- Obtain more info about the error:
vagrant up --provider=virtualbox
Warning
If the following error appears after executing vagrant up
:
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005)
- Reinstall VirtualBox
Warning
If Vagrant gets stuck on the following line after executing vagrant up
:
SSH auth method: private key
- Windows users: Open cmd as admin and execute:
This disables Hyper-V.
bcdedit /set hypervisorlaunchtype off
All the vagrant
commands must be executed in the host machine from the folder
that contains the Vagrantfile (in this case, the project root folder).
Note
For Windows users:
If Vagrant doesn't show any output in the stdout for a Vagrant command after some time,
press the Enter key or right click in the console window.
See this post for more info about this problem.
This will:
- Install Docker inside the VM
- Pull the Docker images from DockerHub
- Run the containers All with the corresponding port mappings.
Note
Docker images/containers will only be downloaded/executed if the
Docker Compose up
line in the Vagrantfile
is uncommented.
vagrant up
vagrant status
This connection is done via SSH.
vagrant ssh
Tip
Some interesting commands to execute inside the VM:
Commmand | Description |
---|---|
free -h |
Display amount of free and used memory in the VM |
docker stats |
Display a live stream of container(s) resource usage statistics. Useful to monitor Docker containers memory usage. |
docker container ls --all |
List all Docker containers (running or not). If both containers specify "Up" in the status column, everything is running fine. |
docker logs <containerid> |
Fetch the logs of a container. Really useful to see what's going on. |
docker top <containerid> |
Display the running processes of a container |
docker exec -it <containerid> <command> |
Run a command in a running container (in interactive mode) |
docker images |
List images |
docker version |
Show the Docker version information |
docker info |
Display system-wide information |
netstat -tulpn | grep LISTEN |
Display network connections (listening TCP or UDP). Useful to check that Jupyter port (8888) is listening. |
The VM welcome message shows the command for connecting to the tmux session.
The tmux window is divided in panes with the following layout:
┌─────────────────┬──────────────────────────┐
│ LINUX CLI │ │
├─────────────────┤ IPYTHON 3.10 INTERPRETER │
│ JUPYTER-LAB URL │ │
└─────────────────┴──────────────────────────┘
The URL for accesing JupyterLab will be shown in the JUPYTER-LAB URL
pane of the tmux session.
Simply copy that URL and paste it in your web browser.
Note
It can also be obtained executing the following command:
docker logs vagrant-python-1 2>&1 | grep -o '[^ ]*127.0.0.1[^ ]*'
Ctrl-B + d
tmux list-sessions
tmux attach-session -t <session-name>
If the tmux session is deleted (for example, using Ctrl-D several times), you may need to restart the tmux server in order to be able to connect again to the tmux session:
tmux kill-server
tmux attach-session -t <session-name>
Note
Only if containers where executed using Docker Compose.
This is useful if you want to clean the data inside the containers.
cd /vagrant
docker compose rm --stop --force
docker compose up -d
Obtain the name of the container you want to connect to:
docker container ls --all
The name is the last column.
Execute the bash command in that container to connect to it:
docker exec -it <container-name> bash
Stopping the VM will stop the Docker containers and turn off the VM.
All the data is persisted inside the containers, and a subsequent turn on of the VM
(and the containers) will have access to that data.
Stop the VM:
vagrant halt
Check the status of the VM:
vagrant status
Start the VM and the containers again:
vagrant up
Destroying the VM will remove all the VM data, and therefore, the containers inside it.
This should be the option used if you do not want to keep the data, and you want to have a "clean" environment in the next turn on of the VM (because the VM and the containers will be created from scratch).
vagrant destroy
Whenever you change the docker-compose.yml
file, you need to run vagrant reload
to redefine the Vagrant box.
Enhanced interactive Python shell.
- Documentation: https://ipython.readthedocs.io/en/stable/
- Demo: https://www.pythonanywhere.com/try-ipython/
- Installation:
pip install ipython
- Execution:
ipython
The latest web-based interactive development environment.
- Documentation: https://jupyterlab.readthedocs.io/en/stable/
- Demo: https://jupyter.org/try-jupyter/lab/
- Installation:
pip install jupyterlab
- Execution:
jupyter lab
The original web application for creating and sharing computational documents.
- Documentation: https://jupyter-notebook.readthedocs.io/en/stable/
- Demo: https://jupyter.org/try-jupyter/retro/notebooks/?path=notebooks/Intro.ipynb
- Installation:
pip install notebook
- Execution:
jupyter notebook
Runs the code in the Jupyter notebooks and transforms them to standalone web applications and dashboards.
- Documentation: https://voila.readthedocs.io/en/stable/index.html
- Demo: https://mybinder.org/v2/gh/voila-dashboards/voila/stable?urlpath=voila%2Ftree%2Fnotebooks
- Installation:
pip install voila
- Execution:
- To use Voilà within a pre-existing Jupyter server, first start the server, then go to the following URL:
<url-of-my-server>/voila
- For example, if you typed jupyter lab and it was running at http://localhost:8888/lab, then Voilà would be accessed at http://localhost:8888/voila.
- To use Voilà within a pre-existing Jupyter server, first start the server, then go to the following URL: