Playing with Docker, Silex, Python, Node and WebSockets

I’m learning Docker. In this post I want to share a little experiment that I have done. I know the code looks like over-engineering but it’s just an excuse to build something with docker and containers. Let me explain it a little bit.

The idea is build a Time clock in the browser. Something like this:


Yes I know. We can do it only with js, css and html but we want to hack a little bit more. The idea is to create:

  • A Silex/PHP frontend
  • A WebSocket server with
  • A Python script to obtain the current time

WebSocket server will open 2 ports: One port to serve webSockets ( and another one as a http server (express). Python script will get the current time and it’ll send it to the webSocket server. Finally one frontend(silex) will be listening to WebSocket’s event and it will render the current time.

That’s the WebSocket server (with and express)

    express = require('express'),
    expressApp = express(),
    server = require('http').Server(expressApp),
    io = require('')(server, {origins: 'localhost:*'})

expressApp.get('/tic', function (req, res) {
    io.sockets.emit('time', req.query.time);

expressApp.listen(6400, '');


That’s our Python script

from time import gmtime, strftime, sleep
import httplib2

h = httplib2.Http()
while True:
    (resp, content) = h.request("http://node:6400/tic?time=" + strftime("%H:%M:%S", gmtime()))

And our Silex frontend

use Silex\Application;
use Silex\Provider\TwigServiceProvider;

$app = new Application(['debug' => true]);
$app->register(new TwigServiceProvider(), [
    'twig.path' => __DIR__ . '/../views',

$app->get("/", function (Application $app) {
    return $app['twig']->render('index.twig', []);


using this twig template

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Docker example</title>
    <link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="css/app.css" rel="stylesheet">
    <script src=""></script>
    <script src=""></script>
<div class="site-wrapper">
    <div class="site-wrapper-inner">
        <div class="cover-container">
            <div class="inner cover">
                <h1 class="cover-heading">
                    <div id="display">
<script src="//localhost:8080/"></script>
<script src=""></script>
var socket = io.connect('//localhost:8080');

$(function () {
    socket.on('time', function (data) {

The idea is to use one Docker container for each process. I like to have all the code in one place so all containers will share the same volume with source code.

First the node container (WebSocket server)

FROM node:argon

RUN mkdir -p /mnt/src
WORKDIR /mnt/src/node

EXPOSE 8080 6400

Now the python container

FROM python:2

RUN pip install httplib2

RUN mkdir -p /mnt/src
WORKDIR /mnt/src/python

And finally Frontend contailer (apache2 with Ubuntu 16.04)

FROM ubuntu:16.04

RUN locale-gen es_ES.UTF-8
RUN update-locale LANG=es_ES.UTF-8
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update -y
RUN apt-get install --no-install-recommends -y apache2 php libapache2-mod-php
RUN apt-get clean -y

COPY ./apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf

RUN mkdir -p /mnt/src

RUN a2enmod rewrite
RUN a2enmod proxy
RUN a2enmod mpm_prefork

RUN chown -R www-data:www-data /mnt/src
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2/


Now we’ve got the three containers but we want to use all together. We’ll use a docker-compose.yml file. The web container will expose port 80 and node container 8080. Node container also opens 6400 but this port is an internal port. We don’t need to access to this port outside. Only Python container needs to access to this port. Because of that 6400 is not mapped to any port in docker-compose

version: '2'

    image: gonzalo123/example_web
    container_name: example_web
     - "80:80"
    restart: always
      - node
      context: ./images/php
      dockerfile: Dockerfile
      - /usr/sbin/apache2
      - -D
     - ./src:/mnt/src

    image: gonzalo123/example_node
    container_name: example_node
     - "8080:8080"
    restart: always
      context: ./images/node
      dockerfile: Dockerfile
      - npm
      - start
     - ./src:/mnt/src

      image: gonzalo123/example_python
      container_name: example_python
      restart: always
        - node
        context: ./images/python
        dockerfile: Dockerfile
        - python
       - ./src:/mnt/src

And that’s all. We only need to start our containers

docker-compose up --build -d

and open our browser at: http://localhost to see our Time clock

Full source code available within my github account

About Gonzalo Ayuso

Web Architect. PHP, Python, Node, Angular, ionic, PostgreSQL, Linux, ... Always learning.

Posted on January 2, 2017, in Docker, js, node.js, php, Python, silex,, Symfony, Technology, twig, Web Development, Websockets and tagged , , , , , , , , , . Bookmark the permalink. 8 Comments.

  1. Hello, very interesting! Just my two cents 😉

    – As the compose file talks about ‘composition’ of containers, I would hide container implementation details on it. I mean, better specify container entrypoint on Dockerfiles (ENTRYPOINT sentence).
    – You can group RUN operations on Dockerfile to generate less layers and smaller containers. For example, instead of three RUN sentences invoking apt-get, have only one RUN apt-get update && apt-get install && apt-get clean.

    • Thanks. I use entrypoint in docker-compose especially because node containers. I prefer to put code in a mounted container (and share it between containers). That means that code isn’t available (I cannot run npm start) until docker-compose is executed. Anyway I agree with you.

  1. Pingback: PHP Annotated Monthly – January 2017 | PhpStorm Blog

  2. Pingback: Trabalhando com Docker, Silex, Python, Node e WebSockets -

  3. Pingback: Trabalhando com Docker, Silex, Python, Node e WebSockets | grupo IO Multi Soluções Inteligentes

  4. Pingback: Trabalhando com Docker, Silex, Python, Node e WebSockets – Tudo sobre PHP

  5. Pingback: Is correct use websocket to this purpose? – program faq

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: