Docker remote containers on WSL2

In this blog post we’ll see how to install and configure Docker to set up a Python programming environment based on Ubuntu 22.04 LTS.

Building an image via Dockerfile

What is Docker?

Docker is a framework for managing applications in environments called containers.

A container is a standard unit that packages up all the sys tools, libs, deps,… an app need. Meaning they are modular, portable, isolated, scalable and fast in terms of development/deployment.

Containers are built when running a docker image on the docker engine. They cannot be considered as a virtualized OS, instead they share the same Linux kernel as the system that it’s running on.

For our case we’ll make use of Windows Subsystem for Linux which enables Windows to run a Linux file system.

Prerequisites

Docker image creation

Containers are created when running a docker image on the docker engine.

The docker engine is executed when running the docker desktop app.

To build the docker image we’ll need a Dockerfile.

A Dockerfile is a text file containing a set of instructions that docker can read to build the image automatically. It must begin with FROM instruction, which specifies the image/tag from which we are building, i.e. Ubuntu 22.04 LTS (ubuntu:latest). Docker hub is the world’s largest library and community for container images.

Docker hub

Before starting the build process, open the Dockerfile to modify some of the variables like username, work directory, etc. The provided Dockerfile also executes a script (python-install.sh) to install Python 3.9 and all the dependencies needed. You can skip it by commenting out the following lines:

# Install Python/dependencies
# COPY python-install.sh $WORKDIR
# RUN $WORKDIR/python-install.sh

# Remove the sh file file
# RUN rm -r $WORKDIR/python-install.sh

Once You have downloaded and edited the files provided, open a terminal inside the folder and run the following command to start the build process:

$ docker build -t <image-name> .

Where <image-name> is the name we want to associate to the image. For example:

$ docker build -t pinux .

Volume creation

To persist data generated by and used by the docker container, i.e. to share data between Windows and the container, we will create a volume.

Folder shared between host and guest

Creating a docker volume is as easy as breathing:

$ docker volume create <volume-name>

where <volume-name> is the name of the volume. For example:

$ docker volume create docker-dev

(OPTIONAL) We can create a quick access shortcut to the container’s data by running the 02-create-qa-shortcut.ps1 script (don’t forget to modify the script):

$ Powershell.exe -executionpolicy remotesigned -File 02-create-qa-shortcut.ps1

Container creation

To create the container run the following command:

$ docker create -it --name <name> --hostname <hostname> -v docker-dev:<path-to-work-dir> <image-name>

Where:

So the command becomes:

$ docker create -it --name pinux --hostname pinux -v docker-dev:/home/ivandorte/python-dev pinux

Once done the container will be accessible by running a simple command:

$ docker start -i <name>
Starting the container...

Automated installation! :sunglasses:

You can lazily execute all the steps above by running the 01-docker-install.bat! :D

VS Code – How to integrate Docker

Install the following extensions

The Docker extension makes it easy to build, manage, and deploy containerized applications in Visual Studio Code.

Docker extension

The Dev Containers extension lets you use a container as a full-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code’s full feature set.

The Python extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. It leverages all of VS Code’s power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.

The Python and Jupyter extensions work together to give you a great Notebook experience in VS Code, providing you the ability to directly view and modify code cells with IntelliSense support, as well as run and debug them.

How to connect to the container from VS Code

  1. Make sure that both the docker engine and the container are running.
  2. From the left pane, navigate to “Remote Explorer” extension.
  3. From the DEV CONTAINERS activity tab, hover your mouse cursor to the container to make further navigation options visible.
  4. Attach the container in a current or new vs code window.
  5. Once loaded, from the left pane, navigate to the Explorer and select “Open Folder”.
    Select your work directory from the drop-down menu.
Connecting to the container through ssh

Running Jupyter notebooks

Jupyter notebooks need to connect to an existing Python kernel to run the code.

All we need to do to make this happen is to install the Python/Jupyter extensions from the host to the container:

  1. From the left pane, navigate to the “Extensions” tab.
  2. From the CONTAINER activity tab, click on the download button then select Python and Jupyter extensions from the drop-down menu and click OK.

Now Jupyter will be finally able to find a Python kernel to execute the code in the document!

Installation of the extension to the container

Closing a remote connection

!Before closing the connection to the remote container save your current work!

Once done click on the green bottom left button to open a drop-down menu. From there select “Close Remote Connection”.

Closing a remote connection

Credits

enricocid

https://www.paypal.me/ivandorte
https://www.paypal.me/enricocid