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 and Debugging PHP7 in Docker

Before developing applications in any new language/platform I make sure that the debugger works in the set up. I had been working on some projects in Python and PHP recently and I have already shared my notes on setting up and debugging Python (Django) application in Docker and how to debug the application.

In this post I will describe how to setup PHP7 in Docker and debug the application. You can checkout the project from this Github repo.

Here is the Docker file for this setup –

FROM php:7.0.17-apache

RUN apt-get update
RUN apt-get install -y apt-utils vim curl sqlite3
RUN pecl install xdebug
# copy test db file
ADD ./db/employee.db /employee.db
# The base image does not have php.ini. 
# Copy our own, with xdebug settings
ADD ./php.ini /usr/local/etc/php/

EXPOSE 80

The container is based on php image from docker hub. The version number is 7.0.17-apache. This example uses Sqlite3 database, so the package is installed.

To debug PHP applications, you need to install xdebug. In the docker file it is installed using pecl. Then employee.db (Sqlite3 DB) is copied from the project folder to the root of docker container.

Continue reading “Setting up and Debugging PHP7 in Docker”

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”

Docker Notes

I recently started using Docker and am very impressed with how easily you can set up environment to develop, test and deploy applications. It provides a virtualization environment, but unlike some of the other virtualization solution, Docker container  (runtime environment for your application)  is a process instead of a complete OS. It runs natively on Linux and uses a single virtual machine on Windows and Mac in which Docker containers run.

Using Docker you can easily create containers required for your application to run, for example containers for database, application server etc and easily deploy them to test or production environment. You can create images of your set-up and re-create the same setup constantly from those images. When you create a Docker image, it has to be based on some variant of Linux base image. You can browse Docker images at Docker Hub.

This post is not meant to be tutorial or detailed information about Docker. You can refer to Docker web site for that. As many of my other posts, this post is meant to be reference for me – about some of the Docker commands I have used so far. Continue reading “Docker Notes”

Social