基本信息
版本等信息
docker --version
更多的信息
docker info
docker version
测试docker
docker run hello-world
尝试运行一个Ubuntu容器
docker run -it ubuntu bash
这样会直接进入到容器的bash
就可以直接在里面操作,exit
可以出来
列出本机拥有的镜像
docker images
docker image ls
两个效果是一样的
列出所有的容器(正在运行的和停止运行的)
docker ps -a
docker container ls -a
docker container ls -aq
Dockerfile创建容器
案例
mkdir testdocker && cd testdocker && vim Dockerfile
# 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"]
vim requirements.txt
Flask
Redis
vim 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 = "<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)
docker build -t friendlyhello .
docker build
就是从一个Dockerfile
文件创建镜像-t
是指向一个名字和标签比如docker build -t testdocker:01 .
docker run -p 4000:80 friendlyhello
运行一个容器映射本地的4000端口到容器的80端口,打开浏览器访问http://127.0.0.1:4000/
出现Hello World!
docker run -d -p 4000:80 friendlyhello
加上-d
参数可以后台运行
docker container ls
列出正在运行的容器
docker container stop 1fa4ab2cf395
停止刚刚后台运行的容器
分享你的镜像
docker login
登陆Docker hub
docker tag imagename username/repository:tag
example:
docker tag friendlyhello john/get-started:part2
给本地的镜像一个tag
好可以上传到自己的仓库,可以docker image ls
查看自己的镜像。
docker push john/get-started:part2
上传有标签的镜像到自己的仓库,上传成功可以在仓库中查看。
docker run -p 4000:80 john/get-started:part2
直接运行刚刚上传自己仓库的镜像,这个时候就不需要安装或者上传什么文件了,直接把之前的有文件的镜像拿过来用就好了。
服务化
docker-compose.yml
文件主要是用来控制容器的
docker-compose.yml
文件
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repo:tag
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
大概内容就是产生5个容器,10%cpu,50M内存,挂掉自动重启,端口映射”80:80”,定义一个负载均衡网络。
docker-compose build
docker-compose up -d
docker-compose down
分别是创建、运行、关闭删除。其实我感觉到这里就差不多够用了,可以直接去hub
上面找一些官方的镜像练练手。
在使用docker stack deploy
之前先运行docker swarm init
docker swarm init
可能会提示你选择网卡接口
docker stack deploy -c docker-compose.yml getstartedlab
部署一个名字叫做getstartedlab
的app,部署了5个镜像实例。
docker stack rm getstartedlab
移除这个部署环境
docker swarm leave --force
强制离开这个swarm
官方Cheat sheet
## List Docker CLI commands
docker
docker container --help
## Display Docker version and info
docker --version
docker version
docker info
## Execute Docker image
docker run hello-world
## List Docker images
docker image ls
## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq
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
docker stack ls # List stacks or apps
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker service ls # List running services associated with an app
docker service ps <service> # List tasks associated with an app
docker inspect <task or container> # Inspect task or container
docker container ls -q # List container IDs
docker stack rm <appname> # Tear down an application
docker swarm leave --force # Take down a single node swarm from the manager
我在黑暗中,化作一个火种,想为你,点亮整片的星光,追随着微风,住进了美梦,你笑着,躲在我心中,不要烟火不要星光,只要问问内心的想法,在我的世界,可有个角落,是你不曾懂的远方。。。 ——来自华晨宇《微光》