Mumbaikar007
Mumbaikar007

Reputation: 441

running conda's jupyter on docker

I am using the docker image continuumio/anaconda3 and want to start jupyter notebook server with conda via browser ...

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"

which results in ...

Package plan for installation in environment /opt/conda:

The following packages will be UPDATED:

    anaconda: 5.0.1-py36hd30a520_1  --> custom-py36hbbc8b67_0
    conda:    4.3.30-py36h5d9f9f4_0 --> 4.4.10-py36_0        
    jupyter:  1.0.0-py36h9896ce5_0  --> 1.0.0-py36_4         
    pycosat:  0.6.2-py36h1a0ea17_1  --> 0.6.3-py36h0a5515d_0 

[I 14:59:00.461 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 14:59:00.475 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 14:59:00.498 NotebookApp] JupyterLab alpha preview extension loaded from /opt/conda/lib/python3.6/site-packages/jupyterlab
JupyterLab v0.27.0
Known labextensions:
[I 14:59:00.499 NotebookApp] Running the core application with no additional extensions or settings
[C 14:59:00.502 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.

and if I use

$ docker run -p 8888:8888 -i -t continuumio/anaconda3 /bin/bash
root@083f8fbb5d3b:/# jupyter notebook

it gives ...

[I 15:00:52.496 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
Traceback (most recent call last):
  File "/opt/conda/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.6/site-packages/jupyter_core/application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "<decorator-gen-7>", line 2, in initialize
  File "/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 1296, in initialize
    self.init_webapp()
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 1120, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/opt/conda/lib/python3.6/site-packages/tornado/tcpserver.py", line 142, in listen
    sockets = bind_sockets(port, address=address)
  File "/opt/conda/lib/python3.6/site-packages/tornado/netutil.py", line 197, in bind_sockets
    sock.bind(sockaddr)
OSError: [Errno 99] Cannot assign requested address

How should I run Jupyter notebook?

Upvotes: 6

Views: 7898

Answers (6)

etbaba
etbaba

Reputation: 1

addition on Aramakus https://stackoverflow.com/a/63225607/16759476 Aramakus's setup works perfectly but i needed to add some package's like cuda ,pytorch

so I updated his files like

  • Dockerfile:
FROM continuumio/miniconda3
 
RUN conda update -n base -c defaults conda
 
COPY environment.yml .
RUN conda env create -f environment.yml
 
RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
 
SHELL ["/bin/bash", "-c"]

WORKDIR /home

ENTRYPOINT ["conda", "run", "-n", "myenv", "jupyter", "notebook", "--ip=0.0.0.0", "--port=8080", "--allow-root", "--NotebookApp.token=''", "--NotebookApp.password=''"]
  • environment.yml (updated python version)
name: myenv
channels:
  - conda-forge
  - defaults
dependencies:
  - jupyter
  - python=3.9.7=h12debd9_1
  - pip:
      - datetime

Upvotes: 0

Aramakus
Aramakus

Reputation: 1920

I might be a bit late for the party, but after couple days of trial and error I have put together docker and docker-compose files that I am using. I am posting my docker-compose, docker and environment.yaml files. Hope it helps those poor souls like myself some time ago:

docker file:

FROM continuumio/miniconda3

# Update conda.
RUN conda update -n base -c defaults conda

# Create the environment:
COPY environment.yml .
RUN conda env create -f environment.yml

WORKDIR /home

ENTRYPOINT ["conda", "run", "-n", "myenv", "jupyter", "notebook", "--ip=0.0.0.0", "--port=8080", "--allow-root", "--NotebookApp.token=''", "--NotebookApp.password=''"]

docker-compose file:

version: '3'

services:
  miniconda:
    container_name: 'jupyter_docker'
    build: '.'
    ports:
      - 8080:8080
    volumes:
      - $PWD/files:/home

And environment.yaml file:

name: myenv
channels:
  - conda-forge
  - defaults
dependencies:
  - jupyter
  - python=3.8.2=hcf32534_0
  - pip:
    - datetime

Running with docker-compose is super simple, a single command docker-compose up creates image if it does not exist already and sets up a container. From there, just go to your browser at localhost:8080. Run docker-compose down --rmi all --remove-orphans to remove container and image after you are done.

Upvotes: 1

Federico Traiman
Federico Traiman

Reputation: 1251

I'm using anaconda on docker running this

docker run -it --rm --name ds -p 8888:8888 jupyter/datascience-notebook

Use it and tell me

Upvotes: 0

Mumbaikar007
Mumbaikar007

Reputation: 441

Thanks Mr. darthbirth,

$ docker run -p 8888:8888 -i -t continuumio/anaconda3 /bin/bash

jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root

and the you have Jupyter notebook up and running.

Press ctrl and click the "login with a token" link on your terminal that looks like,

http://0.0.0.0:8888/?token= ...

or

http://127.0.0.1:8888/?token= ...

Upvotes: 6

rofrano
rofrano

Reputation: 51

Using the answers in this thread, I have come up with a one liner for running Jupyter notebooks with the latest Anaconda docker image (4.0) that will share your current directory with the container and thus saving the notebooks on your host computer.

docker run -t --rm -p 8888:8888 -v $(PWD):/opt/notebooks continuumio/anaconda /bin/bash -c "/opt/conda/bin/jupyter notebook --ip=0.0.0.0 --port=8888 --notebook-dir=/opt/notebooks --allow-root --no-browser"

Note that by mapping your current working directory as a volume in the container (-v $(PWD):/opt/notebooks) and then specifying that as the notebook directory to Jupyter (--notebook-dir=/opt/notebooks) you don't need to make the directory. Docker will automatically do that for you and share it with your host computer. Also note that by adding --rm to the call, docker will remove the container once you shut down the Jupyter server so that you don't have to clean them up later.

You can also use it as an alias in your .bash_profile like this:

alias jupyter='docker run -t --rm -p 8888:8888 -v $(PWD):/opt/notebooks continuumio/anaconda /bin/bash -c "/opt/conda/bin/jupyter notebook --ip=0.0.0.0 --port=8888 --notebook-dir=/opt/notebooks --allow-root --no-browser"'

Then you can just use:

$ jupyter

any time you want to use jupyter notebook using the current directory as your notebook folder. (hope this is helpful to someone)

Upvotes: 4

Arian Acosta
Arian Acosta

Reputation: 6827

To run Jupyter Notebook without going into the Docker container do:

  1. Get the docker image

    docker pull continuumio/anaconda3

  2. Install and start Jupyter Notebook

    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 --allow-root"

    Note that we are passing the --allow-root flag.

  3. After installing, the Notebook server should show a link with a token:

Jupyter Notebook running in Docker

  1. On your browser visit one of the provided URLs or use localhost:

http://localhost:8888/?token=YOUR_TOKEN

Upvotes: 1

Related Questions