docker learn2:dockerfile

 

Dockerfile

说明了容器中应该进行什么的步骤,网络接口和硬件驱动在环境内部是虚拟化的,和系统中其余部分是独立的,所以须要和外部环境映射,html

拷贝文件时也必须指定要拷贝到容器具体文件,使用dockerfile 指望不管在哪配置都大致类似python

 

使用Dockerfile来定义一个容器

 

 

 

 

1.在特定目录下建立三个文件git

Dockerfilegithub

 

# Use an official Python runtime as a parent 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 --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"]redis

 

requirements.txtdocker

Flaskflask

Redis网络

 

app.pyapp

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)curl

 

2.查看当前目录中三个文件是否已经建立好

$ ls

Dockerfile app.py requirements.txt

 

3.建立docker image

docker build -t friendlyhello .

4.查看本地docker image

$ docker image ls

REPOSITORY TAG IMAGE ID

friendlyhello latest 326387cea398

 

5.运行app 使用-p 参数将机器端口4000映射到容器发布端口80

docker run -p 4000:80 friendlyhello

 

6.访问app

http://localhost:4000

 

 

使用ctrl+c能够中断app

7.将app运行改成后台运行docker run -d -p 4000:80 friendlyhello

 

8.使用命令$ curl http://localhost:4000

查看网页内容

 

9.查看正在运行的image:docker container ls

10.关闭正在运行的容器 docker container stop CID

 

 

分享制做的image

dockerhub和github相似,是一个镜像仓库,国外地址 hub.docker.com.,国内没法访问

使用国内网易云 hub.c.163.com

 

1.登陆网易云镜像仓库

docker login -u {登陆帐号} -p {你的网易云密码} hub.c.163.com

Attention:登陆帐号可前往网易云控台,点击右上角用户名- 基本信息里查看。

返回「Login Succeded」即为登陆成功。

2.标记本地镜像

docker tag {镜像名或ID} hub.c.163.com/{你的用户名}/{标签名}

example: docker tag friendlyhello hub.c.163.com/dog948453219/friendlyhello

 

3.你的网易云镜像仓库推送地址为 hub.c.163.com/{你的用户名}/{标签名}

Attention: 此处为你的用户名,不是你的邮箱账号或者手机号码 登陆网易云控制台,页面右上角头像右侧即为「用户名」

  • 推送至不存在的镜像仓库时,自动建立镜像仓库并保存新推送的镜像版本;
  • 推送至已存在的镜像仓库时,在该镜像仓库中保存新推送的版本,当版本号相同时覆盖原有镜像。

 

4.推送至网易云镜像仓库

docker push hub.c.163.com/{你的用户名}/{标签名}

example: docker push hub.c.163.com/dog948453219/friendlyhello

5.默认为私有镜像仓库,推送成功后便可在控制台的「镜像仓库」查看

相关文章
相关标签/搜索