[M1 Mac] How to build a Python environment with VScode + Docker (data analysis / machine learning container)

Last time, I summarized three main methods of building an environment for python on M1 mac.

To enable application development using Django / Flask and execution of programs for machine learning / data scienceHere are the steps to build a Python environment using Docker and Visual Studio Code (VScode) (up to connecting to + Jupyter notebook).

Reason for Docker + VScode

With Docker, you can build and execute a virtual environment with a relatively light weight while easily managing it with a container.Also, if you use separate containers for each project, you can prevent the risk of environmental damage due to dependency conflicts that sometimes occur in Anaconda.

In Visual Studio Code,You can use the ability to access the Python interpreter inside a Docker container by default.. The most popular IDE (Integrated Development Environment) Pycharm in Python needs to be a paid version (about 1 yen a year). VScode boasts the second largest market share and is an IDE as easy to use as Pycharm.

What is the number one development environment for Python, as asked by over 2 developers?

Procedure for building a Python environment using Docker and VScode

  1. Docker installation
  2. Install Visual Studio Code and extensions
  3. Try connecting Vscode and Docker container

1. Install Docker

Official Docker documentationInstall the app according to (the following contents are the same).

First, prepare Rosetta2 before installing docker.Run the following command in the terminal:

 softwareupdate --install-rosetta

Hopefully it will say "software update –install-rosetta", so continueDocker for Apple siliconInstall the app.

docker for apple silicon

When you open Docker, you will be asked for a password, so enter it.

2. Visual Studio CodeAnd installation of extensions

microsoft pageGet VScode from and install it as well.

When you open VScode, it will recognize the language without permission and will install the Japanese localization plug-in, so allow it.Then add the extension "remote-containers".

3. Try connecting Vscode and Docker container

Run the following command from the terminalDocker hubからContainer for jupyter lab (notebook) based on minicondaI will get it.

docker pull jupyter/scipy-notebook

Any container can be created here. If you look for Docker Hub, you'll find a variety of Anaconda and Pytorch containers.

When you open "Image" of the Docker application, a new image is created, so enter "Run" → option to execute the container.

  • Container Name: Favorite container name
  • Local Host: 8888 (port to connect to the container, whatever)
  • Volume: I specified the folder under user / application for the time being (set if you want to share volume with local)
Docker container options

In the VAcode command palette (Command + shift + P), "Remote-Containersattach to Running ContainerWill connect VScode to the container and bring up a new VScode window. If you want to add a python package, operate from this terminal.

Try opening Jupyter notebook

While the container is running, the browser says "http://localhost:8888/", The following page is displayed.

Login screen to Juupyter notebook

The login password (token) is in the "log" that you can see when you click the container, so find it and log in (the OOOOO part of "http: // ~~~~ /? Token = OOOOO")

Log screen of Docker container

The rest is a familiar screen, so I don't think it needs explanation.

After logging in to Jupyter notebook

This is how to build a Python environment with VisualStudioCode + Docker.