Try   HackMD

Share Jupyter notebook with Docker

Go to the work directory with the notebook file

Create the requirements.txt File

For example:

notebook==7.0.2
pandas==1.5.3
folium==0.14.0
matplotlib==3.7.2
sklearn-pandas==2.2.0
seaborn==0.12.2

Create the Dockerfile

For example:

# Use Python 3.11.4 as the base image
FROM python:3.11.4

# Set the working directory
WORKDIR /app

# Copy requirements.txt to the container
COPY requirements.txt .

# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt

# Copy your Jupyter Notebook and other files
COPY SacRT_LRVs_EDA.ipynb .

# Expose the Jupyter Notebook port
EXPOSE 8888

# Command to run Jupyter Notebook with --allow-root flag
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root", "SacRT_LRVs_EDA.ipynb"]

Build the Dockerfile and run the image

build with

docker build -t mysharednotebook .

If you want to make sure that your image has been created, you can type:

docker images

Run the image with

docker run -it -p 8888:8888 mysharednotebook

Directly run the image using Docker Desktop seems to generate URL that's not accessible

Share Docker Image

To share your Docker image and Jupyter Notebook with other collaborators, you can follow these steps:

  1. Share the Docker Image:

    You can share the Docker image you created by pushing it to a container registry. Popular container registries include Docker Hub, Amazon ECR, Google Container Registry, and others. Docker Hub is commonly used for public images, and you can also set up private repositories there.

    1. Tag the Image: Before pushing, tag your Docker image with a repository name. For example, if you're using Docker Hub, your tag might look like: docker tag my_image_name:latest your-dockerhub-username/my_image_name:latest.

    2. Login to Docker Hub: If you're using Docker Hub, log in to your Docker Hub account from the command line using docker login.

    3. Push the Image: Use docker push your-dockerhub-username/my_image_name:latest to push the image to Docker Hub.

  2. Share the Jupyter Notebook:

    If the Jupyter Notebook contains local paths to data on your machine, your collaborators won't be able to access the data directly. You have a few options:

    • Upload Data to a Shared Location: Upload the necessary data files to a cloud storage service (e.g., Google Drive, Dropbox, Amazon S3) and share the download links with your collaborators. Update the notebook to read data from these URLs.

    • Include Data in the Image: If the data is not too large, you can include it within the Docker image itself. Add the data files to the Docker image during the build process. However, this increases the size of the image.

    • Provide Instructions: If the data is too large to include in the image, provide clear instructions to your collaborators on how to obtain the data locally and where to place it. You might want to create a specific folder within the container for data and ask them to mount a volume to that location when running the container.

  3. Running the Container:

    Once your collaborators have the Docker image, they can run it using the docker run command. If you need to expose ports or mount volumes for data or notebooks, make sure to provide clear instructions.

    For example:

    ​​​docker run -p 8888:8888 -v /path/to/local/notebook:/app -v /path/to/local/data:/data your-dockerhub-username/my_image_name:latest
    

    In this command, replace /path/to/local/notebook with the path to your Jupyter Notebook and /path/to/local/data with the path to your data files on the collaborator's machine.

Remember to communicate any necessary environment variables, configurations, and instructions to your collaborators to ensure they can run the container successfully.

Note that sharing sensitive data via public container registries is not recommended. If your data contains sensitive information, consider using private repositories or other secure methods of sharing.

Push to private repositories

To push a Docker image to a private repository, you can follow these steps:

  1. Create a Private Repository:

    If you're using Docker Hub, you can create a private repository by following these steps:

    • Log in to Docker Hub.
    • Click on your profile picture in the top right corner.
    • Select "Create repository."
    • Choose a name for your repository and select "Private" under the "Visibility" option.
    • Click "Create."
  2. Tag the Image:

    After you've created the private repository, tag your local Docker image with the private repository's URL. The format is usually your-dockerhub-username/repository-name.

    ​​​docker tag my_image_name:latest your-dockerhub-username/repository-name:latest
    
  3. Log in to Docker Hub:

    Log in to your Docker Hub account from the command line using docker login.

  4. Push the Image:

    Use the docker push command to push the image to your private repository.

    ​​​docker push your-dockerhub-username/repository-name:latest
    
  5. Sharing with Collaborators:

    Once the image is in your private repository, you can grant access to your collaborators by adding their Docker Hub usernames to the repository's access settings.

    • Go to your repository on Docker Hub.
    • Click "Settings."
    • Under "Access Control," add the Docker Hub usernames of your collaborators and specify the level of access (Read, Write, Admin).
  6. Collaborators Pull the Image:

    Your collaborators can pull the private image using the docker pull command.

    ​​​docker pull your-dockerhub-username/repository-name:latest
    

Remember to provide your collaborators with the appropriate repository name and credentials to access the private repository.

Keep in mind that these instructions are specific to Docker Hub. If you're using a different container registry, the steps may be slightly different, but the general concept of creating a private repository, tagging the image, logging in, and pushing the image remains the same.

To pull: docker pull yangchuhan/comm-enhance-chyang:latest

To run: docker run -p 8888:8888 yangchuhan/comm-enhance-chyang:latest

ref:
How To Share Jupyter Notebooks With Docker
From a Jupyter Notebook to a Docker Container
Docker for Data Science — A Step by Step Guide
Docker for Data Science: An Introduction