什么是Docker?我个人理解:轻量化虚拟,属于Liunx容器的一种封装,将程序和依赖放入Docker容器中运行,虚拟环境,隔离跑。  

Docker的安装

举个常见的例子:mac系统下使用homebrew进行安装

只需要输入:~$ brew cask install docker 就可以了,效果如下。

$ brew cask install docker

==> Creating Caskroom at /usr/local/Caskroom
==> We'll set permissions properly so we won't need sudo in the future
Password:          # 输入 macOS 密码
==> Satisfying dependencies
==> Downloading https://download.docker.com/mac/stable/21090/Docker.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask docker
==> Installing Cask docker
==> Moving App 'Docker.app' to '/Applications/Docker.app'.
🍺  docker was successfully installed!

个人看来Docke核心有三部分:镜像(images)、容器(container)、仓库(registry)。

三者之间的关系:复制镜像可生成Docker容器,同一镜像Docker容器相同,仓库中可存放多个镜像,根据需求pull。

镜像可自定义(dockerfile:用于生成自定义镜像 ,可自行深入了解一下)

Docker安装完之后可配置下镜像加速,我配置的是网易的:找到鲸鱼小图标,点击选择preference->Daemon->registry mirrors中配置镜像加速路径:http://hub-mirror.c.163.com->Apply&Restart就可以了

Docker客户端:

~$ docker
~$ docker help
~$ docker stats --help

Usage:	docker stats [OPTIONS] [CONTAINER...]

Display a live stream of container(s) resource usage statistics

Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

常见的命令:

命令

作用

~$ docker version或~$ docker info

查看版本或信息

~$ docker run ubuntu:15.10 /bin/echo "hi,baby" 

以ubuntu15.10为镜像创建docker容器,然后在该容器中run,输出“hi,baby”

~$ docker run -i -t ubuntu:15.10 /bin/bash

root@48f72bd6af73:/# cat /proc/versio

运行容器交互

~$docker stop 容器Id

终止容器

~$ docker stop 容器名

终止容器

  • ubuntu:15.10指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub下载公共镜像。
  • -t:在新容器内指定一个伪终端或终端。-i:允许你对容器内的标准输入 (STDIN) 进行交互。
  • root@48f72bd6af73:/# cat /proc/version(在容器中运行命令 cat /proc/version和ls分别查看当前系统的版本信息和当前目录下的文件列表)注意:cat后面加空格,不然会出现No such file or directory)

一个web测试:

~$ docker pull training/webapp (载入镜像)

docker容器中运行一个 Python Flask 应用然后运行一个web应用:
parkin@parkindeMacBook-Air:~$ docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
e190868d63f8: Downloading 
909cd34c6fd7: Downloading 
0b9bfabab7c1: Downloading 
a3ed95caeb02: Downloading 
10bbbc0fc0ff: Downloading 
fca59b508e9f: Downloading 
e7ae2541b15b: Downloading 
9dd97ef58ce9: Downloading 
a4c1b0cb7af7: Downloading 
latest: Pulling from training/webapp
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
10bbbc0fc0ff: Pull complete 
fca59b508e9f: Pull complete 
e7ae2541b15b: Pull complete 
9dd97ef58ce9: Pull complete 
a4c1b0cb7af7: Pull complete 
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest

~$ docker run -d -P training/webapp python app.py

注意:-P必须是大写,小写不对,-d:让容器在后台运行。-P:将容器内部使用的网络端口映射到我们使用的主机上。)

错误的:(-p小写)
parkin@parkindeMacBook-Air:~$ docker run -d -p training/webapp python app.py
docker: invalid publish opts format (should be name=value but got 'training/webapp').
See 'docker run --help'.
正确的:(-P大写)
parkin@parkindeMacBook-Air:~$ docker run -d -P training/webapp python app.py 7360aa21c5e4076abc82b6858d4433ff87ff296452b5ef602fe8c22b78cf896
parkin@parkindeMacBook-Air:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
17360aa21c5e        training/webapp     "python app.py"     18 seconds ago      Up 16 seconds       0.0.0.0:32768->5000/tcp   ecstatic_mccarthy

~$ docker ps (查看正运行的容器)

Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机端口 32768 上,访问127.0.0.1:32768会显示“Hello world!”。

parkin@parkindeMacBook-Air:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
17360aa21c5e        training/webapp     "python app.py"     18 seconds ago      Up 16 seconds       0.0.0.0:32768->5000/tcp   ecstatic_mccarthy

~$ docker ps -l (查看最后一次创建的容器)

