最近忙于工作,荒废了博客,在此反省一波~~好了,今天聊聊docker。docker作为一种快速部署生产环境的工具,利用了其自动化的优点,减少在部署中人为繁琐操作,从而提高了部署环境的速度以及准确度,在部署服务器集群中尤为便利,今天就简单入门一下docker吧!

Docker基本概念

入门Docker的初心者来说肯定要面对这三个名词,镜像(Image),容器(Container),仓库(Repository)。

镜像就好比我们装系统时的镜像,包含着原始系统,镜像一般要求比较干净,比如一个nginx镜像,一个php-fpm容器这样子,负责不同共功能的要将镜像划分开。容器则是我们实际的操作环境,容器是基于镜像构建,容器是镜像的示例,就如对象是类的实例一样。而仓库的话是用来存放镜像的,仓库有分私人仓库与公有仓库,公有仓库中有许多官方镜像,比如redis,nginx等等。

常用指令

镜像类 Image

1
docker pull <image>

用于从网络拉取镜像,比如想要拉取nginx镜像时,就打docker pull nginx,默认拉取最新版本,如需拉取其他版本,可使用docker pull nginx:xxxx

1
docker search 关键字

去网络仓库中查找镜像

1
docker images

用于列出本地已拉取的镜像

1
docker history 仓库:标签

显示某个仓库对于标签镜像的历史

1
docker rmi 仓库:标签

删除本地指定仓库标签的镜像

1
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

以某个镜像按照设置创建并运行一个容器

eg.

1
2
#将nginx镜像的一个容器起名为webserver,并把本机9999端口映射到docker容器的80端口
docker run --name webserver -d -p 9999:80 nginx

容器类 Container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#查看运行中的容器
docker ps
#查看所有容器,包括停止的
docker ps -a
# 查看全部容器的id
docker ps -a -q
#查看运行中的容器进程
docker top 容器名
#删除一个容器
docker rm [容器id]
#删除所有容器
docker rm `docker ps -a -q`
#将一个容器保存为镜像
docker commit <CONTAIN-ID> <IMAGE-NAME>
#启动一个已经存在的镜像
docker start 容器id

Docker run

1
docker run -it(命令后交互) --rm(停止后删除) -p 0.0.0.0:8080:9999(端口映射) -v /宿主目录:/容器目录 xxx:xxx(镜像) bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-a, --attach=[] Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
--add-host=[] Add a custom host-to-IP mapping (host:ip)
--blkio-weight=0 Block IO (relative weight), between 10 and 1000
-c, --cpu-shares=0 CPU shares (relative weight)
--cap-add=[] Add Linux capabilities
--cap-drop=[] Drop Linux capabilities
--cgroup-parent= Optional parent cgroup for the container
--cidfile= Write the container ID to the file
--cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota=0 Limit the CPU CFS quota
--cpuset-cpus= CPUs in which to allow execution (0-3, 0,1) 绑定容器到指定CPU运行
--cpuset-mems= MEMs in which to allow execution (0-3, 0,1) 绑定容器到指定MEM运行
-d, --detach=false Run container in background and print container ID 后台运行容器,并返回容器ID
--device=[] Add a host device to the container
--dns=[] Set custom DNS servers 指定容器使用的DNS服务器,默认和宿主一致
--dns-search=[] Set custom DNS search domains 指定容器DNS搜索域名,默认和宿主一致
-e, --env=[] Set environment variables 设置环境变量
--entrypoint= Overwrite the default ENTRYPOINT of the image
--env-file=[] Read in a file of environment variables 从指定文件读入环境变量
--expose=[] Expose a port or a range of ports
-h, --hostname= Container host name 指定容器的hostname
--help=false Print usage
-i, --interactive=false Keep STDIN open even if not attached 以交互模式运行容器,通常与 -t 同时使用
--ipc= IPC namespace to use
-l, --label=[] Set meta data on a container
--label-file=[] Read in a line delimited file of labels
--link=[] Add link to another container
--log-driver= Logging driver for container
--log-opt=[] Log driver options
--lxc-conf=[] Add custom lxc options
-m, --memory= Memory limit
--mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-swap= Total memory (memory + swap), '-1' to disable swap
--name= Assign a name to the container 为容器指定一个名称
--net=bridge Set the Network mode for the container 指定容器的网络连接类型,支持 bridge/host/none/container:<name|id> 四种类型
--oom-kill-disable=false Disable OOM Killer
-P, --publish-all=false Publish all exposed ports to random ports
-p, --publish=[] Publish a container's port(s) to the host 端口映射 宿主:容器
--pid= PID namespace to use
--privileged=false Give extended privileges to this container
--read-only=false Mount the container's root filesystem as read only
--restart=no Restart policy to apply when a container exits
--rm=false Automatically remove the container when it exits
--security-opt=[] Security Options
--sig-proxy=true Proxy received signals to the process
-t, --tty=false Allocate a pseudo-TTY 为容器重新分配一个伪输入终端,通常与 -i 同时使用
-u, --user= Username or UID (format: <name|uid>[:<group|gid>])
--ulimit=[] Ulimit options
--uts= UTS namespace to use
-v, --volume=[] Bind mount a volume 挂载宿主文件夹 宿主:容器
--volumes-from=[] Mount volumes from the specified container(s)
-w, --workdir= Working directory inside the container