Docker Python應用程序示例


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

  1. 創建一個目錄
[email protected]:~$ mkdir -p /home/yiibai/docker/docker-python-app
  1. 進入目錄
[email protected]:~$ 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命令。

[email protected]:~/docker/docker-python-app$ sudo docker build -t python-app .

執行結果如下 -

[email protected]:~/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
[email protected]:~/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
[email protected]:~/docker/docker-python-app$
  1. 查看可用的Docker映像

可以通過以下命令查看所有可用的Docker映像。

[email protected]:~/docker/docker-python-app$ docker images

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

[email protected]:~/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
[email protected]:~/docker/docker-python-app$
  1. 運行Docker

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

[email protected]:~/docker/docker-python-app$ sudo docker run python-app

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

[email protected]:~/docker/docker-python-app$ sudo docker run python-app
Hello from python file
[email protected]:~/docker/docker-python-app$