Docker入门与实践-Docker基本命令

实验环境

阿里云主机:CentOS 7.4 (64位)
主机配置:2核8G (抢占式实例,0.07元/每小时)

1、查看Linux操作系统信息(使用常用的各种命令)

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) 
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# uname -r
3.10.0-693.2.2.el7.x86_64
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /proc/version
Linux version 3.10.0-693.2.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Sep 12 22:26:13 UTC 2017
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core) 
Release:        7.4.1708
Codename:       Core
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

2、安装Docker

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# yum install -y docker 
Loaded plugins: fastestmirror
base                                                                                                                                                                                                                                     | 3.6 kB  00:00:00     
epel                                                                                                                                                                                                                                     | 5.3 kB  00:00:00     
extras                                                                                                                                                                                                                                   | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                                                  | 2.9 kB  00:00:00     
(1/5): epel/x86_64/group_gz                                                                                                                                                                                                              |  95 kB  00:00:00     
(2/5): extras/7/x86_64/primary_db                                                                                                                                                                                                        | 164 kB  00:00:00     
(3/5): epel/x86_64/updateinfo                                                                                                                                                                                                            | 1.0 MB  00:00:00     
(4/5): epel/x86_64/primary_db                                                                                                                                                                                                            | 6.7 MB  00:00:00     
(5/5): updates/7/x86_64/primary_db                                                                                                                                                                                                       | 7.5 MB  00:00:00     
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
Installed:
  docker.x86_64 2:1.13.1-109.gitcccb291.el7.centos                                                                                                                                                                                                              

Dependency Installed:
  PyYAML.x86_64 0:3.10-11.el7                               atomic-registries.x86_64 1:1.22.1-29.gitb507039.el7                       audit-libs-python.x86_64 0:2.8.5-4.el7                       checkpolicy.x86_64 0:2.5-8.el7                              
  container-selinux.noarch 2:2.107-3.el7                    container-storage-setup.noarch 0:0.11.0-2.git5eaf76c.el7                  containers-common.x86_64 1:0.1.37-3.el7.centos               device-mapper-event.x86_64 7:1.02.158-2.el7_7.2             
  device-mapper-event-libs.x86_64 7:1.02.158-2.el7_7.2      device-mapper-persistent-data.x86_64 0:0.8.5-1.el7                        docker-client.x86_64 2:1.13.1-109.gitcccb291.el7.centos      docker-common.x86_64 2:1.13.1-109.gitcccb291.el7.centos     
  libaio.x86_64 0:0.3.109-13.el7                            libcgroup.x86_64 0:0.41-21.el7                                            libsemanage-python.x86_64 0:2.5-14.el7                       libyaml.x86_64 0:0.1.4-11.el7_0                             
  lvm2.x86_64 7:2.02.185-2.el7_7.2                          lvm2-libs.x86_64 7:2.02.185-2.el7_7.2                                     oci-register-machine.x86_64 1:0-6.git2b44233.el7             oci-systemd-hook.x86_64 1:0.2.0-1.git05e6923.el7_6          
  oci-umount.x86_64 2:2.5-3.el7                             policycoreutils-python.x86_64 0:2.5-33.el7                                python-IPy.noarch 0:0.75-6.el7                               python-pytoml.noarch 0:0.1.14-1.git7dea353.el7              
  setools-libs.x86_64 0:3.3.8-4.el7                         subscription-manager-rhsm-certificates.x86_64 0:1.24.13-4.el7.centos      yajl.x86_64 0:2.0.4-4.el7                                   

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7                 audit-libs.x86_64 0:2.8.5-4.el7                     device-mapper.x86_64 7:1.02.158-2.el7_7.2 device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2 libsemanage.x86_64 0:2.5-14.el7 policycoreutils.x86_64 0:2.5-33.el7
  selinux-policy.noarch 0:3.13.1-252.el7_7.6 selinux-policy-targeted.noarch 0:3.13.1-252.el7_7.6

Complete!
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

3、启动Docker

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# systemctl start docker
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-03-20 19:47:53 CST; 22s ago
     Docs: http://docs.docker.com
 Main PID: 2000 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─2000 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=...
           └─2006 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime dock...

Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.627133923+08:00" level=warning msg="Docker could not enable SELinux on the host system"
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.654806116+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.655352429+08:00" level=info msg="Loading containers: start."
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.702315029+08:00" level=info msg="Firewalld running: false"
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.775269517+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.823104888+08:00" level=info msg="Loading containers: done."
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.833924962+08:00" level=info msg="Daemon has completed initialization"
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.833945271+08:00" level=info msg="Docker daemon" commit="cccb291/1.13.1" graphdriver=overlay2 version=1.13.1
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ systemd[1]: Started Docker Application Container Engine.
Mar 20 19:47:53 iZ8vb1ut8fdqv17chxclgkZ dockerd-current[2000]: time="2020-03-20T19:47:53.846962380+08:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.

