欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7 Docker 安装及镜像和容器的基本命令

程序员文章站 2024-01-31 12:15:40
...
  • 安装环境

CentOS7  root用户

  • 将yum包更新到最新

yum update
  • 安装需要的软件包

yum-utils提供yum-config-manager功能,另外两个devicece mapper是驱动依赖的包

yum install -y yum-utils device-mapper-persistent-data lvm2
  • 设置yum源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • 安装docker

yum install -y docker-ce

验证是否安装成功

[aaa@qq.com ~]# docker -v
Docker version 19.03.6, build 369ce74a3c
  • 配置阿里云加速器

登录阿里云,根据下图箭头所指操作:

CentOS7 Docker 安装及镜像和容器的基本命令

CentOS7 Docker 安装及镜像和容器的基本命令

以下获取到的代码就是你的加速代码:

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["你的加速器地址"]
}
EOF

# 重启docker
sudo systemctl daemon-reload
sudo systemctl restart docker

直接粘贴即可。注意:最后一行   systemctl restart docker   这一行是粘贴不上的。需要重新复制一下粘贴。

粘贴完之后验证一下:

​​​​​​​cat /etc/docker/daemon.json   #查看这个文件

{
  "registry-mirrors": ["https://*******.mirror.aliyuncs.com"]
}
  • docker服务的管理

systemctl start docker      #开启
systemctl stop docker       #停止
systemctl restart docker    #重启
systemctl enable docker     #开机启动
systemctl status docker     #查看状态

#查看状态结果演示:running

