Docker介绍

先打开Docker官网看看,官网给了最简单的介绍,构建 - 分享 - 运行!

【Docker学习笔记】Docker从入门到实战之基础篇_MySQL


Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的APP(可以是一个WEB应用或数据库应用等等)及其运行环境能够做到“一次镜像,处处运行”。

【Docker学习笔记】Docker从入门到实战之基础篇_centos_02

Linux容器技术的出现就解决了这样一个问题,而 Docker 就是在它的基础上发展过来的。将应用打成镜像,通过镜像成为运行在Docker容器上面的实例,而 Docker容器在任何操作系统上都是一致的,这就实现了跨平台、跨服务器。只需要一次配置好环境,换到别的机子上就可以一键部署好,大大简化了操作。


安装&启动&配置

安装 | 卸载

前提条件

目前,CentOS 仅发行版本中的内核支持 Docker。Docker 运行在CentOS 7 (64-bit)上,要求系统为64位、Linux系统内核版本为 3.8以上,这里选用Centos7.x为例。

官网安装指导文档

Install Docker Engine on CentOS 包含安装和卸载的所有步骤

下面是安装步骤总结

1、检查CentOS版本,命令方式。云服务器的话直接在控制台看

$ cat /etc/redhat-release

2、卸载旧版本(可跳过)

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

3、yum安装gcc相关

$ yum -y install gcc
$ yum -y install gcc-c++

4、安装需要的软件包

$ yum install -y yum-utils

5、设置stable镜像仓库

注意:镜像仓库使用阿里云的

$ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

6、更新yum软件包索引

$ yum makecache fast

7、安装Docker CE

$ yum -y install docker-ce docker-ce-cli containerd.io

8、启动Docker

$ systemctl start docker

9、测试

能正常输出信息,能运行helloworld即表示成功

$ docker version$ docker run hello-world

10、卸载步骤

$ systemctl stop docker
$ yumremove docker-ce docker-ce-cli containerd.io
$ rm -rf /var/lib/docker
$ rm -rf /var/lib/containerd


配置阿里云镜像加速器

  1. 阿里云Docker镜像仓库服务
  2. 阿里云镜像加速器

下载镜像时会从阿里云的docker hub镜像仓库下载,速度快!

按照提示,执行命令即可

【Docker学习笔记】Docker从入门到实战之基础篇_Docker_03


启动Docker - 运行hello-world

[root@VM-24-10-centos ~]# systemctl start docker
[root@VM-24-10-centos ~]# ps -ef | grep docker
root     27743     1  2 15:04 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root     27891 20762  0 15:04 pts/0    00:00:00 grep --color=auto docker
[root@VM-24-10-centos ~]# docker version
Client: Docker Engine - Community
 Version:           23.0.3
 API version:       1.42
 Go version:        go1.19.7
 Git commit:        3e7cbfd
 Built:             Tue Apr  4 22:04:18 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.3
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.7
  Git commit:       59118bf
  Built:            Tue Apr  4 22:02:01 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.20
  GitCommit:        2806fc1057397dbaeefbea0e4e17bddfbd388f38
 runc:
  Version:          1.1.5
  GitCommit:        v1.1.5-0-gf19387a
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@VM-24-10-centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:ffb13da98453e0f04d33a6eee5bb8e46ee50d08ebe17735fc0779d0349e889e9
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@VM-24-10-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   18 months ago   13.3kB
[root@VM-24-10-centos ~]#


常用命令

帮助启动类命令

查看docker版本信息: docker version

启动docker: systemctl start docker

停止docker: systemctl stop docker

重启docker: systemctl restart docker

开机启动: systemctl enable docker

查看docker运行状态: systemctl status docker

查看docker概要信息: docker info

查看docker总体帮助文档: docker --help

查看docker命令帮助文档: docker [具体命令] --help

