本文首發于Gevin的博客
最近docker發布了Mac版本和Windows版本,使開發者用起來更方便簡單了。Docker本來就是虛擬化技術,基于Docker來構建開發環境順理成章。Gevin這兩天也整理了構建開發環境的思路,在Mac下試驗了一下,整體效果還是滿意的。今天以django開發環境的構建為例,把構建思路記錄下來,和大家分享一下。
一、基本思路
1. 創建一個用于開發Django App的目錄
mkdir django-example && cd django-example
2. 構建基本開發環境
touch Dockerfile
touch pip.conf requirements.txt
pip.conf
文件填入以下內容,以便一會用pip安裝Python 模塊時使用阿里云鏡像加速:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
requirements.txt
文件中填入要安裝的Python 模塊:
django
編寫構建開發環境的Dockerfile文件,填入以下內容:
# MAINTAINER Gevin <flyhigher139@gmail.com>
# DOCKER-VERSION 1.12.0
#
# Dockerizing Python: Dockerfile for building python applications
FROM python:2.7.12
MAINTAINER Gevin <flyhigher139@gmail.com>
WORKDIR /usr/src/app
# 使用阿里云的pip鏡像
COPY pip.conf /root/.pip/pip.conf
COPY requirements.txt /usr/src/app/requirements.txt
RUN pip install -r /usr/src/app/requirements.txt
EXPOSE 8000
CMD ["bash"]
然后執行下面命令構建鏡像:
docker build -t gevin/django-example:0.1 .
構建成功后,執行docker images
命令,可以查看到當前構建好的image
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gevin/django-example 0.1 1855fc3c8062 12 hours ago 698.9 MB
3. 使用構建的image拉起開發環境
執行下面命令,可以以前臺形式拉起django-example鏡像的一個container:
docker run -it --rm -v $(pwd):/usr/src/app gevin/django-example:0.1
上面命令使用了data volume
,把當前目錄掛載到container中的工作目錄下,這樣當前目錄下的所有文件都會映射到container的工作目錄下,在工作目錄下的所有改動,也都會保存到宿主機的當前目錄下。
4. 創建django項目
上一步的命令創建了一個安裝了django的交互式的container,直接在該container中執行創建django項目的命令即可:
root@7c91f460599f:/usr/src/app# django-admin startproject dj_example
上述命令,在container中基于django的命令創建了一個django項目,由于上一步操作時把宿主機的當前目錄掛載到container的工作目錄下,因此,剛剛在container中創建的django項目,在宿主機上也能看到。
container:
root@7c91f460599f:/usr/src/app# ls
Dockerfile dj_example pip.conf requirements.txt
宿主機:
django-example ls
Dockerfile dj_example pip.conf requirements.txt
5. 啟動django項目
docker run -it --rm -p 8000:8000 -v $(pwd):/usr/src/app gevin/django-example:0.1 python dj_example/manage.py runserver 0.0.0.0:8000
二、Docker-compose與Django環境的結合
每次使用上面章節中介紹的冗長命令來使用django環境非常麻煩,docker-compose
可以簡化操作。
首先在當前目錄下創建docker-compose.yml
文件:
? django-example touch docker-compose.yml
然后在該文件中寫入如下內容:
version: '2'
services:
django-example:
image: gevin/django-example:0.1
volumes:
- ./dj_example:/usr/src/app
ports:
- 8000:8000
command: python manage.py runserver 0.0.0.0:8000
執行下面命令即可拉起django服務:
? django-example docker-compose up
# Starting djangoexample_django-example_1
# Attaching to djangoexample_django-example_1
在瀏覽器中訪問http://localhost:8000
,即可看到默認的django頁面
注:
上面的docker-compose文件,把
./dj_example
目錄掛載到/usr/src/app
,免去執行django命令時,需要對應到下級目錄的麻煩,但這樣隱藏了原來container中的requirements.txt
文件,需要注意。
基于docker-compose 執行django 命令
使用docker-compose 的 run
命令,可以在容器內執行相應操作,如:
對django服務的數據庫做migrate
操作:
docker-compose run django-example python manage.py migrate
創建超級用戶:
docker-compose run django-example python manage.py createsuperuser
# Username (leave blank to use 'root'): gevin
# Email address:
# Password:
# Password (again):
# Superuser created successfully.
創建成功后,訪問http://localhost:8000/admin
,即可使用剛創建的用戶(即gevin),登錄數據庫管理頁面
由于使用了數據卷,保存在sqlite數據庫中的數據會一直有效。
三、簡化方案
上面方案已經成功構建了django 環境,并應用于開發。上面的方案主要是為了闡述實現思路,在實際操作起來至少有兩個麻煩:(1)需要進入容器里面創建django項目;(2)由于django項目是建立在當前目錄的子目錄下,使用docker-compose 時為了命令簡單通用,更換了數據卷。
在實踐中,利用docker-compose的run
命令,沒必要進入容器創建django項目;只要把django項目建立在當前目錄下,也沒必要更換數據卷了。
因此,可以把上面的方案再理一下,按下面步驟構建開發環境,并應用到開發中去。
Outline:
1. <a name="create-dockefile"></a>創建Dockerfile文件
對Python開發環境而言,最好再創建pip.conf和requirements.txt文件,以便方便安裝項目必須的Python依賴,其他語言的開發環境就具體情況而定。
以Python為例,Dockerfile 內容如下:
# MAINTAINER Gevin <flyhigher139@gmail.com>
# DOCKER-VERSION 1.12.0
#
# Dockerizing Python: Dockerfile for building python applications
FROM python:2.7.12
MAINTAINER Gevin <flyhigher139@gmail.com>
WORKDIR /usr/src/app
# 使用阿里云的pip鏡像
COPY pip.conf /root/.pip/pip.conf
COPY requirements.txt /usr/src/app/requirements.txt
RUN pip install -r /usr/src/app/requirements.txt
EXPOSE 8000
CMD ["bash"]
2. <a name="build-image"></a> 構建鏡像
docker build -t gevin/django-example:0.1 .
3. <a name="create-docker-compose"></a> 創建docker-compose文件
docker-compose文件內容如下:
version: '2'
services:
django-example:
image: gevin/django-example:0.1
volumes:
- .:/usr/src/app
ports:
- 8000:8000
command: python manage.py runserver 0.0.0.0:8000
4. <a name="create-project"></a> 創建項目
docker-compose run django-example django-admin startproject dj_project .
果然需要migrate數據庫,創建超級用戶等,可以在這里一并創建,也可以在后面的開發中再創建:
docker-compose run django-example python manage.py migrate
docker-compose run django-example python manage.py createsuperuser
5. <a name="run-project"></a> 拉起項目
docker-compose up
四、其他
Gevin認為,雖然基于docker可以構建開發環境,但還是vagrant用起來更舒服,docker更加適合做CI,測試和部署。
實際工作中如何使用docker,就仁者見仁,智者見智了。