docker-项目应用

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

 

1,首先要安装docker,先确定docker是否正常运行

docker run hello-world

2,创建dockerfile文件夹,里面配置三个文件

dockerfile文件

# Use an official Python runtime as a parent image
FROM python:3.6-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 --trusted-host pypi.python.org -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"]
dockerfile
Flask
Redis
requirements.txt

再创建两个文件,requirements.txt然后app.py将它们放在同一个文件夹中Dockerfile这完成了我们的应用程序,您可以看到它非常简单。当上述Dockerfile被内置到的图像,app.py并且requirements.txt是因为存在DockerfileADD命令,并从输出app.py是通过HTTP得益于访问EXPOSE 命令。

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
app.py

3,cd 到dockerfile文件夹下

$ ls
Dockerfile        app.py            requirements.txt

现在运行build命令。这会创建一个Docker镜像,我们将使用-t来标记,创建一个image名称代表

docker build -t friendlyhello .     #ps:别忘了最后的点
$ docker image ls   #查看所有的image

REPOSITORY            TAG                 IMAGE ID
friendlyhello         latest              326387cea398

针对Linux用户的故障排除

代理服务器在启动并运行后可以阻止与您的网络应用程序的连接。如果您位于代理服务器的后面,

请使用以下ENV命令为代理服务器指定主机和端口,将以下行添加到Dockerfile中

# Set proxy server, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port

代理服务器设置

DNS错误配置可能会产生问题pip您需要设置您自己的DNS服务器地址才能pip正常工作。您可能需要更改Docker守护程序的DNS设置。您可以/etc/docker/daemon.json使用dns密钥编辑(或创建)配置文件,如下所示:

{
  "dns": ["your_dns_address", "8.8.8.8"]
}

在上面的例子中,列表的第一个元素是你的DNS服务器的地址。第二项是Google的DNS,当第一项不可用时可以使用它。

在继续之前,保存daemon.json并重新启动docker服务。

sudo service docker restart

一旦修复,重试运行build命令。

4,运行应用程序

运行应用程序,使用以下命令将计算机的端口4000映射到容器的已发布端口80 -p

docker run -p 4000:80 friendlyhello

您应该看到Python正在为您的应用提供服务的消息http://0.0.0.0:80但是该消息来自容器内部,它不知道你将该容器的端口80映射到4000,从而制作正确的URL http://localhost:4000

现在让我们以分离模式在后台运行应用程序:

docker run -d -p 4000:80 friendlyhello
$ docker container ls  #查看所有的容器
CONTAINER ID        IMAGE               COMMAND             CREATED
1fa4ab2cf395        friendlyhello       "python app.py"     28 seconds ago

 

现在docker container stop用来结束这个过程,使用CONTAINER ID如下所示:

docker container stop 1fa4ab2cf395

 

5,分享你的image

使用您的Docker ID登录

如果您没有Docker帐户,请在cloud.docker.com注册一个帐户 记下你的用户名。

登录到本地计算机上的Docker公共注册表。

$ docker login      #登录你的自己的docker注册表

把本地的image放在自己私人的docker注册表中

username/repository:tag     #username :用户名   repository:tag    存储库:标签(可以不写,但是写上可以区分)

 

 例如:

docker tag friendlyhello john/get-started:part2

 

 查看新的 docker image ls

$ docker image ls

REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
friendlyhello            latest              d9e555c53008        3 minutes ago       195MB
john/get-started         part2               d9e555c53008        3 minutes ago       195MB
python                   2.7-slim            1c7128a655f6        5 days ago          183MB
...

 

 

6,上传新的图像

将您的标记图像上传到存储库:
docker push username/repository:tag     #docker push john/get-started

 

 完成后,此上传的结果将公开发布。如果您登录到Docker Hub,则可以通过其pull命令在那里看到新映像

7,从远程存储库中提取并运行图像

从现在开始,您可以使用docker run此命令在任何机器上使用并运行您的应用程序:

docker run -p 4000:80 username/repository:tag

 

 如果图像在机器上本地不可用,则Docker将其从存储库中取出。

If the image isn’t available locally on the machine, Docker pulls it from the repository.

$ docker run -p 4000:80 john/get-started:part2
Unable to find image 'john/get-started:part2' locally
part2: Pulling from john/get-started
10a267c67f42: Already exists
f68a39a6a5e4: Already exists
9beaffc0cf19: Already exists
3c1fe835fb6b: Already exists
4c9f1fa8fcb8: Already exists
ee7d8f576a14: Already exists
fbccdcced46e: Already exists
Digest: sha256:0601c866aab2adcc6498200efd0f754037e909e5fd42069adeff72d1e2439068
Status: Downloaded newer image for john/get-started:part2
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

 

 无论在哪里docker run执行,它都会将您的图像,Python以及所有依赖项从中拉出requirements.txt

然后运行您的代码。它们都在一个整洁的小包中一起旅行,并且您不需要在主机上安装任何Docker来运行它。

 

posted @ 2018-05-30 12:19  forjie  阅读(128)  评论(0)    收藏  举报