镜像命令

  • docker images
  • docker search 某个***镜像名字
  • 搜索镜像
  • docker search --limit 5 redis
  • 查找redis镜像前5条
  • 查找默认返回25条记录,想要返回前5个镜像可带上--limit 5
  • docker pull 某个***镜像名称
  • 下载镜像
  • docker pull 镜像名称[:tag]
  • tag即版本号,不带则默认latest最新
  • docker system df 查看镜像/容器/数据卷所占的空间
  • docker rmi 某个镜像名字ID
  • 删除单个,如:docker rmi -f 7614ae9453d1
  • 删除多个,如:docker rmi -f 7614ae9453d1 feb5d9fea6a5
  • 删除全部,如:docker rmi -f $(docker images -aq)


容器命令

新建+启动容器

命令格式:docker run [OPTIONS] IMAGE [COMMAND] [ARGS…]

OPTIONS说明(常用):有些参数是一个短横线-,有些是两个短横线--

  • --name="容器名称" #为容器指定一个名称
  • -d #后台运行容器并返回容器ID,也即启动 守护式容器(后台运行)
  • -it #即启动交互式容器(前台有伪终端,等待交互)
  • -i #interactive,以交互模式运行容器,通常与-t同时使用
  • -t #tty,为容器重新分配一个伪输入终端,通常与-i同时使用
  • -P #随机端口映射,大写P
  • -p #指定端口映射,小写p

【Docker学习笔记】Docker从入门到实战之基础篇_MySQL_04


方式1:启动交互式容(前台客户端命令行)
[root@VM-24-10-centos /]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
ubuntu_image.tar   latest    c226b38cac88   26 seconds ago   72.8MB
redis              latest    7614ae9453d1   15 months ago    113MB
ubuntu             latest    ba6acccedd29   18 months ago    72.8MB
hello-world        latest    feb5d9fea6a5   18 months ago    13.3kB
[root@VM-24-10-centos /]# docker run -it ubuntu /bin/bash
root@72932ffc7aa2:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@72932ffc7aa2:/#

参数说明:

-i: 交互式操作。

-t: 终端。

ubuntu : ubuntu 镜像名称。

/bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。要退出终端,直接输入 exit


方式2:启动守护式容器(后台运行服务)
[root@VM-24-10-centos /]# docker run -d redis
605ea04b278d8a96b69143434df2fe7af858d0d19200397066a61620223248ef
[root@VM-24-10-centos /]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
605ea04b278d   redis          "docker-entrypoint.s…"   9 seconds ago    Up 9 seconds    6379/tcp   frosty_chebyshev

参数说明:
-d #指定容器的后台运行模式

补充:交互式容器 和 守护式容器 对比
交互式容器启动方式,相当于直接启动一个命令窗口,并且可以指定交互方式如/bin/bash守护式同期启动方式,可以理解为启动一个后台服务进程
特别说明:
比如执行docker run -d ubuntu启动守护式容器,然后docker ps -a进行查看,会发现容器已经退出,很重要的要说明的一点:Docker容器后台运行,就必须有一个前台进程。
容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。

这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可(例如service nginx start)。但是这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,这样的容器后台启动后,会立即自杀因为他觉得他没事可做了。所以,最佳的解决方案是将你要运行的程序以前台进程的形式运行,常见就是命令行模式,表示我还有交互操作,别中断,O(∩_∩)O哈哈~


列出当前所有正在运行的容器

命令格式:docker ps [OPTIONS]

OPTIONS参数说明:

-a :列出当前所有正在运行的容器+历史上运行过的

-l :显示最近创建的容器。

-n:显示最近n个创建的容器。

-q :静默模式,只显示容器编号。

案例:

