本文說的是建立dockerapp的基本步聚蔬崩,前提docker已經安裝成功
1.The app itself(建立應用)
建立一個文件夾敦锌,在文件夾下邊建立三個文件绿渣,如下所示:
$ls
Dockerfile ? ?app.py ? requirements.txt
三個文件的具體內容如下:
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
通過pip安裝requirements.txt中的依賴 redis flask
pip install -r requirements.txt?
?
2.Build the App
$lsDockerfileapp.pyrequirements.txt ?
#建立應用
docker build -t friendlyhello . ? 建立名為friendlyhello的應用
查看建立的應用
$docker images
REPOSITORY? ? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID
friendlyhello? ? ? ? latest? ? ? ? ? ? ? 326387cea398
3.Run the app ? #運行app
Run the app, mapping your machine’s port 4000 to the container’sEXPOSEd port 80 using-p:
docker run -p 4000:80 friendlyhello ? #在前臺運行
Now let’s run the app in the background, in detached mode:
docker run -d -p 4000:80 friendlyhello ?#后臺運行
4.查看docker運行的應用
$docker ps
通過 http://localhost:4000訪問應用.
5.#通過containet Id 來停止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.(如果沒有docker帳號注冊一個)
二胰苏、Log in your local machine.(輸入下邊命名登錄)
? docker login (根據(jù)提示輸入用戶名密碼)
三婚苹、發(fā)布景象:
給app打標記
docker tag friendlyhello username/repository:tag
friendlyhello #為app的名稱
username:你注冊的docker的名字
repository:你建立的docker鏡像的倉庫
tag:應用打一個標記號岸更,版本的變遷
Upload your tagged image:(上傳鏡像)
docker push username/repository:tag
上傳后可以在你的docker看到剛才上傳的應用鏡像,這個是公開可用的
7.使用你放在docker 倉庫的鏡像
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:
運行命令如下:
docker run -p 4000:80 username/repository:tag