溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么運(yùn)用docker配置python開發(fā)環(huán)境

發(fā)布時(shí)間:2022-05-25 14:40:51 來(lái)源:億速云 閱讀:299 作者:iii 欄目:大數(shù)據(jù)

本文小編為大家詳細(xì)介紹“怎么運(yùn)用docker配置python開發(fā)環(huán)境”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“怎么運(yùn)用docker配置python開發(fā)環(huán)境”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

步驟:

1.安裝docker(這里不詳細(xì)介紹)

# 參考命令
sudo wget -qo- https://get.docker.com/ | sh

2.應(yīng)用目錄結(jié)構(gòu)

├──fanxiangce
_docker
  ├── dockerfile
  ├── readme.md 
  └─fanxiangce
    └──app
    ├── manage.py            
      └── requirements
      ├── common.txt

3.編寫dockerfile(詳細(xì)命令解釋可以參考)

########################################################## 
# dockerfile to run a flask-based web application# based on an centos:7 image 
##########################################################
# set the base image to use to centos 
from centos:7

# set the file maintainer 
maintainer jasonwang,wjs7740@163.com

# set env varibles used in this dockerfile (add a unique prefix, such as dockyard) 
# local directory with project source 
env dockyard_src=fanxiangce

# directory in container for all project files 
env dockyard_srchome=/opt 
# directory in container for project source files 
env dockyard_srcproj=/opt/fanxiangce

# update the defualt application repository source list 
run yum -y install epel-release 
run yum -y install python-pip 
run yum clean all

# copy application source code to srcdir 
copy $dockyard_src $dockyard_srcproj

# create application subdirectories 
workdir $dockyard_srcproj 
run mkdir log 
volume ['$dockyard_srcproj/log/']

# install python dependencies 
run pip install --upgrade pip
run pip install -r $dockyard_srcproj/requirements/common.txt

# port to expose 
expose 8000

# copy entrypoint script into the image 
workdir $dockyard_srcproj

4. build鏡像

# 在dockerfile同級(jí)目錄下
sudo docker build -t jason/webdemo .

成功后顯示successfully,同時(shí)返回鏡像id,如圖:

怎么運(yùn)用docker配置python開發(fā)環(huán)境

5.查看并啟動(dòng)鏡像

注意,此處-p映射的端口(主機(jī)端口:容器端口),容器端口應(yīng)與flask應(yīng)用定義的端口一致

sudo docker images
sudo docker run -it --name webdemo -p 8000:8000 jason/webdemo /bin/bash

 6.在容器中啟動(dòng)flask應(yīng)用

python manage.py runserver -p 8000

啟動(dòng)成功如下截圖:

怎么運(yùn)用docker配置python開發(fā)環(huán)境

7.在瀏覽器輸入127.0.0.1:8000即可訪問(wèn)應(yīng)用

怎么運(yùn)用docker配置python開發(fā)環(huán)境

8.后續(xù)

如果容器關(guān)閉后需要再次進(jìn)入,可以用如下命令

# 查看當(dāng)前容器 
sudo docker ps -a
# 啟動(dòng)容器
docker start webdemo
# 進(jìn)入容器
docker attach webdemo

讀到這里,這篇“怎么運(yùn)用docker配置python開發(fā)環(huán)境”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI