docker学习

发布时间 2023-04-19 16:08:35作者: 木易同学

镜像(image)

镜像可以理解为一个模板,里边有一些东西,可以通过run命令来进行运行。通过某一个镜像可以创建多个容器(最终的服务或者是项目就是运行在容器里边的)

 

容器(container)

Docker利用容器技术可以独立运行一个或者一组应用。可以通过镜像运行启动、停止、删除的基本命令

目前可以把容器理解为一个简易的Linux系统

 

仓库(repository)

存放镜像的地方分为公有仓库(Docker hub、腾讯云、阿里云等等)和私有仓库

公有仓库默认都是国外的,需要配置镜像加速

 

第一天:常用的命令

镜像命令

docker images查看本机全部镜像 

[root@centoss dockertest]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   6 months ago   13.3kB

# 详细解释
REPOSITORY    镜像的仓库源
TAG           镜像的版本标签信息
IMAGE ID      镜像的ID
CREATED       镜像的创建时间
SIZE          镜像的大小

# 可选项
Options:
  -a, --all             # 列出所有的镜像
      --digests         # 展示digests签名信息
  -q, --quiet           # 只显示镜像的ID
  ###################################### 这下便的并不常用就不再多家解释了
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output

docker search 搜索镜像

一个查找MySQL镜像的实例

[root@localhost muyi]# docker search mysql
NAME                            DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14048               [OK]                
mariadb                         MariaDB Server is a high performing open sou…   5367                [OK]                
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   783                 [OK]                
percona                         Percona Server is a fork of the MySQL relati…   604                 [OK]                
bitnami/mysql                   Bitnami MySQL Docker Image                      83                                      [OK]
databack/mysql-backup           Back up mysql databases to... anywhere!         82                                      
linuxserver/mysql-workbench                                                     48                                      
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   45                                      
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   38                                      
circleci/mysql                  MySQL is a widely used, open-source relation…   29                                      
google/mysql                    MySQL server for Google Compute Engine          23                                      [OK]
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   14                                      
bitnami/mysqld-exporter                                                         4                                       
vitess/mysqlctld                vitess/mysqlctld                                1                                       [OK]

docker pull下载镜像

# 下载命令格式:docker pull mysql[:tag]

[root@centos dockertest]# docker pull mysql
Using default tag: latest            # 上边没有写tag要求具体的版本号,这边的就是下载latest最新版
latest: Pulling from library/mysql
f003217c5aae: Pull complete          # 分层下载,docker images的核心  联合文件系统
65d94f01a09f: Pull complete
43d78aaa6078: Pull complete
a0f91ffbdf69: Pull complete
59ee9e07e12f: Pull complete
04d82978082c: Pull complete
70f46ebb971a: Pull complete
db6ea71d471d: Pull complete
c2920c795b25: Pull complete
26c3bdf75ff5: Pull complete
9ec1f1f78b0e: Pull complete
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195    # 签名信息(防伪标志)
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest    # 真实地址,也就是说"docker pull mysql"和"docker pull docker.io/library/mysql:latest"是同一个意思

# 下载其他版本的话可以查官方支持的版本。然后比如说:
docker pull mysql:5.7
# 下边有一张图可以看到官方提供的信息,5.7是存在的

###########################################
[root@centos dockertest]# docker pull mysql:5.7
5.7: Pulling from library/mysql
f003217c5aae: Already exists        # 这些东西实际上都是使用的共用的,这就是docker的联合文件系统所讲到的东西
65d94f01a09f: Already exists
······
ba61822c65c2: Pull complete
······
Digest: sha256:1a73b6a8f507639a8f91ed01ace28965f4f74bb62a9d9b9e7378d5f07fab79dc
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
###########################################

# 解释
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

docker rmi删除镜像(这边的rmi代表的是remove image)

[root@centos dockertest]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       f26e21ddd20d   2 days ago     450MB
mysql         latest    667ee8fb158e   2 days ago     521MB
hello-world   latest    feb5d9fea6a5   6 months ago   13.3kB
[root@centos dockertest]# docker rmi -f 667ee8fb158e
Untagged: mysql:latest
Untagged: mysql@sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195
Deleted: sha256:667ee8fb158e365450fc3f09712208fe44e9f1364a9b130fed95f3f4862f8a63
Deleted: sha256:daba3b5cd1b725f259d5d59049c7011f6a20f2e7e4782b873cadeedbc1a4f122
Deleted: sha256:407a68a397de3c8e4da2ccd388f27c795519414cc9319f674e7b9a492cd8514b
Deleted: sha256:6967db79cb8031d7e505dd82c91c6426e84c6ad2098d06c1c090485886bc1c63
Deleted: sha256:3dcc0f922d11309b2e4870d5f42c5005f086c9b9c1e128ed2b712b65fa79a5c9
Deleted: sha256:ae5d4ab2734d5c27069212b2690dc2c7ac4cd85ee5e122607cb175f7fe908914
[root@centos dockertest]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       f26e21ddd20d   2 days ago     450MB
hello-world   latest    feb5d9fea6a5   6 months ago   13.3kB

# !!!!删除全部镜像
docker rmi -f $(docker images- aq) # 这边的$代表的是传参,执行$后边括号里边的内容并发送到前边进行执行,简单来说就是查询目前所有镜像的id并依次删除

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

容器命令

docker run 启动容器

命令格式:docker run [可选参数] (image)

常见参数说明:
--name="NAME"    # 给容器起的名字
-d               # 表示以后台方式运行
-i   -t   -it    # 使用交互方式运行,可以进入容器查看内容
-p               # 指定容器的端口,可以主机映射 比如:-p 8080:8080
    -p (ip:主机端口:容器端口)
    -p (主机端口:容器端口)    # 常用
    -p (容器端口)
-P               # 上边的是小写的p,这个是大写的,表示随机指定端口

docker ps查看所有正在运行中的容器

# 查看正在运行的容器
[root@centos dockertest]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
# 查看运行过的容器倒序输出
[root@centos dockertest]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                     PORTS     NAMES
de33bc517d24   centos        "/bin/bash"   6 minutes ago   Exited (0) 2 minutes ago             stupefied_rhodes
403730c6b9e3   hello-world   "/hello"      2 hours ago     Exited (0) 2 hours ago               kind_kare
d7d3f3b9bc35   hello-world   "/hello"      2 hours ago     Exited (0) 2 hours ago               cranky_almeida

# 常用选项
docker ps        # 列出当前正在运行的容器
          -a     # 列出当前正在运行的容器以及曾经运行过的容器
          -n=?   # 显示最近创建的容器(传入的参数表示显示个数)
          -q     # 只显示容器的编号(和只显示镜像编号那里(注意下))

快捷键退出容器

exit        # 容器停止并退出
Crtl -P+Q   # 容器不停止并退出

docker rm删除容器

docker rm (容器id)    # 不能删除正在运行中的容器,如果需要删除运行中的容器需要加上-f
docker rm -f $(docker ps -aq) # 删除全部容器
docker ps -a -q|xargs docker rm # 删除全部容器

启动和停止容器docker start

docker start (容器id)    # 启动容器
docker restart (容器id)  # 重启容器
docker stop (容器id)     # 停止容器
docker kill (容器id)     # 暴力停止容器
docker attach (容器id)   # 进入正在运行中的容器

其他命令

查看日志 :docker logs (容器id)

查看容器进程信息:docker top (容器id)

查看容器详细信息:docker inspect

进入当前正在运行的容器:
docker exec -it

docker attach

从容器拷贝文件到主机上:docker cp (容器id:容器内路径) (目的主机路径)