Giter Club home page Giter Club logo

docker-demo-with-simple-python-app's Introduction

python-app-docker-demo

This demo shows two steps:

  • Install docker-ce on Centos 7
  • Build and run a simple docker image with a python+flask+gunicorn web application.

Install docker-ce on Centos 7

Refer to https://docs.docker.com/engine/installation/linux/docker-ce/centos/ You can also find other OS installation docs from here.

Uninstall old versions

$ sudo yum remove docker \
                  docker-common \
                  docker-selinux \
                  docker-engine

Install using repository

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce
sudo systemctl start docker
sudo docker run hello-world

Other commands:

  • check docker status
sudo systemctl status docker.service
  • stop docker
sudo systemctl stop docker
  • uninstall docker-ce
sudo yum remove docker-ce
  • remove all images, container, volumes
sudo rm -rf /var/lib/docker

Build/Run a simple python+flask docker web app

Create the Dockerfile

FROM python:2.7

# Creating Application Source Code Directory
RUN mkdir -p /usr/src/app

# Setting Home Directory for containers
WORKDIR /usr/src/app

# Installing python dependencies
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt

# Copying src code to Container
COPY . /usr/src/app

# Application Environment variables
#ENV APP_ENV development
ENV PORT 8080

# Exposing Ports
EXPOSE $PORT

# Setting Persistent data
VOLUME ["/app-data"]

# Running Python Application
CMD gunicorn -b :$PORT -c gunicorn.conf.py main:app

Build your image

Normally, image name convention is something like: {company/application-name}:{version-number}. In the demo, I just use {application-name}:{version-number}

sudo docker build -t my-python-app:1.0.1 .

check all docker images

$ sudo docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
my-python-app           1.0.1               2b628d11ba3a        22 minutes ago      701.6 MB
docker.io/python        2.7                 b1d5c2d7dda8        13 days ago         679.3 MB
docker.io/hello-world   latest              05a3bd381fc2        5 weeks ago         1.84 kB

2b628d11ba3a is the image ID, some commands based on the ID.

  • tag
sudo docker tag 2b628d11ba3a my-python-app:1.0.1
sudo docker tag 2b628d11ba3a my-python-app:latest
  • remove image
$ sudo docker rmi --force 2b628d11ba3a

Run your image

$ sudo docker run -d -p 8080:8080 my-python-app:1.0.1

You can use sudo docker ps to list all running containers.

$ sudo docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
4de6041072b7        my-python-app:1.0.1   "/bin/sh -c 'gunicorn"   20 minutes ago      Up 20 minutes       0.0.0.0:8080->8080/tcp   elegant_kowalevski

4de6041072b7 is the running container id. Some commands below are what you might need.

  • display logs in running container
$ sudo docker logs 4de6041072b7
[2017-10-23 20:29:49 +0000] [7] [INFO] Starting gunicorn 19.6.0
[2017-10-23 20:29:49 +0000] [7] [INFO] Listening at: http://0.0.0.0:8080 (7)
[2017-10-23 20:29:49 +0000] [7] [INFO] Using worker: gthread
[2017-10-23 20:29:49 +0000] [11] [INFO] Booting worker with pid: 11
[2017-10-23 20:29:49 +0000] [12] [INFO] Booting worker with pid: 12

  • stop your container
$ sudo docker stop 4de6041072b7
  • login inside the container
$ sudo docker exec -it 4de6041072b7 /bin/sh
# ls /usr/src/app
Dockerfile  README.md  gunicorn.conf.py  gunicorn_pid.txt  main.py  main.pyc  requirements.txt
# exit

Test your application

$ curl http://localhost:8080
Hello World

docker-demo-with-simple-python-app's People

Contributors

bennzhang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.