[aaa@qq.com ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2020-02-23 13:59:36 CST; 8min ago
     Docs: https://docs.docker.com
 Main PID: 11579 (dockerd)
    Tasks: 8
   Memory: 46.3M
   CGroup: /system.slice/docker.service
           └─11579 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

2月 23 13:59:35 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:35.076937797+08:00" le...rpc
2月 23 13:59:35 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:35.076951996+08:00" le...rpc
2月 23 13:59:35 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:35.076959177+08:00" le...rpc
2月 23 13:59:35 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:35.162544901+08:00" le...t."
2月 23 13:59:36 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:36.329348664+08:00" le...ss"
2月 23 13:59:36 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:36.829029223+08:00" le...e."
2月 23 13:59:36 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:36.891769205+08:00" le...3.6
2月 23 13:59:36 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:36.891982409+08:00" le...on"
2月 23 13:59:36 localhost.localdomain dockerd[11579]: time="2020-02-23T13:59:36.917905174+08:00" le...ck"
2月 23 13:59:36 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
  • 镜像管理命令

docker images  查看本地镜像    docker images -q 查看本地所有镜像的ID
docker search  在线搜索镜像
docker pull    下载拉取镜像    docker pull mysql:5.5 下载指定版本的镜像
docker rmi     删除本地镜像    docker rmi `docker images -q`删除本地所有镜像
  • docker images  #查看本地镜像

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              afaec1334369        2 days ago          471MB
mysql               5.5                 d404d78aa797        9 months ago        205MB

镜像名称             版本                 镜像ID               创建时间            大小

docker images -q  #查看本地所有镜像的ID

[aaa@qq.com ~]# docker images -q
d404d78aa797
  • docker search  #搜索远程仓库镜像

NAME(名称), DESCRIPTION(描述),STARS(点赞数)

OFFICIAL(是否官方),AUTOMATED(是否自动流程构建)

[aaa@qq.com ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9152                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3248                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   676                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   69                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   62                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      36                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
prom/mysqld-exporter                                                              26                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   24                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   18                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   14                                      
arey/mysql-client                 Run a MySQL client from a docker container      13                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         10                                      
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   5                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  4                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   2                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
[aaa@qq.com ~]# 
  • docker pull 下载镜像

[aaa@qq.com ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
619014d83c02: Pull complete 
9ced578c3a5f: Pull complete 
731f6e13d8ea: Pull complete 
3c183de42679: Pull complete 
6de69b5c2f3c: Pull complete 
122a561a4196: Pull complete 
1abf8e9f34f0: Pull complete 
1e5887414166: Pull complete 
95adaca07078: Pull complete 
42c8c6542347: Pull complete 
0ae93d9077ae: Pull complete 
42131d6ef54e: Pull complete 
Digest: sha256:c7c6c5beb312fd2eb21af4f144d14b6ef29c9c2f9c5e1f3f74ffa75e38fad1f4
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              afaec1334369        2 days ago          471MB

docker pull mysql这样下载的是mysql的最新版——latest,可是我不想要最新版,我就想要mysql5.5版本。怎么办呢?

方法一:

命令:docker pull mysql:5.5   英文冒号

docker pull mysql:5.5    #下载指定版本,下载前线查询一下有没有

#先查看一下有没有mysql5.5

[aaa@qq.com ~]# docker search mysql5.5
NAME                     DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
micktwomey/mysql5.5                                                      1                                       [OK]
717160040/mysql5.5       MySQL5.5root带远程连接权限                             1                                       
andreiqw/mysql5.5.22     mysql 5.5.22 based on debian:jessie. Repo: h…   0                                       
gaumire/mysql5.5         Custom MySQL image version 5.5.                 0                                       
phox/mysql5.5                                                            0                                       [OK]
jmw1/mysql5.5            Default .cnf MySQL Server                       0                                       
prodaptimages/mysql5.5                                                   0                                       
shrebo/mysql5.5                                                          0                                       
etsbim/mysql5.5                                                          0                                       
lopydev/mysql5.5         mysql5.5                                        0                                       
pengfeifan/mysql5.5.53                                                   0                                       
jiezhiz/mysql5.5                                                         0                                       
smartatech/mysql5.5                                                      0                                       
adolgarev/mysql5.5                                                       0                                       
461525162/mysql5.5       mysql 5.5 for centos7                           0                                       
kyawphyonaing/mysql5.5                                                   0                                       
vikasmunjal25/mysql5.5                                                   0                                       
cwa302/mysql5.5                                                          0                                       
lujieni/mysql5.5                                                         0                                       
luohua2018/mysql5.5                                                      0                                       
zm274310577/mysql5.5                                                     0                                       
silkesrp/mysql5.5.55                                                     0                                       
maitarek/mysql5.5                                                        0                                       
alcionemorais/mysql5.5   Instala mysql5.5                                0                                       
gilra200/mysql5.5                                                        0      


#下载mysql:5.5

[aaa@qq.com ~]# docker pull mysql:5.5
5.5: Pulling from library/mysql
743f2d6c1f65: Pull complete 
3f0c413ee255: Pull complete 
aef1ef8f1aac: Pull complete 
f9ee573e34cb: Pull complete 
3f237e01f153: Pull complete 
03da1e065b16: Pull complete 
04087a801070: Pull complete 
7efd5395ab31: Pull complete 
1b5cc03aaac8: Pull complete 
2b7adaec9998: Pull complete 
385b8f96a9ba: Pull complete 
Digest: sha256:12da85ab88aedfdf39455872fb044f607c32fdc233cd59f1d26769fbf439b045
Status: Downloaded newer image for mysql:5.5
docker.io/library/mysql:5.5

#下载成功

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              afaec1334369        2 days ago          471MB
mysql               5.5                 d404d78aa797        9 months ago        205MB

方法二:

打开 https://hub.docker.com  搜索 ,之后下载,好了之后上传到centos中docker的镜像目录,详细的这里就不介绍了。只给大家说一下上传到哪个目录。使用docker info命令可以查看镜像信息,信息如下:

[aaa@qq.com ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 2
 Server Version: 19.03.6
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-1062.12.1.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 972.4MiB
 Name: localhost.localdomain
 ID: IONN:BK4R:FIU5:IDDZ:6OE4:SVUF:IOF6:BGBO:FD2R:OHMZ:FDOZ:ZIZO
 Docker Root Dir: /var/lib/docker    <——————镜像目录
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://tue4pc99.mirror.aliyuncs.com/
 Live Restore Enabled: false

我们可以看到 /var/lib/docker<——————镜像目录,上传到这里就可以了。

  • docker  rmi镜像删除

docker  rmi  镜像ID/名称和版本号

例如:我现在不想要mysql最新版,只留下mysql5.5

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              afaec1334369        2 days ago          471MB
mysql               5.5                 d404d78aa797        9 months ago        205MB

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              afaec1334369        2 days ago          471MB
mysql               5.5                 d404d78aa797        9 months ago        205MB
[aaa@qq.com ~]# ^C
[aaa@qq.com ~]# ^C
[aaa@qq.com ~]# docker rmi afaec1334369 
Error response from daemon: invalid reference format
这里报错说是:守护进程的错误响应:无效的引用格式,那我们通过版本号删除

[aaa@qq.com ~]# docker rmi mysql:latest
Untagged: mysql:latest
Untagged: aaa@qq.com:c7c6c5beb312fd2eb21af4f144d14b6ef29c9c2f9c5e1f3f74ffa75e38fad1f4
Deleted: sha256:afaec1334369d4bb6fe566f3f3d4b075ed4b74a6fcba61f9740aee4715888b21
Deleted: sha256:2e6c9aa9d020930f5cd87642a6b1301b0fb6530541ad2c1307f6a5a7c6be2bf8
Deleted: sha256:12cc75beecacd8f363465aaf4dba0c6af62013a358c4262ec3bd34aa7085d124
Deleted: sha256:b025f80f98cfb69a52cd92831df7fd664e6800234dbbf23a3f987c099c187c05
Deleted: sha256:a4de85eae15629437f4975d194a6e2beafc75491552aec6ce447381faa77b2fd
Deleted: sha256:40529fb0dad09f427d3bfe1ce6eab16498ad2220bddd43ecc9aafa1982c9ea9c
Deleted: sha256:c4f22e2745a43e3f085510b5badf7c290a0695fc5608cc4e7283b6eb177c493f
Deleted: sha256:27b43d2e6ee5180ecadf4827dce0cddbfd5220a0760e08b2965656253c66c772
Deleted: sha256:0de337169373e6779cb3ca09485e95fedd4ac98abee19b839cd46e294a64f363
Deleted: sha256:73f1cb0f35d3377b825488e38241d0e12c63e7d30946362402dd8ab2e9467d81
Deleted: sha256:5807022bbb80a63e78831d4dff1ac497a450287ce43fbb0381623b19f5d45c8a
Deleted: sha256:1aaef8d601e09d40fc66f3531268e837f4ae3eedf84f94359fa33177f0be4c6e
Deleted: sha256:e0db3ba0aaea8a01d5cb000aeb449c153be0a47a369cafc4e912b85fb18192cf

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.5                 d404d78aa797        9 months ago        205MB

删除成功

docker  rmi `docker images -q`  #删除本地所有镜像, `这个符号是tab键上面的键

相关标签: docker