Skip to content

surajssd/hitcounter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Walkthrough containerizing application

1. Running the app in traditional way

1.1. Start redis server

  • Install redis

$ sudo dnf -y install git redis python2-virtualenv python-pip python-devel
  • Start redis server

$ sudo systemctl start redis

1.2. Start flask server

  • Clone this repository

$ git clone https://github.com/container-workbook/docker-workshop/
$ cd docker-workshop/pyconapp/
  • Create a python virtualenv

$ virtualenv venv
$ . venv/bin/activate
(venv) $
  • Install all the requirements

(venv) $ pip install -r requirements.txt
  • Start flask app

(venv) $ export REDIS_HOST=127.0.0.1
(venv) $ python app.py

1.3. Check if app is running

$ curl 127.0.0.1:5000
<h3>Hello Pythonistas</h3> <br/><h2>Number of Hits:</2> 1<br/>

2. Running app in container

2.1. Containerize flask app

  • Create Dockerfile that looks like this:

$ cat Dockerfile
FROM fedora:24

RUN dnf update -y && \
    dnf install -y python-pip python-devel

ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it.sh

RUN chmod +x /usr/local/bin/wait-for-it.sh && mkdir -p /app

WORKDIR /app
COPY . /app

RUN pip install -r requirements.txt

EXPOSE 5000

ENTRYPOINT /app/entrypoint.sh
  • Create an entrypoint.sh file which looks like this:

$ cat entrypoint.sh
#!/bin/bash

/usr/local/bin/wait-for-it.sh REDIS_HOST:6379

cd /app
python app.py
  • Add permissions to entrypoint.sh

$ chmod +x entrypoint.sh

2.2. Multi-container app definition

  • Create a docker-compose.yml file that defines multi-container application

$ cat docker-compose.yml
version: '2'

services:
  redis:
    image: redis:latest
    ports:
      - "6379"

  app:
    build: .
    ports:
      - "5000"
    environment:
      - REDIS_HOST=redis

2.3. Bring up the application

$ docker-compose up

2.4. Check if the app is running

  • Find out the port flask app is exposed on:

$ docker ps | grep pyconapp_app
  • curl on it:

$ curl 127.0.0.1:32774
<h3>Hello Pythonistas</h3> <br/><h2>Number of Hits:</2> 1<br/>

So that’s how you can migrate an app that is monolithic and containerize it.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published