How to connect google colab with localhost running docker image?

Question:

Errors while connecting with localhost link

I am trying to run meilisearch in colab. I run meiliSearch in docker and I have a localhost link http://localhost:7700/. I have tried both http://localhost:7700/ as well as http://192.168.43.57:7700. But I am getting error. Although I can connect with my local jupyter notebook. Any solution?

Asked By: user9301619

||

Answers:

I can’t see the full screen but most likely the error in colab is because it is hosted on remote server by Google research where as the http://localhost:7700/ is on your local system.

You can’t connect them using http://localhost:7700/.
And if you have also used your machines ip address, that is http://192.168.43.57:7700 and can’t connect then did you first expose your port number 7700 to internet?

You will need to expose your local machine and some ports(7700) using different services which provide the required.

You can read the following article on how to setup tunneling service or ssh to expose the local development on internet.

Medium Article on How to expose a local development server to the Internet

Or use this directly. Use local tunnel

Answered By: Prashant Solanki

Please Follow below steps to connect docker image as a local runtime for Google-colab:

  1. Create a Dockerfile and paste the below contents

# Jupyter data science image including all packages, you may change the image as per your requirements
FROM jupyter/datascience-notebook:latest

# Colab step 1
RUN pip install jupyter_http_over_ws 
    && jupyter serverextension enable --py jupyter_http_over_ws

# Expose the port for host network
EXPOSE 8888

# Colab step 2
CMD ["jupyter","notebook","--NotebookApp.allow_origin='https://colab.research.google.com'","--allow-root","--port","8888","--NotebookApp.port_retries=0","--ip","0.0.0.0"]

  1. Build the colab-runtime image by running the command in the terminal from the same directory where Dockerfile is saved

docker build -t colab-runtime .

An Image colab-runtime:latest will be created.

  1. Fire the command in terminal to run image as a container

    Note:- Replace {your_host_path} with your host system path. e.g /home/mujassim/work:/home/jovyan/work

  • Do not change /home/jovyan/work path.

docker run -it -p 8888:8888 -v {your_host_path}:/home/jovyan/work colab-runtime:latest

After Running the above command, A URL is provided by the container in the logs.

  1. Paste that URL in colab runtime by replacing only 127.0.0.1 or container_id with localhost

  2. If you dont want to run step no. 3 again and again to start the jupyter server then follow the commands listed below:

    • Note down the container id and name of currently running jupyter server by: docker container ls
    • Stop the container for a while: docker container stop {container_id}
    • Rename the container : docker rename {old_container_name} colab-runtime
    • Now you may run the following commands to start or stop the local runtime for google-colab: docker container start colab-runtime or docker container stop colab-runtime

Note: You have to provide new URL each time in google colab.

Answered By: Mujassim JamaL