How to create Anaconda environment using Docker on M1 Chip MacOS

2022-08-20

Docker for M1 chip released before I knew it… I’ll try to move Miniforge to Anaconda environment. Keeping record here, because I’m not used to use Docker so that it took lot of time to build environment. To make easy to access from MacOS, I’ve done the following.

  • Making shared folder to share text coded on MacOS.
  • Setting to access Jupyter Notebook from local network to show the matplotlib’s graph.

Build Docker environment

First step, install Docker. Please download installer from this page and install as following instruction on pages.

Next, Build an Anaconda Environment with reference to the official website. Enter following the command to get docker image.

docker pull continuumio/anaconda3

Entering following official instruction commands will result in error.

docker run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser"

Details of error.

[I 2021-05-29 03:26:43.961 LabApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[C 03:26:43.963 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.

Add '–allow-root to bypass’. That command will be success, but a shared folder is not created.

docker run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir -p /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser --allow-root"

So, Use '-v’ option for specifying the shared file. If you want to open MOUNTED_DIR as default open foloder, set MOUNTED_DIR to '-notebook-dir=’ and delete line “mkdir -p /opt/notebooks && \".

docker run -v <SHARED_FOLDER_PATH>:<MOUNTED_DIR> \
    -i -t --name <CONTAINER_NAME> \
    -p 8888:8888 \
    continuumio/anaconda3 /bin/bash -c "\
    conda install jupyter -y --quiet && \
    mkdir -p /opt/notebooks && \
    jupyter notebook \
    --notebook-dir=/opt/notebooks --ip='*' --port=8888 \
    --no-browser --allow-root"

The container has been created. Next, access to Jupyter Notebook.

[I 08:12:51.691 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 08:12:51.692 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/nbserver-691-open.html
    Or copy and paste one of these URLs:
        http://f703614b19e3:8888/?token=1096eac49e73fjaioefjaeofajeofajeofajeaojf
     or http://127.0.0.1:8888/?token=1096eac49e73fjaioefjaeofajeofajeofajeaojf

Access Jupyter Notebook

Access container created earlier. In the above example, enter 'http://127.0.0.1:8888/?token=1096eac49e73fjaioefjaeofajeofajeofajeaojf’ on browser. If there is a entry field for token, please input '1096eac49e73fjaioefjaeofajeofajeofajeaojf’.

Successfully connecting Jupyter Notebook!!
Make sure you can use matplotlib. Create new .ipynb project under any directory.

Make sure you can use matplotlib.

How to start after the second time

Enter docker start command.

docker start CONTAINER_NAME

You can access to the same address as the first time.

Container couldn’t start after second time, if you enter following official instruction command. I found the reason on this article. 'mkdir’ command has occurred an error, because that folder already existed on second time. so you need to add '-p’ option on 'mkdir’.