Docker Python應用程式範例


在這篇教學文章中,將演示如何在docker中執行python應用程式。首先需要建立Dockerfile,可參考以下步驟,並一步步實踐。

  1. 建立一個目錄
yiibai@ubuntu:~$ mkdir -p /home/yiibai/docker/docker-python-app
  1. 進入目錄
yiibai@ubuntu:~$ cd /home/yiibai/docker/docker-python-app
  1. 建立一個名稱為:Dockerfile的Dockerfile檔案
FROM python
COPY . /home/yiibai/src
CMD ["python", "/home/yiibai/src/index.py"]
  1. 建立一個Python檔案

建立一個在Docker容器中執行的python檔案:index.py

print("Hello from python file");
  1. 建立Docker映像

要建立這個python應用程式的Docker映像,需要使用以下Docker命令。

yiibai@ubuntu:~/docker/docker-python-app$ sudo docker build -t python-app .

執行結果如下 -

yiibai@ubuntu:~/docker/docker-python-app$ docker build -t python-app .
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.29/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=python-app&target=&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied
yiibai@ubuntu:~/docker/docker-python-app$ sudo docker build -t python-app .
[sudo] password for yiibai:
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM python
latest: Pulling from library/python
10a267c67f42: Already exists
fb5937da9414: Pull complete
9021b2326a1e: Pull complete
dbed9b09434e: Pull complete
ea8a37f15161: Pull complete
9fb634154ace: Pull complete
6df6941e6752: Pull complete
df67ecfc860b: Pull complete
Digest: sha256:b2dfd12f7fb4997345b11b74b154849d0549e270221030cd0450b85b7f6c0e92
Status: Downloaded newer image for python:latest
 ---> b6cc5d70bc28
Step 2/3 : COPY . /src
 ---> 3f5ca1d2cf05
Removing intermediate container dc2c6cc3df67
Step 3/3 : CMD python /src/index.py
 ---> Running in 998b7f2b3ed2
 ---> bc4ea57dd1b5
Removing intermediate container 998b7f2b3ed2
Successfully built bc4ea57dd1b5
Successfully tagged python-app:latest
yiibai@ubuntu:~/docker/docker-python-app$
  1. 檢視可用的Docker映像

可以通過以下命令檢視所有可用的Docker映像。

yiibai@ubuntu:~/docker/docker-python-app$ docker images

執行上面命令,結果如下 -

yiibai@ubuntu:~/docker/docker-python-app$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python-app          latest              bc4ea57dd1b5        3 minutes ago       689MB
php-app             latest              7bd0234c2184        33 hours ago        390MB
<none>              <none>              0420419feb44        33 hours ago        390MB
<none>              <none>              ca59a5a4f3fb        33 hours ago        390MB
<none>              <none>              c2e626f9d9c6        34 hours ago        390MB
java-app            latest              bd61e7f49911        2 days ago          643MB
php                 7.0-apache          a4322279ced1        2 days ago          390MB
python              latest              b6cc5d70bc28        3 weeks ago         689MB
java                8                   d23bdf5b1b1b        4 months ago        643MB
hello-world         latest              48b5124b2768        4 months ago        1.84kB
yiibai@ubuntu:~/docker/docker-python-app$
  1. 執行Docker

建立Docker映像後,現在可以使用以下命令來執行它。

yiibai@ubuntu:~/docker/docker-python-app$ sudo docker run python-app

上述命令執行Docker映像並執行python檔案。 執行後,產生結果如下輸出所示。

yiibai@ubuntu:~/docker/docker-python-app$ sudo docker run python-app
Hello from python file
yiibai@ubuntu:~/docker/docker-python-app$