Docker学习笔记(一):容器和镜像

Docker配置阿里云镜像加速

#第一步
sudo mkdir -p /etc/docker
#第二步
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://7mp81chq.mirror.aliyuncs.com"]
}
EOF
#第三步
sudo systemctl daemon-reload
#第四步
sudo systemctl restart docker

Docker执行过程

[root@jun bin]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@jun bin]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

执行时先查找有无镜像,没有镜像则下载,镜像不存在则无法运行

底层原理

docker为什么比VM快?

  • 因为Docker有比虚拟机更少的抽象层.
  • docker利用的是宿主机的内核,不需要像虚拟机一样重新加载一个操作系统内核,VM则需要Guest OS

Docker的常用命令

官方文档https://docs.docker.com/reference/

docker version #显示Docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令

镜像命令

docker images #查看已有镜像
docker images -a #列出所有镜像
docker images -q #只显示镜像的id

[root@jun /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

[root@jun /]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

[root@jun /]# docker images -q
d1165f221234

docker search #搜索镜像

[root@jun /]# docker search --help

Usage:  docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
[root@jun /]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11042     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4186      [OK]   

docker pull 镜像下载

[root@jun /]# docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output
[root@jun /]# docker pull mysql
Using default tag: latest             # 如果不指定版本,则下载最新的版本
latest: Pulling from library/mysql
b4d181a07f80: Pull complete       # 分层下载,docker image的核心 联合文件系统
a462b60610f5: Pull complete 
578fafb77ab8: Pull complete 
524046006037: Pull complete 
d0cbe54c8855: Pull complete 
aa18e05cc46d: Pull complete 
32ca814c833f: Pull complete 
9ecc8abdb7f5: Pull complete 
ad042b682e0f: Pull complete 
71d327c6bb78: Pull complete 
165d1d10a3fa: Pull complete 
2f40c47d0626: Pull complete 
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

docker rmi 删除镜像

[root@jun /]# docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

容器命令

只有镜像存在才能创建容器,下载一个centos来测试学习

# 启动并进入容器
[root@jun /]# docker run -it centos /bin/bash       使用交互方式运行并进入容器查看内容
[root@f635888533e0 /]# ls      查看容器内部的centos
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器中退回主机
[root@f635888533e0 /]# exit
exit
[root@jun /]# ls
bin   dev  home  lib64       media  opt   root  sbin  sys  usr
boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var

docker ps 列出所有运行的容器

  • -a 列出正在运行以及创建过的容器
  • -a -n=1 显示最近的一个容器
  • -aq 显示所有容器的编号
# 列出所有运行的容器
[root@jun /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@jun /]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                      PORTS     NAMES
f635888533e0   centos        "/bin/bash"   4 minutes ago   Exited (0) 56 seconds ago             competent_galileo
0809555ab0f6   hello-world   "/hello"      5 hours ago     Exited (0) 5 hours ago                fervent_pasteur

退出容器

  • exit # 容器停止并直接退出
  • Crtl + P + Q # 容器不停止退出

删除容器

  • docker rm [容器id] 删除指定的容器,不能删除正在运行的容器
  • docker rm -f $(docker ps -aq) 删除所有的内容
  • docker ps -a -q|xargs docker rm 删除所有的容器

启动和停止容器

  • docker start 容器id 启动容器
  • docker restart 容器id 重启容器
  • docker stop 容器id 停止当前正在运行的容器
  • docker kill 容器id 强制停止当前正在运行的容器

常用其他命令

后台启动容器

  • docker run -d 容器名
  • 常见的坑:docker容器使用后台运行,就必须要有一个其它进程,docker发现没有应用,就会自动停止

查看日志

  • docker logs -tf --tail 10 容器id
[root@jun /]# docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42
                       minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for
                       42 minutes)

查看容器的元数据

  • docker inspects 容器Id
[root@jun /]# docker inspect --help

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type

进入正在运行的容器

  • docker exec -it 容器id bashshell
  • docker attach 容器id

docker exec # 进入容器后开启一个新的终端,可以在里面操作
docker attach # 进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上面

  • docker cp 容器id:容器内路径 目的主机路径
# 查看主机目录下的文件
[root@jun home]# touch hello.go
[root@jun home]# ls
hello.go  jun  Tim
[root@jun home]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS        PORTS     NAMES
39937021aebd   centos    "/bin/bash"   11 hours ago   Up 11 hours             confident_hoover
25e914d28107   centos    "/bin/bash"   11 hours ago   Up 11 hours             happy_carver

# 进入docker 容器内部
[root@jun home]# docker attach 25e914d28107
[root@25e914d28107 /]# cd /home
[root@25e914d28107 home]# ls

# 在容器内新建文件
[root@25e914d28107 home]# touch test.go
[root@25e914d28107 home]# exit
exit
[root@jun home]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS        PORTS     NAMES
39937021aebd   centos    "/bin/bash"   11 hours ago   Up 11 hours             confident_hoover
[root@jun home]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED        STATUS                      PORTS     NAMES
39937021aebd   centos        "/bin/bash"   11 hours ago   Up 11 hours                           confident_hoover
25e914d28107   centos        "/bin/bash"   11 hours ago   Exited (0) 18 seconds ago             happy_carver
f635888533e0   centos        "/bin/bash"   17 hours ago   Exited (0) 17 hours ago               competent_galileo
0809555ab0f6   hello-world   "/hello"      22 hours ago   Exited (0) 22 hours ago               fervent_pasteur

# 将这个文件拷贝出来到主机上
[root@jun home]# docker cp 25e914d28107:/home/test.go /home
[root@jun home]# ls
hello.go  jun  test.go  Tim
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容