[root@VM-24-10-centos /]# docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
1df0b0dd0cf7   7614ae9453d1   "docker-entrypoint.s…"   34 seconds ago   Up 33 seconds   6379/tcp   flamboyant_zhukovsky
[root@VM-24-10-centos /]# docker run -it ubuntu /bin/bash
root@ee7ac02f826b:/# read escape sequence
[root@VM-24-10-centos /]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS      NAMES
ee7ac02f826b   ubuntu         "bash"                   9 seconds ago        Up 8 seconds                   awesome_feynman
1df0b0dd0cf7   7614ae9453d1   "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp   flamboyant_zhukovsky
[root@VM-24-10-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                      PORTS      NAMES
ee7ac02f826b   ubuntu         "bash"                   21 seconds ago       Up 20 seconds                          awesome_feynman
1df0b0dd0cf7   7614ae9453d1   "docker-entrypoint.s…"   About a minute ago   Up About a minute           6379/tcp   flamboyant_zhukovsky
cdeaa8464466   redis          "docker-entrypoint.s…"   2 minutes ago        Exited (0) 2 minutes ago               hopeful_gagarin
72932ffc7aa2   ubuntu         "/bin/bash"              5 minutes ago        Exited (0) 2 minutes ago               adoring_greider
35254abd9bc8   7614ae9453d1   "docker-entrypoint.s…"   38 minutes ago       Exited (0) 34 minutes ago              wizardly_jennings
42666291afbc   ba6acccedd29   "bash"                   39 minutes ago       Exited (0) 39 minutes ago              elated_dijkstra
[root@VM-24-10-centos /]# docker ps -a -q
ee7ac02f826b
1df0b0dd0cf7
cdeaa8464466
72932ffc7aa2
35254abd9bc8
42666291afbc


退出 & 进入容器

退出容器

  1. exit:run进入容器后使用exit退出,则容器停止
  2. 快捷键ctrl+p+q:进入容器后使用快捷键退出,容器不会停止

进入容器
进入正在运行的容器并以命令行方式交互
命令格式:docker exec -it 容器ID /bin/bash

[root@VM-24-10-centos /]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
605ea04b278d   redis          "docker-entrypoint.s…"   20 minutes ago   Up 20 minutes   6379/tcp   frosty_chebyshev
ee7ac02f826b   ubuntu         "bash"                   43 minutes ago   Up 43 minutes              awesome_feynman
1df0b0dd0cf7   7614ae9453d1   "docker-entrypoint.s…"   44 minutes ago   Up 44 minutes   6379/tcp   flamboyant_zhukovsky
[root@VM-24-10-centos /]# docker exec -it 605ea04b278d /bin/bash
root@605ea04b278d:/data# redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

另一种方式:
命令格式:docker attach 容器ID,表示重新进入

注意两者区别:

  • attach 直接进入容器启动命令的终端,不会启动新的进程 用exit退出,会导致容器的停止。
  • exec 是在容器中打开新的终端,并且可以启动新的进程 用exit退出,不会导致容器的停止。

推荐使用exec


容器的停止 & 启动 & 重启

  1. 停止 正在运行的容器

命令格式:docker stop 容器ID或容器名称

  1. 强制停止 正在运行的容器

命令格式:docker kill 容器ID或容器名称

  1. 启动 已停止的容器

命令格式:docker start 容器ID或容器名称

  1. 重启 正在运行的容器

命令格式:docker restart 容器ID或容器名称

上面命令可通过docker [COMMAND] --help查看更多信息

删除已停止容器

命令格式:docker rm 容器ID如果要一次性删除多个容器实例

  • docker rm -f $(docker ps -a -q)
  • docker ps -a -q | xargs docker rm

查看容器日志

命令格式:docker logs 容器ID

查看容器内进程信息

命令格式:docker top 容器ID

查看容器内部细节

命令格式:docker inspect 容器ID

从容器拷贝文件到宿主机

命令格式:docker cp 容器ID:/dir/file /dir/file

容器的导入和导出

导出
命令格式:docker export 容器ID > file_name.tar导入
命令格式:cat file_name.tar | docker import - 用户名/镜像名:版本号然后docker images就能看到刚导入的镜像了

常用命令速查

【Docker学习笔记】Docker从入门到实战之基础篇_centos_05

attach    Attach to a running container                 # 当前 shell 下 attach 连接指定运行镜像

build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像

commit    Create a new image from a container changes   # 提交当前容器为新的镜像

cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中

create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器

diff      Inspect changes on a container's filesystem   # 查看 docker 容器变化

events    Get real time events from the server          # 从 docker 服务获取容器实时事件

exec      Run a command in an existing container        # 在已存在的容器上运行命令

export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

history   Show the history of an image                  # 展示一个镜像形成历史

images    List images                                   # 列出系统当前镜像

import    Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]

info      Display system-wide information               # 显示系统相关信息

