Docker入門與實踐-Docker基本命令

實驗環(huán)境

阿里云主機:CentOS 7.4 (64位)
主機配置:2核8G (搶占式實例筒严,0.07元/每小時)

1、查看Linux操作系統(tǒng)信息(使用常用的各種命令)

[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 (builder@kbuilder.dev.centos.org) (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容器系統(tǒng)

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

10、查看啟動了nginx的Docker容器系統(tǒng)中的操作系統(tǒng)信息

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 (builder@kbuilder.dev.centos.org) (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系統(tǒng)之前的操作系統(tǒng)信息如下

[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 (builder@kbuilder.dev.centos.org) (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的基本操作方法及原理

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末校坑,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子千诬,更是在濱河造成了極大的恐慌耍目,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,884評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件徐绑,死亡現(xiàn)場離奇詭異邪驮,居然都是意外死亡,警方通過查閱死者的電腦和手機傲茄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,755評論 3 385
  • 文/潘曉璐 我一進店門毅访,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沮榜,“玉大人,你說我怎么就攤上這事喻粹◇∪冢” “怎么了?”我有些...
    開封第一講書人閱讀 158,369評論 0 348
  • 文/不壞的土叔 我叫張陵守呜,是天一觀的道長型酥。 經(jīng)常有香客問我,道長查乒,這世上最難降的妖魔是什么弥喉? 我笑而不...
    開封第一講書人閱讀 56,799評論 1 285
  • 正文 為了忘掉前任,我火速辦了婚禮玛迄,結果婚禮上由境,老公的妹妹穿的比我還像新娘。我一直安慰自己憔晒,他們只是感情好藻肄,可當我...
    茶點故事閱讀 65,910評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拒担,像睡著了一般嘹屯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上从撼,一...
    開封第一講書人閱讀 50,096評論 1 291
  • 那天州弟,我揣著相機與錄音,去河邊找鬼低零。 笑死婆翔,一個胖子當著我的面吹牛,可吹牛的內容都是我干的掏婶。 我是一名探鬼主播啃奴,決...
    沈念sama閱讀 39,159評論 3 411
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼雄妥!你這毒婦竟也來了最蕾?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,917評論 0 268
  • 序言:老撾萬榮一對情侶失蹤老厌,失蹤者是張志新(化名)和其女友劉穎瘟则,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體枝秤,經(jīng)...
    沈念sama閱讀 44,360評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡醋拧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,673評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丹壕。...
    茶點故事閱讀 38,814評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡庆械,死狀恐怖,靈堂內的尸體忽然破棺而出雀费,到底是詐尸還是另有隱情干奢,我是刑警寧澤,帶...
    沈念sama閱讀 34,509評論 4 334
  • 正文 年R本政府宣布盏袄,位于F島的核電站忿峻,受9級特大地震影響,放射性物質發(fā)生泄漏辕羽。R本人自食惡果不足惜逛尚,卻給世界環(huán)境...
    茶點故事閱讀 40,156評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望刁愿。 院中可真熱鬧绰寞,春花似錦、人聲如沸铣口。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脑题。三九已至件缸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間叔遂,已是汗流浹背他炊。 一陣腳步聲響...
    開封第一講書人閱讀 32,123評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留已艰,地道東北人痊末。 一個月前我還...
    沈念sama閱讀 46,641評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像哩掺,于是被迫代替她去往敵國和親凿叠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,728評論 2 351

推薦閱讀更多精彩內容

  • 一嚼吞、docker介紹 docker的優(yōu)勢 1盒件、更高效的利用系統(tǒng)資源由于容器不需要進行硬件虛擬以及運行完整操作系統(tǒng)等...
    Mr_Du_Biao閱讀 662評論 0 0
  • Docker入門 Docker命令官方示例 [toc] 1. Docker架構 1.1 Docker鏡像與Dock...
    CaiGuangyin閱讀 1,835評論 2 6
  • 1 Docker簡介 1.1 什么是虛擬化 ? 在計算機中,虛擬化(英語:Virtualization)是一種...
    hyperdebug閱讀 312評論 0 1
  • 課程目標 [ ] Mac Pro 上如何安裝Docker[ ] 概念:Docker 引擎誊薄,宿主機,鏡像(Image...
    區(qū)影閱讀 6,100評論 5 12
  • 2015-08-06 22:41:31 “對死后不足三十年的作家锰茉,原則上是不屑一顧的呢蔫。那種書不足為信”。 "不是說...
    ypxgxf閱讀 320評論 0 0