(一)Docker的安装及基本命令

一、安装环境

CentOS7 root用户

二、安装docker

1、将yum包更新到最新

yum update

2、安装需要的软件包

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

yum install -y yum-utils device-mapper-persistent-data lvm2

3、设置yum源

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

4、安装docker

yum install -y docker-ce
docker -v
Docker version 19.03.6, build 369ce74a3c

5、配置阿里云加速器

​​​​登录阿里云,根据下图箭头所指操作:
(一)Docker的安装及基本命令_第1张图片
以下获取到的代码就是你的加速代码:
(一)Docker的安装及基本命令_第2张图片
以下获取到的代码就是你的加速代码:

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

重启docker

systemctl daemon-reload
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
 
[root@localhost ~]# 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相关命令

1、docker info 守护进程的系统资源设置

[root@pokes03 ~]# docker info
Client:
 Debug Mode: false
 
Server:
 Containers: 2    
  Running: 2        #运行的容器数量
  Paused: 0
  Stopped: 0
 Images: 4          #共有几个镜像
 Server Version: 19.03.11    #docker版本
 Storage Driver: overlay2    #存储驱动,如果你的这里是dm说明你的内核版本太低。overlay远比dm强大
  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: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-1127.8.2.el7.x86_64    #内核版本
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 468.4MiB    #资源数
 Name: pokes03
 ID: J2RQ:MO7G:TEZO:UIRQ:NJDJ:XCSH:X7MN:KYJE:BBOE:GP5F:WT4S:HVOE
 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
 
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

以上你只需要注意你的存储驱动即可,其他都只是了解。

2、docker search 搜索远程仓库镜像

NAME(名称), DESCRIPTION(描述),STARS(点赞数)OFFICIAL(是否官方),AUTOMATED(是否自动流程构建)

[root@localhost ~]# 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]
[root@localhost ~]# 

除了上面的方法搜索镜像之外,你还可以访问官网:hub.docker.com

3、docker pull 下载镜像

以下载MySQL为例。

3.1、下载MySQL最新版

不用加任何版本号,默认就是最新版。最新版latest版本

[root@localhost ~]# 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
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              afaec1334369        2 days ago          471MB

3.2、下载指定版本MySQL5.5

下载mysql5.5的镜像

docker pull mysql:5.5

下载前线查询一下有没有

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

[root@localhost ~]# 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

[root@localhost ~]# 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

#下载成功

4、docker images 查看本地镜像

上面我们下载成功了MySQL5.5,我们需要查看本地的镜像

[root@localhost ~]# 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

5、docker run 运行镜像生成容器

doker run --name mywordpress --link db:mysql -p 8080:80 -d wordpress
运行容器      给容器起个名字		连接MySQL        映射端口    使用的镜像

后续详细讲解,这里先提一下。

6、docker ps 查看容器的进程