4、查看Docker版本

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      cccb291/1.13.1
 Built:           Tue Mar  3 17:21:24 2020
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      cccb291/1.13.1
 Built:           Tue Mar  3 17:21:24 2020
 OS/Arch:         linux/amd64
 Experimental:    false
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

5、配置DaoCloud 加速器

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /etc/docker/daemon.json
{}
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
docker version >= 1.12
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],}
Success.
You need to restart docker to take effect: sudo systemctl restart docker 
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /etc/docker/daemon.json
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],}
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

该脚本可以将 --registry-mirror 加入到你的 Docker 配置文件 /etc/docker/daemon.json 中。适用于 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1,其他版本可能有细微不同。更多详情请访问文档https://www.daocloud.io/mirror#accelerator-doc。

6、下载nginx镜像

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker search nginx
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                              Official build of Nginx.                        12840     [OK]       
docker.io   docker.io/jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1754                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   760                  [OK]
docker.io   docker.io/linuxserver/nginx                  An Nginx container, brought to you by Linu...   97                   
docker.io   docker.io/bitnami/nginx                      Bitnami nginx Docker Image                      78                   [OK]
docker.io   docker.io/tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rt...   64                   [OK]
docker.io   docker.io/jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ...   46                   
docker.io   docker.io/nginxdemos/hello                   NGINX webserver that serves a simple page ...   42                   [OK]
docker.io   docker.io/nginx/unit                         NGINX Unit is a dynamic web and applicatio...   36                   
docker.io   docker.io/jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        35                   [OK]
docker.io   docker.io/nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         28                   
docker.io   docker.io/privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & ...   23                   [OK]
docker.io   docker.io/schmunk42/nginx-redirect           A very simple container to redirect HTTP t...   18                   [OK]
docker.io   docker.io/blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                   [OK]
docker.io   docker.io/nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  13                   
docker.io   docker.io/centos/nginx-112-centos7           Platform for running nginx 1.12 or buildin...   12                   
docker.io   docker.io/centos/nginx-18-centos7            Platform for running nginx 1.8 or building...   12                   
docker.io   docker.io/raulr/nginx-wordpress              Nginx front-end for the official wordpress...   12                   [OK]
docker.io   docker.io/nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       9                    
docker.io   docker.io/sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats...   7                    [OK]
docker.io   docker.io/mailu/nginx                        Mailu nginx frontend                            6                    [OK]
docker.io   docker.io/bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Con...   4                    [OK]
docker.io   docker.io/ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                    [OK]
docker.io   docker.io/centos/nginx-110-centos7           Platform for running nginx 1.10 or buildin...   0                    
docker.io   docker.io/wodby/nginx                        Generic nginx                                   0                    [OK]
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ... 
latest: Pulling from docker.io/library/nginx
68ced04f60ab: Pull complete 
28252775b295: Pull complete 
a616aa3b0bf2: Pull complete 
Digest: sha256:2539d4344dd18e1df02be842ffc435f8e1f699cfc55516e2cf2cb16b7a9aea0b
Status: Downloaded newer image for docker.io/nginx:latest
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              6678c7c2e56c        2 weeks ago         127 MB
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

7、启动nginx镜像

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker run -p 8000:80 --name my_nginx -d nginx 
e2fbd26d4a818a532a3d636181f93251000593ebf06d611f45ae0076a6798f49
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
e2fbd26d4a81        nginx               "nginx -g 'daemon ..."   9 seconds ago       Up 9 seconds        0.0.0.0:8000->80/tcp   my_nginx
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

8、通过浏览器访问主机的8000端口

Host主机的8000端口映射到了nginx容器的80端口

9、进入启动了nginx的Docker容器系统

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# docker exec -it  my_nginx /bin/bash
root@e2fbd26d4a81:/# 

10、查看启动了nginx的Docker容器系统中的操作系统信息

root@e2fbd26d4a81:/# cat /etc/redhat-release
cat: /etc/redhat-release: No such file or directory
root@e2fbd26d4a81:/# uname -r
3.10.0-693.2.2.el7.x86_64
root@e2fbd26d4a81:/# cat /proc/version
Linux version 3.10.0-693.2.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Sep 12 22:26:13 UTC 2017
root@e2fbd26d4a81:/# lsb_release -a
bash: lsb_release: command not found
root@e2fbd26d4a81:/# cat /etc/issue
Debian GNU/Linux 10 \n \l
root@e2fbd26d4a81:/# 

进入Docker系统之前的操作系统信息如下

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) 
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# uname -r
3.10.0-693.2.2.el7.x86_64
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /proc/version
Linux version 3.10.0-693.2.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Sep 12 22:26:13 UTC 2017
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core) 
Release:        7.4.1708
Codename:       Core
[root@iZ8vb1ut8fdqv17chxclgkZ ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@iZ8vb1ut8fdqv17chxclgkZ ~]# 

大家对比一下,能得到什么结论呢?
相信通过这些基本命令,可以帮助你更详细的了解Docker的基本操作方法及原理

你可能感兴趣的:(Docker入门与实践-Docker基本命令)