docker

1.docker建立镜像并且发布到仓库

2017-05-17  本文已影响120人  think_lonely

本文说的是建立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 (根据提示输入用户名密码)

三、发布景象:

给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


上一篇 下一篇

猜你喜欢

热点阅读