本文說(shuō)的是建立dockerapp的基本步聚,前提docker已經(jīng)安裝成功
1.The app itself(建立應(yīng)用)
建立一個(gè)文件夾,在文件夾下邊建立三個(gè)文件,如下所示:
$ls
Dockerfile ? ?app.py ? requirements.txt
三個(gè)文件的具體內(nèi)容如下:
Dockerfile
# Use an official Python runtime as a base image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
requirements.txt
Flask
Redis
app.py
通過(guò)pip安裝requirements.txt中的依賴 redis flask
pip install -r requirements.txt?
?
2.Build the App
$lsDockerfileapp.pyrequirements.txt ?
#建立應(yīng)用
docker build -t friendlyhello . ? 建立名為friendlyhello的應(yīng)用
查看建立的應(yīng)用
$docker images
REPOSITORY? ? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID
friendlyhello? ? ? ? latest? ? ? ? ? ? ? 326387cea398
3.Run the app ? #運(yùn)行app
Run the app, mapping your machine’s port 4000 to the container’sEXPOSEd port 80 using-p:
docker run -p 4000:80 friendlyhello ? #在前臺(tái)運(yùn)行
Now let’s run the app in the background, in detached mode:
docker run -d -p 4000:80 friendlyhello ?#后臺(tái)運(yùn)行
4.查看docker運(yùn)行的應(yīng)用
$docker ps
通過(guò) http://localhost:4000訪問(wèn)應(yīng)用.
5.#通過(guò)containet Id 來(lái)停止app
Now use docker stop to end the process, using the CONTAINER ID, like so:
docker stop e36ae6aaad8f
6.Share your image(分享鏡像)
一、If you don’t have a Docker account, sign up for one at?cloud.docker.com. Make note of your ? username.(如果沒(méi)有docker帳號(hào)注冊(cè)一個(gè))
二、Log in your local machine.(輸入下邊命名登錄)
? docker login (根據(jù)提示輸入用戶名密碼)
三、發(fā)布景象:
給app打標(biāo)記
docker tag friendlyhello username/repository:tag
friendlyhello #為app的名稱
username:你注冊(cè)的docker的名字
repository:你建立的docker鏡像的倉(cāng)庫(kù)
tag:應(yīng)用打一個(gè)標(biāo)記號(hào),版本的變遷
Upload your tagged image:(上傳鏡像)
docker push username/repository:tag
上傳后可以在你的docker看到剛才上傳的應(yīng)用鏡像,這個(gè)是公開(kāi)可用的
7.使用你放在docker 倉(cāng)庫(kù)的鏡像
Once complete, the results of this upload are publicly available. From now on, you can use docker run and run your app on any machine with this command:
運(yùn)行命令如下:
docker run -p 4000:80 username/repository:tag