inspect   Return low-level information on a container   # 查看容器详细信息

kill      Kill a running container                      # kill 指定 docker 容器

load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]

login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器

logout    Log out from a Docker registry server          # 从当前 Docker registry 退出

logs      Fetch the logs of a container                 # 输出当前容器日志信息

port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口

pause     Pause all processes within a container        # 暂停容器

ps        List containers                               # 列出容器列表

pull      Pull an image or a repository from the docker registry server   # 从docker镜像源服务器拉取指定镜像或者库镜像

push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器

restart   Restart a running container                   # 重启运行的容器

rm        Remove one or more containers                 # 移除一个或者多个容器

rmi       Remove one or more images       # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]

run       Run a command in a new container              # 创建一个新的容器并运行一个命令

save      Save an image to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]

search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像

start     Start a stopped containers                    # 启动容器

stop      Stop a running containers                     # 停止容器

tag       Tag an image into a repository                # 给源中镜像打标签

top       Lookup the running processes of a container   # 查看容器中运行的进程信息

unpause   Unpause a paused container                    # 取消暂停容器

version   Show the docker version information           # 查看 docker 版本号

wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值


Docker镜像


是什么


重点理解

Docker镜像层都是只读的,容器层是可写的。

当容器启动时,一个新的可写层被加载到镜像的顶部,这一层通常被称作“容器层”,“容器层”之下都叫“镜像层”。

所有对容器的改动 - 无论添加、删除、还是修改文件都只会发生在容器层中。只有容器层是可写的,容器层下面的所有镜像层都是只读的。

【Docker学习笔记】Docker从入门到实战之基础篇_Docker_06


Docker镜像commit操作案例

docker commit 提交容器副本使之成为一个新的镜像
命令格式:docker commit -m="description" -a="author" 容器ID 新镜像名:[tag]例如,将本地已存在的ubuntu镜像,安装vim工具,生成workfile.txt,然后提交新镜像。

