BugSquanch
BugSquanch

Reputation: 326

Can't access jupyter notebook inside docker container

I know there are a lot of questions of people who are struggling with this but please read the entire post.
So I just want to create a dockerfile that exposes port 8888 to access a jupyter notebook later on.

This is my dockerfile:

FROM continuumio/anaconda3
ENV DEBIAN_FRONTEND=noninteractive

ARG USERNAME=remote
ARG USER_UID=1000
ARG USER_GID=$USER_UID

COPY environment.yml* noop.txt /tmp/conda-tmp/

RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
    && apt-get -y install git openssh-client less iproute2 procps iproute2 lsb-release \
    && if [ -f "/tmp/conda-tmp/environment.yml" ]; then /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; else echo "did not find environment.yml"; fi \
    && rm -rf /tmp/conda-tmp \
    && groupadd --gid $USER_GID $USERNAME \
    && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
    && chmod 0440 /etc/sudoers.d/$USERNAME \
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

ENV DEBIAN_FRONTEND=dialog

EXPOSE 8888

I run the following commands to get the container up and running:

docker build -t intel . (create the image)
docker run -t -d --publish=8888:8888 --name ig intel (start a container based on the image)

Everything up to this moment runs without any problems, but now comes the thing I don't understand:
This runs just fine. jupyter notebook --allow-root --no-browser --ip 0.0.0.0 --port 8888

But when I try to go to

localhost:8888?/token=(the token jupyter provides)
or
ipofthecontainer:8888?/token=(the token jupyter provides)

It does nothing(connection timed out) and I have no clue why.

I check the ip of the container with:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name

and I verify that the EXPOSE worked with:

docker ps

platform: windows
python-version: 3.7.7
conda environment: yes

What am I not seeing here?

Upvotes: 2

Views: 6449

Answers (2)

dejanualex
dejanualex

Reputation: 4358

First modify your docker file in order to start jupyter notebook, add this line at the end of the Dockerfile:

CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

Then build again the image and when you start the container use -p option: --publish , -p Publish a container’s port(s) to the host

docker run -t -d -p 8888:8888 --name ig intelligait3d where the mapping is -p <HOST port>:<CONTAINER port>

Here you can find the reference for docker run command: https://docs.docker.com/engine/reference/commandline/run/

Upvotes: 4

DazWilkin
DazWilkin

Reputation: 40416

EXPOSE is only for documentation; it has no effect.

You need to add --publish=8888:8888 to your docker run so that the container's port is mapped to a host port.

docker run --tty --detach --publish=8888:8888 --name=ig intelligait3d

NB The publish flag does not require the host-port:container-port to match

Upvotes: 2

Related Questions