{"id":2179,"date":"2024-05-09T16:30:19","date_gmt":"2024-05-09T11:00:19","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=2179"},"modified":"2024-05-09T16:31:33","modified_gmt":"2024-05-09T11:01:33","slug":"running-jupyter-notebook-in-ubuntu-docker-container","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/running-jupyter-notebook-in-ubuntu-docker-container\/","title":{"rendered":"Running Jupyter Notebook in Ubuntu Docker Container"},"content":{"rendered":"\n<p>Recently I attempted to run Jupyter Notebook in Ubuntu Docker container for a project requiring OpenCV and PyTorch. Sharing the Docker configuration files I used here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>docker-compose.yml<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>version: \"3\"\n\nservices: \n  ml-docker:\n    build: .\n    container_name: ML-docker\n    stdin_open: true\n    tty: true\n    ports:\n      - '8888:8888'\n    volumes: \n      - $PWD\/shared:\/shared<\/code><\/pre>\n\n\n\n<p>Jupyter notebook listens on port 8888 by default. So, I am mapping it to the same port on the host machine. I have also created a folder \u2018shared\u2019 in the same folder where above docker-compose.yml is located. And I have made this folder accessible to the container using <em>volumes<\/em> declaration. The above file instructs docker to build Dockerfile in the same folder. Content of the Dockerfile is as follows &#8211;<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dockerfile<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM ubuntu\n\nRUN apt-get update\n\n#required for OpenCV\nRUN apt-get install -y libgl1-mesa-glx \nRUN apt-get install -y libglib2.0-0\n\nRUN apt install -y python3\nRUN apt install -y python3-pip\nRUN python3 -m pip install torch torchvision\nRUN python3 -m pip install transformers \nRUN python3 -m pip install numpy pandas matplotlib jupyter opencv-python<\/code><\/pre>\n\n\n\n<p>Base image is ubuntu. It then has commands to install Python3 and required libraries for OpenCV and PyTorch. <br><br>To start the container, run following command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em><strong>docker-compose up<\/strong><\/em><\/code><\/pre>\n\n\n\n<p>In a separate terminal window, run \u201c<strong><em>docker ps<\/em><\/strong>\u201d to get the id of the newly launched Docker container and attach to that container using following command &#8211; <\/p>\n\n\n\n<p><em><strong>docker attach &lt;replace-with-docker-id-from-ps-command&gt;<\/strong><\/em><\/p>\n\n\n\n<p>See <a href=\"http:\/\/ramkulkarni.com\/blog\/docker-notes\/\" data-type=\"post\" data-id=\"1888\">my blog on Docker<\/a> for detailed Docker commands or refer to <a href=\"https:\/\/docs.docker.com\/\">Docker documentation<\/a>.<\/p>\n\n\n\n<p>Run following Jupyter command from the container &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em><strong>jupyter notebook --allow-root --ip=\"0.0.0.0\"<\/strong><\/em><\/code><\/pre>\n\n\n\n<p>\u2014allow-root allows jupyter notebook to run as root, which by default is not allowed. \u2014ip=\u201c0.0.0.0\u201d allows it to listen to requests from all IP addresses. This will allow host environment to make request to jupyter notebook from the browser.<\/p>\n\n\n\n<p>Copy the URL with token from Jupyter notebook console output and paste it in the browser in the host environment. You can now create new notebooks (which will be created in \u2018shared\u2019 folder in your host environment) and execute them from the host environment.<\/p>\n\n\n\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I attempted to run Jupyter Notebook in Ubuntu Docker container for a project requiring OpenCV and PyTorch. Sharing the Docker configuration files I used here. docker-compose.yml Jupyter notebook listens on port 8888 by default. So, I am mapping it to the same port on the host machine. I have also created a folder \u2018shared\u2019 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/running-jupyter-notebook-in-ubuntu-docker-container\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Running Jupyter Notebook in Ubuntu Docker Container&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"everybody","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[135,115],"tags":[133,162],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-z9","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2179"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=2179"}],"version-history":[{"count":5,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2179\/revisions"}],"predecessor-version":[{"id":2185,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/2179\/revisions\/2185"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=2179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=2179"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=2179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}