[root@VM-24-10-centos ~]# docker run -it ba6acccedd29 /bin/bash
root@3ccbfc3ff293:~# apt-get update
……
root@3ccbfc3ff293:~# apt-get -y install vim
……
root@3ccbfc3ff293:~# touch workfile.txt
root@3ccbfc3ff293:~# vim workfile.txt
……
root@3ccbfc3ff293:~# [root@VM-24-10-centos ~]# 
[root@VM-24-10-centos ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
3ccbfc3ff293   ba6acccedd29   "/bin/bash"              10 minutes ago   Up 10 minutes                                                          trusting_dirac
[root@VM-24-10-centos ~]# docker commit -m="my ubuntu" -a="will" 3ccbfc3ff293 myubuntu:1.0.1
sha256:097bf19d27035e68b1ecae6f3dd9565c33feeec36e47ab9545b98bde4e9e7248
[root@VM-24-10-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
myubuntu      1.0.1     097bf19d2703   55 seconds ago   183MB
ubuntu        latest    ba6acccedd29   18 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   18 months ago    13.3kB
[root@VM-24-10-centos ~]#

命令解析:
apt-get update #更新包管理工具

apt-get -y install vim #安装vim工具


本地镜像发布到阿里云


阿里云Docker介绍

【Docker学习笔记】Docker从入门到实战之基础篇_MySQL_07

制作镜像的方法

  • docker commit
  • DockerFile


阿里云 容器镜像服务

账号:*******

密码:*******

  • 打开容器镜像服务首页
  • 创建命名空间
  • 创建镜像仓库
  • 根据提示命令操作拉取&推送即可

【Docker学习笔记】Docker从入门到实战之基础篇_Docker_08

将镜像推送到Registry

将前面制作的ubuntu镜像上传到阿里云镜像仓库。上传完成就能在阿里云镜像仓库中看到了

[root@VM-24-10-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
myubuntu      1.0.1     097bf19d2703   55 seconds ago   183MB
redis         latest    7614ae9453d1   15 months ago    113MB
mysql         8.0       3218b38490ce   15 months ago    516MB
ubuntu        latest    ba6acccedd29   18 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   18 months ago    13.3kB
[root@VM-24-10-centos ~]#  docker login --username=<username> registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@VM-24-10-centos ~]# docker tag 097bf19d2703 registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub:1.0.1
[root@VM-24-10-centos ~]# docker push registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub:1.0.1
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub]
1fb4eb4b5d52: Pushed 
9f54eef41275: Pushed 
1.0.1: digest: sha256:e163f2b1ca76f891ca12085a434e0557b21458856a2b4cad7cbcc69282561f7c size: 741
[root@VM-24-10-centos ~]#


从Registry中拉取镜像

将本地的unbuntu删除,然后从阿里云镜像仓库拉取,启动容器会发现workfile.txt依然在

[root@VM-24-10-centos ~]# docker images
REPOSITORY                                                     TAG       IMAGE ID       CREATED          SIZE
myubuntu                                                       1.0.1     097bf19d2703   41 minutes ago   183MB
registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub   1.0.1     097bf19d2703   41 minutes ago   183MB
redis                                                          latest    7614ae9453d1   15 months ago    113MB
mysql                                                          8.0       3218b38490ce   15 months ago    516MB
ubuntu                                                         latest    ba6acccedd29   18 months ago    72.8MB
hello-world                                                    latest    feb5d9fea6a5   18 months ago    13.3kB
[root@VM-24-10-centos ~]# docker rmi -f 097bf19d2703
Untagged: myubuntu:1.0.1
Untagged: registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub:1.0.1
Untagged: registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub@sha256:e163f2b1ca76f891ca12085a434e0557b21458856a2b4cad7cbcc69282561f7c
Deleted: sha256:097bf19d27035e68b1ecae6f3dd9565c33feeec36e47ab9545b98bde4e9e7248
Deleted: sha256:80b8fc3a37095f785161908d7dc44817cb469ad863970f812d405262132d00c0
[root@VM-24-10-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
redis         latest    7614ae9453d1   15 months ago   113MB
mysql         8.0       3218b38490ce   15 months ago   516MB
ubuntu        latest    ba6acccedd29   18 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   18 months ago   13.3kB
[root@VM-24-10-centos ~]#  docker pull registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub:1.0.1
1.0.1: Pulling from codeplayground/mydockerhub
7b1a6ab2e44d: Already exists 
547a4071b58d: Pull complete 
Digest: sha256:e163f2b1ca76f891ca12085a434e0557b21458856a2b4cad7cbcc69282561f7c
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub:1.0.1
registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub:1.0.1
[root@VM-24-10-centos ~]# docker images
REPOSITORY                                                     TAG       IMAGE ID       CREATED          SIZE
registry.cn-hangzhou.aliyuncs.com/codeplayground/mydockerhub   1.0.1     097bf19d2703   42 minutes ago   183MB
redis                                                          latest    7614ae9453d1   15 months ago    113MB
mysql                                                          8.0       3218b38490ce   15 months ago    516MB
ubuntu                                                         latest    ba6acccedd29   18 months ago    72.8MB
hello-world                                                    latest    feb5d9fea6a5   18 months ago    13.3kB
[root@VM-24-10-centos ~]# docker run -it 097bf19d2703 /bin/bash
root@7ad72d62213c:/# cd ~
root@7ad72d62213c:~# cat workfile.txt 
hello vim !
root@7ad72d62213c:~#


本地镜像发布到私有仓库


Docker Registry

  • 官方Docker Hub地址:https://hub.docker.com/,中国大陆访问太慢了且准备被阿里云取代的趋势,不太主流。
  • Dockerhub、阿里云这样的公共镜像仓库可能不太方便,涉及机密的公司不可能提供镜像给公网,所以需要创建一个本地私人仓库供给团队使用,基于公司内部项目构建镜像。
  • Docker Registry是官方提供的工具,可以用于构建私有镜像仓库。


搭建私有镜像库

<以后需要再补充>


Docker容器数据卷

是什么

卷就是目录或文件,存在于一个或多个容器中,由docker挂载到容器,但不属于联合文件系统,因此能够绕过Union File System提供一些用于持续存储或共享数据的特性。卷的设计目的就是数据的持久化,完全独立于容器的生存周期,因此Docker不会在容器删除时删除其挂载的数据卷。

容器卷,将docker容器内的数据保存进宿主机文件系统中。简单理解为类似Redis里面的rdb和aof文件;

运行一个带有容器卷存储功能的容器实例,命令格式:
docker run -it --privileged=true -v /宿主机绝对路径:/容器内路径 --name=容器名称 镜像ID


能干嘛

将运用与运行的环境打包镜像,run后形成容器实例运行 ,但是我们对数据的要求希望是持久化的;
Docker容器产生的数据,如果不备份,那么当容器实例删除后,容器内的数据自然也就没有了;
为了能保存数据在docker中我们使用卷。
特点:
1:数据卷可在容器之间共享或重用数据
2:卷中的更改可以直接实时生效,爽
3:数据卷中的更改不会包含在镜像的更新中
4:数据卷的生命周期一直持续到没有容器使用它为止


使用案例

  1. 宿主和容器之间映射添加容器卷

启动ubuntu容器,并在宿主和容器之间映射添加容器卷,此时容器和宿主机的挂载目录可以实现数据共享

[root@VM-24-10-centos /]# docker run -it --privileged=true -v /tmp/ubuntu_volume:/tmp/ubuntu_volume --name=u2 ubuntu /bin/bash

# 查看数据卷是否挂在成功
[root@VM-24-10-centos /]# docker inspect u2
[
 ……
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/tmp/ubuntu_volume",
                "Destination": "/tmp/ubuntu_volume",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
……
]
  1. 读写规则映射添加说明
  • 读写(默认)
  • 参数:--privileged=true -v /tmp/ubuntu_volume:/tmp/ubuntu_volume
  • 只读
  • 容器实例内部被限制只读,,read only
  • 参数:--privileged=true -v /tmp/ubuntu_volume:/tmp/ubuntu_volume**:ro**
  1. 卷的继承和共享

参数:--volumes-from 父容器ID或名称

例如:docker run -it --privileged=true --volumes-from u1 --name=u2 ubuntu /bin/bash


Docker常规安装

总体步骤

  1. 搜索镜像
  1. https://hub.docker.com/_/mysql
  2. docker search 镜像名称
  1. 拉取镜像
  2. 查看镜像
  3. 启动镜像
  1. 服务端口映射
  1. 停止容器
  2. 移除容器

安装MySQL


获取镜像

docker hub查找所需要的版本
拉取MySQL镜像到本地:docker pull mysql:8.0

创建容器

创建容器,指定容器卷目录、指定mysql实例连接密码

注意:如果你是云服务器,需要在控制台手动开放端口,才能远程连接使用

docker run -d  \
	-p 3306:3306  \
	--privileged=true  \
	-v /atguigu/mysql/log:/var/log/mysql  \
	-v /atguigu/mysql/data:/var/lib/mysql  \
	-v /atguigu/mysql/conf:/etc/mysql/conf.d  \
	-e MYSQL_ROOT_PASSWORD=123456  \
	--name=atguigu-mysql-8  \
	mysql:8.0


配置

1、MySQL设置登陆密码
可以像上面创建容器启动时设置环境变量方式设置密码,还可以直接进入mysql设置密码

docker exec -it 
mysql -uroot -p atguigu-mysql-8 
ALTER USER 'root@%' IDENTIFIED WITH mysql_native_password BY '123456'

2、容器环境-字符乱码

容器中使用mysql时,默认不支持中文,可在进入容器时指定环境配置

命令:docker exec -it 3e68c5168d7e **env LANG=C.UTF-8** /bin/bash

3、MySQL字符编码配置

docker环境中,mysql默认拉丁文字符集

【Docker学习笔记】Docker从入门到实战之基础篇_MySQL_09


可以在容器卷映射目录添加配置文件

# cd /atguigu/mysql/conf
# touch my.cnf
# vi my.cnf

将以下配置添加到配置文件

[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8

检查是否配置成功show variables like 'character%';

然后重启容器docker restart即可


Ref

尚硅谷Docker实战教程(docker教程天花板)


个人学习笔记~欢迎您的指正\建议\意见~