本文内容从docker官方文档摘抄,仅介绍基础概念,将与swarm相关的service、stack都没载入
Docker基础
基本概念
-
image 与 container
A container is launched by running an image. An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.
容器就是跑镜像的,镜像包括了程序运行的所有资源,包括代码,运行时,lib,环境变量,配置文件等
-
container 与 docker

容器跑在docker上,每个容器之间相互独立。docker运行在host os之上


基本操作
-
docker帮助
docker
docker COMMAND --help
docker container --help
-
docker版本
docker --version
-
docker的信息
docker info
列出docker的基本信息,包括Container状态,image个数等
-
运行image
docker run hello-world
-
列出image
docker image ls
-
列出container
docker container ls # running
docker container ls --all # all
docker container ls -aq # all in quiet mode
-
列出云节点[对于swarms]
docker node ls
-
列出所有服务[对于swarms]
docker service ls
-
查看某个服务具体信息[对于swarms]
docker service ps 服务id
containers
-
层级结构
Container(2018-8-28docker基础/底层)
Services: 定义container的行为[defines how containers behave in production]
Stack: 定义service的相互作用[defines the interacions of all the services]
-
Dockerfile
- 映射容器内的资源与容器外的,如端口,磁盘等
- 指明需要从外部环境中copy到容器的文件
创建一空目录,然后在里边创建一Dockerfile格式如下:
# 引用一个官方的python镜像 Use an official Python runtime as a parent image
FROM python:2.7-slim
# 设置工作目录
WORKDIR /app
# 将当前目录copy到容器的/app下
ADD . /app
# 在容器内安装需要的包 Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# 容器内暴露的虚拟端口80
EXPOSE 80
# 环境变量
ENV NAME World
# 启动容器时运行命令
CMD ["python", "app.py"]
Flask
Redis
app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(2018-8-28docker基础/host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(2018-8-28docker基础/__name__)
@app.route(2018-8-28docker基础/"/")
def hello(2018-8-28docker基础/):
try:
visits = redis.incr(2018-8-28docker基础/"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(2018-8-28docker基础/name=os.getenv(2018-8-28docker基础/"NAME", "world"), hostname=socket.gethostname(2018-8-28docker基础/), visits=visits)
if __name__ == "__main__":
app.run(2018-8-28docker基础/host='0.0.0.0', port=80)
-
构建app镜像
在同目录下执行:
docker build -t firendlshello
-
查看镜像
docker image ls
-
运行镜像
docker run -p 4000:80 friendlyhello
这样在外部需要用4000来访问,http://localhost:4000
-
查看运行中的container
docker container ls
-
停止container
docker container stop "CONTATINER ID"
-
分享镜像[暂时略过]
类似于github,有个dockerhub,可以通过docker push 来上传镜像