parkin@parkindeMacBook-Air:~$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
cae0bfc03bfe        training/webapp     "python app.py"     21 seconds ago      Up 19 seconds       0.0.0.0:7777->5000/tcp   goofy_elgamal

~$ docker run -d -p 5000:5000 training/webapp python app.py (修改端口)

注意:修改端口时 -p是小写,再次强调小写。

parkin@parkindeMacBook-Air:~$  docker run -d -p 7777:5000 training/webapp python app.py
cae0bfc03bfe972c31fddeee5f7dcfdb3e883bbb3c8ec31a01e61cb8819af340
parkin@parkindeMacBook-Air:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
cae0bfc03bfe        training/webapp     "python app.py"     9 seconds ago       Up 8 seconds        0.0.0.0:7777->5000/tcp    goofy_elgamal
edcc4ba9f383        training/webapp     "python app.py"     31 minutes ago      Up 21 minutes       0.0.0.0:5000->5000/tcp    admiring_germain
17360aa21c5e        training/webapp     "python app.py"     39 minutes ago      Up 39 minutes       0.0.0.0:32768->5000/tcp   ecstatic_mccarthy

~$ docker port 容器id或者名字(web应用容器ID或名字查看容器端口映射)

parkin@parkindeMacBook-Air:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
edcc4ba9f383        training/webapp     "python app.py"     14 seconds ago      Up 13 seconds       0.0.0.0:5000->5000/tcp    admiring_germain
17360aa21c5e        training/webapp     "python app.py"     8 minutes ago       Up 8 minutes        0.0.0.0:32768->5000/tcp   ecstatic_mccarthy
parkin@parkindeMacBook-Air:~$ docker port edcc4ba9f383 
5000/tcp -> 0.0.0.0:5000
parkin@parkindeMacBook-Air:~$ docker port admiring_germain
5000/tcp -> 0.0.0.0:5000
parkin@parkindeMacBook-Air:~$ docker port 17360aa21c5e  
5000/tcp -> 0.0.0.0:32768

~$ docker logs -f id或者名字(查看web应用程序日志)

-f: 让 docker logs 输出容器内部的标准。应用程序使用的是 5000 端口和应用程序的访问日志。

parkin@parkindeMacBook-Air:~$ docker logs -f 17360aa21c5e  
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [14/Mar/2019 05:23:59] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:23:59] "GET /favicon.ico HTTP/1.1" 404 -
172.17.0.1 - - [14/Mar/2019 05:30:27] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:30:28] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:30:28] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:30:28] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:30:29] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:30:29] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [14/Mar/2019 05:30:49] "GET / HTTP/1.1" 200 -

^C

~$ docker top 名字(查看web应用程序进程)

parkin@parkindeMacBook-Air:~$ docker top admiring_germain
PID                 USER                TIME                COMMAND
3602                root                0:00                python app.py
parkin@parkindeMacBook-Air:~$ docker inspect

~$ docker inspect 名字(docker inspect查看docker底层信息)

JSON 文件记录 Docker 容器的配置和状态信息
    {
        "Id": "edcc4ba9f383daac707a62d4f8bede9a6a818c99aa9639daa4db5492629e5c1d",
        "Created": "2019-03-14T05:29:54.4430641Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            ...,
        }
    }
]

~$ docker stop 名字(停止应用程序)

parkin@parkindeMacBook-Air:~$ docker stop admiring_germain
admiring_germain

~$ docker start 名字(重启应用程序)

parkin@parkindeMacBook-Air:~$ docker start admiring_germain
admiring_germain

~$ docker rm 名字(移除应用程序)

注意:移除容器的时候必须先停止容器,否则会报错。

未关闭:
parkin@parkindeMacBook-Air:~$ docker rm admiring_germain
Error response from daemon: You cannot remove a running container edcc4ba9f383daac707a62d4f8bede9a6a818c99aa9639daa4db5492629e5c1d. Stop the container before attempting removal or force remove
已关闭:
parkin@parkindeMacBook-Air:~$ docker stop admiring_germain
admiring_germain
parkin@parkindeMacBook-Air:~$ docker rm admiring_germain
admiring_germain
parkin@parkindeMacBook-Air:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
cae0bfc03bfe        training/webapp     "python app.py"     28 minutes ago      Up 28 minutes       0.0.0.0:7777->5000/tcp    goofy_elgamal
17360aa21c5e        training/webapp     "python app.py"     About an hour ago   Up About an hour    0.0.0.0:32768->5000/tcp   ecstatic_mccarthy