在这篇文章中 我将创建一个python容器并在docker容器中运行python脚本,然后直接通过docker运行python脚本。

拉并启动Docker映像

docker pull python
启动一个python容器
docker run -it -d --name mypython -p 8888:80 -v /data/www/python/:/usr/src/myapp/python bash
说明:
–name mypython容器名称
-p 8888:80主机端口8888映射到容器端口80
-v / data / www / python /:/ usr / src / myapp /映射目录,容器目录/ usr / src / myapp /持久到主机/ data / www / python /目录




    在Docker中执行Python脚本

    然后在主机上写一个hello.py文件,然后输入容器
    docker exec -it mypython bash
    在容器中运行Python程序
    python /usr/src/myapp/hello.py
    $ docker run -it -d --name python -p 82:80 -v /data/www/python/:/usr/src/myapp/ mypython bash
    Unable to find image 'python:latest' locally
    latest: Pulling from library/python
    90fe46dd8199: Pull complete 
    35a4f1977689: Pull complete 
    bbc37f14aded: Pull complete 
    74e27dc593d4: Pull complete 
    4352dcff7819: Pull complete 
    deb569b08de6: Pull complete 
    98fd06fa8c53: Pull complete 
    7b9cc4fdefe6: Pull complete 
    512732f32795: Pull complete 
    Digest: sha256:ad7fb5bb4770e08bf10a895ef64a300b288696a1557a6d02c8b6fba98984b86a
    Status: Downloaded newer image for python:latest
    2374355d7d50455008de0de70d62fc69ca018c3661bfbce78755bb480ec83628
    [node1] (local) [email protected] ~
    $ cd /data/www/python/
    [node1] (local) [email protected] /data/www/python
    $ vi hello.py
    print ("Hello World!!!")
    
    
    $ ls
    hello.py
    [node1] (local) [email protected] /data/www/python
    $ docker exec -it mypython bash
    [email protected]:/# python /usr/src/myapp/hello.py
    Hello World!!!
    [email protected]:/# exit
    exit

    使用Docker执行Python脚本

    [node1] (local) [email protected] /data/www/python
    $ docker exec -it python python /usr/src/myapp/hello.py
    Hello World!!!
    [node1] (local) [email protected] /data/www/python
    $ 

    通过 约翰扬

    发表评论