1. 程式人生 > >docker 執行python web服務

docker 執行python web服務

建立一個最最簡單的python+django專案,然後在docker容器中執行。 環境: 192.168.174.134伺服器中 docker映象: python:2(python2.7.15) django: 1.8.3 1. 在134伺服器中建立python專案 django-admin startproject ops python manage.py migrate 建立一個root使用者 python manage.py createsuperuser

[email protected]:~/ops# pwd
/root/ops
[email protected]:~/ops# ls
db.sqlite3  manage.py  ops
[email protected]
:~/ops# ls ops __init__.py __init__.pyc settings.py settings.pyc urls.py urls.pyc wsgi.py wsgi.pyc

2. docker pull python:2將映象pull下來

[email protected]:~/ops# docker images | grep python
python                                 2                   6f0ab4c651e7        4 days ago          907.7 MB

3. 編寫Dockerfile

[email protected]:~/docker/dockerfile# ls
Django-1.8.3.tar.gz  Dockerfile  Dockerfile.bak  ops  ops.tar.gz
[email protected]:~/docker/dockerfile# cat Dockerfile
FROM python:2
ADD Django-1.8.3.tar.gz /usr/local/src

WORKDIR /usr/local/src/Django-1.8.3

RUN python setup.py install
ADD ops.tar.gz /code

WORKDIR /code/ops

ENV PATH /usr/bin/python:$PATH

EXPOSE 8888

CMD ["/bin/bash", "run.sh"]

其中run.sh放在了ops專案的目錄下,為啟動python服務的指令碼

[email protected]:~/docker/dockerfile/ops# ls
db.sqlite3  manage.py  ops  run.sh
[email protected]:~/docker/dockerfile/ops# cat run.sh 
python /code/ops/manage.py runserver 0.0.0.0:8888
[email protected]:~/docker/dockerfile/ops# pwd
/root/docker/dockerfile/ops

4. 通過Dockerfile構建映象 docker build -t ops:v1 . 之前構建過了,構建日誌比較簡單了

Sending build context to Docker daemon 7.351 MB
Sending build context to Docker daemon 
Step 0 : FROM python:2
 ---> 6f0ab4c651e7
Step 1 : ADD Django-1.8.3.tar.gz /usr/local/src
 ---> Using cache
 ---> 3396d9f2fe48
Step 2 : WORKDIR /usr/local/src/Django-1.8.3
 ---> Using cache
 ---> 4748eaa68a72
Step 3 : RUN python setup.py install
 ---> Using cache
 ---> 2c9ed9c547b2
Step 4 : ADD ops.tar.gz /code
 ---> Using cache
 ---> 8a513509c908
Step 5 : WORKDIR /code/ops
 ---> Using cache
 ---> b097beb37496
Step 6 : ENV PATH /usr/bin/python:$PATH
 ---> Using cache
 ---> 3264fc493457
Step 7 : EXPOSE 8888
 ---> Using cache
 ---> c719b6159390
Step 8 : CMD /bin/bash run.sh
 ---> Using cache
 ---> 9f1e094d0189
Successfully built 9f1e094d0189

檢視映象:

[email protected]:~/docker/dockerfile# docker images | grep ops
ops                                    v1                  f3fccc152eaa        11 seconds ago      983.8 MB

5. 建立執行容器

[email protected]:~/docker/dockerfile# docker run -d --name ops -p 8888:8888 ops:v1
eeb14a3e7ea034f93a3e9158734e1f63fce85cb267a836b45d410fbd88306c64

檢視將容器中的8888埠對映到本機的8888埠

[email protected]:~/docker/dockerfile# docker ps -l
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                    NAMES
eeb14a3e7ea0        ops:v1              "/bin/bash run.sh"   35 seconds ago      Up 34 seconds       0.0.0.0:8888->8888/tcp   ops
[email protected]:~/docker/dockerfile# netstat -antp | grep 8888
tcp6       0      0 :::8888                 :::*                    LISTEN      52832/docker-proxy

訪問驗證 在這裡插入圖片描述