[root@pokes03 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
1c81ec9d37fe        wordpress:latest    "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:8000->80/tcp   root_wordpress_1
1daf35d8d135        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Up About an hour    3306/tcp, 33060/tcp    root_db_1
[root@pokes03 ~]# 

7、docker stop 停止容器

docker stop 容器名称版本号或者容器ID号

最好是使用ID,如果使用名称则还需要加上版本号

停止所有运行的容器

docker stop $(docker ps -aq)
docker stop `docker ps -aq`

以上两个命令是等价的。

8、docker rmi 删除容器

docker+rmi+镜像ID或者名称和版本号

删除容器之前,必须先停止容器,否则无法删除。

docker  rmi `docker images -q`
docker rm $(docker ps -aq)

9、docker inspect 查看容器所有基本信息

[root@pokes03 ~]# docker inspect wordpress:latest
[
    {
        "Id": "sha256:2c9350d16623803c9bcfa58973b9bdba18e06d3107b15ba8090c09db946c27bf",
        "RepoTags": [
            "wordpress:latest"
        ],
        "RepoDigests": [
            "wordpress@sha256:602ea0959040cd101e5e6923474612a15f9dd330ab45ceaec4e07dc762d6
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-06-12T00:07:55.207481684Z",
        "Container": "a5b6cd410c01c96a947c5e32490970812359d7cee4baeedffbfb28c39b7a0e23",
        "ContainerConfig": {
            "Hostname": "a5b6cd410c01",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \
                "PHP_INI_DIR=/usr/local/etc/php",
                "APACHE_CONFDIR=/etc/apache2",
                "APACHE_ENVVARS=/etc/apache2/envvars",
                "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2 --disable-cgi",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE
                "PHP_LDFLAGS=-Wl,-O1 -pie",
                "GPG_KEYS=CBAF69F173A0FEA4B537F470D66C9593118BCCB6 F38252826ACD957EF380D39
                "PHP_VERSION=7.3.19",
                "PHP_URL=https://www.php.net/distributions/php-7.3.19.tar.xz",
                "PHP_ASC_URL=https://www.php.net/distributions/php-7.3.19.tar.xz.asc",
                "PHP_SHA256=6402faa19b1a8c4317c7612632bce985684a5bbae0980a5779a40194398824
                "PHP_MD5=",
                "WORDPRESS_VERSION=5.4.2",
                "WORDPRESS_SHA1=e5631f812232fbd45d3431783d3db2e0d5670d2d"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"apache2-foreground\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:c10a2278bd33b371ee332f97d8eea4b18b297e7eb6ee086e4b829ff224640
            "Volumes": {
                "/var/www/html": {}
            },
            "WorkingDir": "/var/www/html",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGWINCH"
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \
                "PHP_INI_DIR=/usr/local/etc/php",
                "APACHE_CONFDIR=/etc/apache2",
                "APACHE_ENVVARS=/etc/apache2/envvars",
                "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2 --disable-cgi",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE
                "PHP_LDFLAGS=-Wl,-O1 -pie",
                "GPG_KEYS=CBAF69F173A0FEA4B537F470D66C9593118BCCB6 F38252826ACD957EF380D39
                "PHP_VERSION=7.3.19",
                "PHP_URL=https://www.php.net/distributions/php-7.3.19.tar.xz",
                "PHP_ASC_URL=https://www.php.net/distributions/php-7.3.19.tar.xz.asc",
                "PHP_SHA256=6402faa19b1a8c4317c7612632bce985684a5bbae0980a5779a40194398824
                "PHP_MD5=",
                "WORDPRESS_VERSION=5.4.2",
                "WORDPRESS_SHA1=e5631f812232fbd45d3431783d3db2e0d5670d2d"
            ],
            "Cmd": [
                "apache2-foreground"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:c10a2278bd33b371ee332f97d8eea4b18b297e7eb6ee086e4b829ff224640
            "Volumes": {
                "/var/www/html": {}
            },
            "WorkingDir": "/var/www/html",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": null,
            "StopSignal": "SIGWINCH"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 540262290,
        "VirtualSize": 540262290,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d0d5e484c0b2d7c53a387f32034d1a76c823774e59c46666c160b89dc940c4fceef6aaf52b09ffbef0495a37895f/diff:/var/lib/docker/overlay2/7da/var/lib/docker/overlay2/d30bd98850c4db91ad8f3a2304bb2861e828c20d1f50f6b1b385ec0600f8399d/95fb7f1c62e43b13de672108a55/diff:/var/lib/docker/overlay2/63c0b97bb2a0578a66fb5c3f3eb13ae7e1177c9abc306fd151fb83c48581a7f4fbe88b82ac456efc083645d327bd/diff:/var/lib/docker/overlay2iff:/var/lib/docker/overlay2/82f426dee1fcf26b0cd754d89c4dbf7be83e7ac6a342d5796f79b8e8d9b030b9fd3ad9dd3f71a52ee9ec37eeb857/diff:/var/lib/docker/overlay2/90492d2f9575c394aaeecf40c31c34b71a96a10696c7706f05a21fe11957e1e4427e2d2f52995d78bcacc5fd71a5/diff:/var/lib/docker/over16/diff:/var/lib/docker/overlay2/94502395d936aa593d4575cebc7feaa39f361e07ac0661ef30aea379154cb345475703ded124b8343fa5fc26a564/diff:/var/lib/docker/overlay2/48ae99b670235cb9a6b05e51ay2/213e63785f45838a744391f25e3874480202e41e26faa54cc784a00a2e3330a9/diff:/var/lib/docker/4b0e70/diff:/var/lib/docker/overlay2/0d4b21ac998b9a81f0e3f9fe6ebafa41c657fb1bff36f0634555723f9f7f3e501f71ab286639f8a4979cae93e3b3/diff",
                "MergedDir": "/var/lib/docker/overlay2/ccafc8b7d78d176f58b618c0744455292e7
                "UpperDir": "/var/lib/docker/overlay2/ccafc8b7d78d176f58b618c0744455292e7f
                "WorkDir": "/var/lib/docker/overlay2/ccafc8b7d78d176f58b618c0744455292e7fe
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:13cb14c2acd34e45446a50af25cb05095a17624678dbafbcc9e26086547c1d74",
                "sha256:4e53c951cb3be8dd433b04b4baaf5d7e3d28b8769e8ee695f9168b256e85754f",
                "sha256:a5df928da0a7242fe40ea6375f20d8434e1a5b88ad6453c3e4e1196d46a55187",
                "sha256:db497de51efd83c51695b81f35d1e73e3d6d99f645cad9e26777a474abe9f660",
                "sha256:fe216093409999341449fdc24c41bf40e73dd165f1df2e1616de862e6f9400a0",
                "sha256:5be09852131792826711673c0b2ee7abde7d52ada63c74de1c7f812182604106",
                "sha256:32e2a84acc26eaa3a3afaed80376a67c8498ddc6825622ac1c81ed7376741215",
                "sha256:cd275085ca7adac1147329b75445799a169d87c19c6fa84c1c62ac7d148248d5",
                "sha256:331aeb83b78f90b8d549e951d766d51b97c87c34a53d5336b9e1f1d59d4b164e",
                "sha256:e4039a54cf07b15bfd65fd073911e7f0bd3b50733f7d68af2883e3abe701563d",
                "sha256:4f34bd1e5854798e75cb689c46a34c33d02d27902a5741e1845fccaad22b11c9",
                "sha256:fa0bdf5f6bfd63960ee85c04d1c52ba6c96778ed6bfc539aa81ae06e0ad43137",
                "sha256:0838256aa71b34314a47ea271ba905b87fe6ad6f903f7c6d3aaa03254323d1ad",
                "sha256:c2fa989035223ec633a53ca0a557e6272103187411a5a2bf394fe27f55cb191b",
                "sha256:7483f924f7529904ea2cd84072f0ebd3bd200b974aaaf14bfb5bd547026a0ee3",
                "sha256:aa70623a5747f22f4072ae0697286feeae4e59d684b934bff1ada7df1960dbee",
                "sha256:8f8d6a24a5f1876a6f97b6af4c10af7302372dbb6f92216e01a4d98276386c4b",
                "sha256:37659df06d3e1c46605bd735f85b1c943e61f59483ce719dcbbb2ae48bfdfe80",
                "sha256:b440f9ae1d4caa4451ca6518082d85c114e1739191dc46077210a0ec3f2d198e",
                "sha256:267d60806e3ed912c3418f6a9da30058667d3b7b2a22ca2f4c8c4fab4200db9f",
                "sha256:b2cb1046d1ea9e0f935e9ab56241404bbfd8c4ed566da5b2bd45879bf3a8f622"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

10、docker logs 查看容器日志

docker logs 容器ID

11、docker commit 制作镜像

docker commit -a="lonely" -m="test commit" 原容器id  lonely/mytomcat:1.0

commit:提交镜像,将容器id对应的镜像修改后,使用commit生产 自定义镜像
lonely/mytomcat 表示镜像名
1.0 : 表示镜像版本
-a= : 表明作者
-m=:新镜像的描述信息

你可能感兴趣的:(docker)