Given a Dockerfile on an EC2 instance that has, at the very least, these commands at the end:

EXPOSE 8888
ENTRYPOINT ["/bin/bash"]
  1. Build the image
docker build -t <image name> .
  1. Run the container
docker run -it -p 8888:8888 --ip=0.0.0.0 <image name>

If you want to persist data from the host to the container, include bind mounts:

docker run -it -p 8888:8888 -v /abs/path/to/data:/app/data --ip=0.0.0.0 <image name>
  1. Within the container, run Jupyter Lab (note, specifying the port isn’t necessary if you’re using the default port 8888):
jupyter lab --port=8888 --ip=0.0.0.0 --allow-root

Troubleshooting#

If you try to open Jupyter Lab in your browser, and you get a “refused to connect” error, ensure that the SSH config you have for your connection to your EC2 instance (or for SSH in general if you don’t have a specific entry for EC2) looks like this:

Host <ip block/address>
    <any other commands here>
    LocalForward 8888 127.0.0.1:8888

If you connect via a bastion box, LocalForward only needs to be in the entry for the specific instance(s) you’re trying to connect to, and not in the entry for the bastion box.

Source#

https://stackoverflow.com/questions/39734097/unable-to-connect-to-jupyter-notebook-served-by-docker