# 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
```
:::warning
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:
```bash
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.
:::info
:::spoiler 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`.
```bash
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.
```bash
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.
```bash
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.
:::
:::info
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](https://predictivehacks.com/how-to-share-jupyter-notebooks-with-docker/)
[From a Jupyter Notebook to a Docker Container](https://ploomber.io/blog/notebook-to-docker/)
[Docker for Data Science — A Step by Step Guide](https://towardsdatascience.com/docker-for-data-science-a-step-by-step-guide-1e5f7f3baf8e#39fe)
[Docker for Data Science: An Introduction](https://www.datacamp.com/tutorial/docker-for-data-science-introduction?irclickid=xGr0nGxHpxyPUdHS3AzPOUtwUkF3m4QwQXBn0I0&irgwc=1&utm_medium=affiliate&utm_source=impact&utm_campaign=000000_1-2003851_2-mix_3-all_4-na_5-na_6-na_7-mp_8-affl-ip_9-na_10-bau_11-Bing%20Rebates%20by%20Microsoft&utm_content=BANNER&utm_term=EdgeBingFlow)