入门-container-最底层
最底层是container
,第二层是service
,最高层是stack
。
前提
- dockers version:1.13或者更高(17.03.1-ce)
Dockerfile
定制container
dockerfile
定义了container
的环境里面的内容。在container
里面的资源(如网络接口和磁盘驱动)都是虚拟的,和物理机分离的,所以必须映射端口、指定要copy
的文件。创建一个空目录,创建一个
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"]
需要在container中运行的App
在
Dockerfile
的同级目录中添加两个文件:requirements.txt
和app.py
。【入门-container-最底层】
Dockerfile
中的ADD
命令将这两个文件存放在image
中,EXPOSE
命令可以让我们通过HTTP访问。requirements.txt
Flask
Redis
app.py
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 = "cannot connect to Redis, counter disabled"html = "Hello {name}!
" \
"Hostname: {hostname}
" \
"Visits: {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)
在一个构建这个appcontainer
中获取hostname
得到的是container ID
。
docker build -t friendlyhello .
运行app
docker run -p 4000:80 friendlyhellodocker run -d -p 4000:80 friendlyhellodocker psdocker stop 1fa4ab2cf395
分享image
A registry is a collection of repositories, and a repository is a collection of images.登陆docker id
docker login
给
image
打标签
本地image和一个registry
中的一个repository
建立联系的标记为username/repository:tag
。docker tag friendlyhello john/get-started:part1
发布image
docker push username/repository:tag
从远程仓库获取image
docker run -p 4000:80 username/repository:tag
在构建和运行image的时候,如果没有指定:tag
,将会使用:latest
。运行的时候如果没有tag则运行:latest
的版本(不见得是最新的版本)。
docker build -t friendlyname .# Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname# Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname# Same thing, but in detached mode
docker ps# See a list of all running containers
docker stop# Gracefully stop the specified container
docker ps -a# See a list of all containers, even the ones not running
docker kill# Force shutdown of the specified container
docker rm# Remove the specified container from this machine
docker rm $(docker ps -a -q)# Remove all containers from this machine
docker images -a# Show all images on this machine
docker rmi # Remove the specified image from this machine
docker rmi $(docker images -q)# Remove all images from this machine
docker login# Log in this CLI session using your Docker credentials
docker tag username/repository:tag# Tag for upload to registry
docker push username/repository:tag# Upload tagged image to registry
docker run username/repository:tag# Run image from a registry
推荐阅读
- typeScript入门基础介绍
- Android|Android sqlite3数据库入门系列
- Android下的IO库-Okio源码解析(一)|Android下的IO库-Okio源码解析(一) 入门
- 深度学习-入门
- 第三章|第三章 进校园重拾旧梦 登讲台初为人师第一节 接乱班面临考验 ,遇高师指点入门
- iOS开发技术之美—iOS入门技术的基础学习
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- 【入门】Python网络爬虫与信息提取1
- 垂直农场101-室内农业入门知识
- 所有Python入门书籍的整理,初学者必看,附赠所有电子版(三)