Running Jupyter Notebook in Ubuntu Docker Container

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

version: "3"

services: 
  ml-docker:
    build: .
    container_name: ML-docker
    stdin_open: true
    tty: true
    ports:
      - '8888:8888'
    volumes: 
      - $PWD/shared:/shared

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 ‘shared’ in the same folder where above docker-compose.yml is located. And I have made this folder accessible to the container using volumes declaration. The above file instructs docker to build Dockerfile in the same folder. Content of the Dockerfile is as follows –

Continue reading “Running Jupyter Notebook in Ubuntu Docker Container”

Setting up Ubuntu for HuggingFace

Just wanted to document steps I took to successfully setup an Ubuntu machine for using models on HuggingFace. Refer to https://huggingface.co/docs/transformers/installation for installation instructions for using Transformers module.

Ubuntu comes installed with Python3. I started with creating a new environment for the project. I switched to the folder where I was going to create projects/files for using HuggingFace

Creating new Python Environment

  1. python3 -m venv .env
  2. source .env/bin/activate
Continue reading “Setting up Ubuntu for HuggingFace”

Remote Debugging Django Project in Docker

In the last post I described how to setup Django and Apache in Docker container. In this post I will describe how to remote debug the Django application running in the same setup. If you look at the Dockerfile of the project (in the last post), you would see that it installs ptvsd package – this package helps to debug Django applications running remotely using Visual Studio Code.

Install Visual Studio Code, if you haven’t installed it already. Then install VS Code extension for Python. I have installed this extension by Don Jayamanne, and it supports debugging of Python applications. If you don’t know how to install extensions in VS Code, this video might help you.

To enable remote debugging of Django application, or Python applications in general, you need to run ptvsd server on the machine where Django is configure, which in our case is a Docker container. You need to embed following code snippet in your Django app, and it needs to run only once, because it listens to a port.

import ptvsd

ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3500))

The above snippet starts ptsvd server, that listens on port 3500. We configured Python to run in Apache2 using mod-wsgi in the Docker container, but I haven’t found a way to embed the above code in a Django application in this setup, where it would run only once (if it is run multiple times, it will try to attach to the same port and that would fail). So far the best way I have found to debug Django application remotely in Docker is to start Django development server on a different port (than one where Apache is listening) and embed the debugger code in manage.py. You run the development server by running manage.py, for example – Continue reading “Remote Debugging Django Project in Docker”

Docker project for Python3, Django and Apache2 setup

I was working on a Django project and it took me some time to make Django work with Apache2. So I thought I would create a Docker project for this setup. You can find the project at https://github.com/ramkulkarni1/django-apache2-docker. It sets up Python3, Django, Apache2 and a sample Django site too.

All configurations for making Django work with Apache2 are created when you create a Docker container using this project. Simply run following command from the project folder.

$ docker-compose up

(See my post Docker Notes on information about setting up Docker and docker commands). One the container is up, browse to http://localhost:8005.

Here is a quick summary of what Dockerfile does – Continue reading “Docker project for Python3, Django and Apache2 setup”

Configuring Apache on Mac OS X (Mavericks) for Python scripting

I spent a couple of hours today trying to figure out how to configure Apache Web Server on Mac OS X to execute Python scripts, so I thought I would document the process for my own reference.

OS X is preinstalled with Apache and Python. Apache executable (apachectl) is at /usr/sbin/apachectl and Python is at /usr/bin/python. But the configurations for Apache are at /etc/apache2, specifically in the file httpd.conf. If you open the file and look for DocumentRoot, you will find that default document root is set to /Library/WebServer/Documents.

I did not want to change the default doc root but at the same time did not want to store my scripts in the default folder. I could have created a virtual host or an alias. I decided to do the later because it is simpler than creating virtual host; and I was configuring Python for development only. So just below the document root setting I added the alias – Continue reading “Configuring Apache on Mac OS X (Mavericks) for Python scripting”

Social