1. 程式人生 > >Docker入門實操-學習筆記

Docker入門實操-學習筆記

iproute weixin 格式 cti 文件包含 mir unit dockerhub 運行

一、安裝級運行docker

  1. 安裝
    [root@localhost ~]# curl -sSL https://get.daocloud.io/docker | sh

  2. 運行
    [root@localhost ~]# which docker
    /usr/bin/docker
    [root@localhost ~]# docker version
    Client:
    Version:           18.06.0-ce
    API version:       1.38
    Go version:        go1.10.3
    Git commit:        0ffa825
    Built:             Wed Jul 18 19:08:18 2018
    OS/Arch:           linux/amd64
    Experimental:      false
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

報錯

[root@localhost ~]# systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since 二 2018-07-31 11:29:01 CST; 46s ago
     Docs: https://docs.docker.com
  Process: 35176 ExecStart=/usr/bin/dockerd (code=exited, status=1/FAILURE)
 Main PID: 35176 (code=exited, status=1/FAILURE)

7月 31 11:29:01 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
7月 31 11:29:01 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
7月 31 11:29:01 localhost.localdomain systemd[1]: docker.service failed.
7月 31 11:29:01 localhost.localdomain systemd[1]: docker.service holdoff time over, scheduling restart.
7月 31 11:29:01 localhost.localdomain systemd[1]: start request repeated too quickly for docker.service
7月 31 11:29:01 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
7月 31 11:29:01 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
7月 31 11:29:01 localhost.localdomain systemd[1]: docker.service failed.

這個可以通過如下方式解決:
查看文件系統 /etc/docker/daemon.json 有沒有這個文件,沒有測創建它包括二級目錄 docker
在daemon.json文件中輸入以下內容:

{

  "storage-driver"
  :

  "devicemapper"

 }

如果daemon.json文件包含格式不正確的JSON,Docker將無法啟動。

再次啟動docker,正常

[root@localhost ~]# systemctl start docker

查看狀態
[root@localhost ~]# docker version
Client:
 Version:           18.06.0-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        0ffa825
 Built:             Wed Jul 18 19:08:18 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.0-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       0ffa825
  Built:            Wed Jul 18 19:10:42 2018
  OS/Arch:          linux/amd64
  Experimental:     false

3 查看幫助

docker help

4 查看本地倉庫的鏡像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

5 在DockerHub 上搜索共享鏡像

[root@localhost ~]# docker search centos7
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ansible/centos7-ansible           Ansible on Centos7                              114                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   35                                      
openshift/base-centos7            A Centos7 derived base image for Source-To-I…   29
(省略)

6 拉取共享鏡像
在拉取前需要更改/etc/docker/daemon.json(國內訪問dockerhub速度你懂得,在這裏使用了ustc的鏡像加速)

[root@localhost ~]# more /etc/docker/daemon.json
{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}

[root@localhost ~]# docker pull docker.io/ansible/centos7-ansible
Using default tag: latest
latest: Pulling from ansible/centos7-ansible
45a2e645736c: Pull complete 
1c3acf573616: Pull complete 
edcb61e55ccc: Pull complete 
cbae31bad30a: Pull complete 
aacbdb1e2a62: Pull complete 
fdeea4fb835c: Pull complete 
Digest: sha256:39eff7d56b96530d014083cd343f7314c23acbd1ecf37eb75a71a2f6584d0b02
Status: Downloaded newer image for ansible/centos7-ansible:latest

7.再次查看本地倉庫:

[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
ansible/centos7-ansible   latest              688353a31fde        19 months ago       447MB

二、運行容器

  1. 運行一個名字為ws的容器,並獲取一個shell;
    [root@localhost ~]# docker run -it --name=ws docker.io/ansible/centos7-ansible /bin/bash
    [root@ca8bd201ee4b ansible]#

    此時我們進入ws容器的shell,在這裏可以執行一些基本命令。
    安裝ip命令

    [root@ca8bd201ee4b ansible]# yum -y install iproute
    Loaded plugins: fastestmirror, ovl
    Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
    base                                                                               | 3.6 kB  00:00:00     
    epel/x86_64/metalink                                                               | 5.8 kB  00:00:00     
    epel                                                                               | 3.2 kB  00:00:00     
    extras                                                                             | 3.4 kB  00:00:00     
    updates                                                                            | 3.4 kB  00:00:00     
    (1/7): base/7/x86_64/group_gz                                                      | 166 kB  00:00:00     
    (2/7): epel/x86_64/group_gz                                                        |  88 kB  00:00:00     
    (3/7): extras/7/x86_64/primary_db                                                  | 172 kB  00:00:00     
    (4/7): epel/x86_64/updateinfo                                                      | 930 kB  00:00:01     
    (5/7): epel/x86_64/primary                                                         | 3.6 MB  00:00:01     
    (6/7): base/7/x86_64/primary_db                                                    | 5.9 MB  00:00:02     
    (7/7): updates/7/x86_64/primary_db            
    (省略)
    [root@ca8bd201ee4b ansible]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    24: eth0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

    看著和在宿主機上沒有區別?

從docker裏面可以直接進入物理機

[root@ca8bd201ee4b ansible]# ssh 192.168.10.138
The authenticity of host ‘192.168.10.138 (192.168.10.138)‘ can‘t be established.
ECDSA key fingerprint is 0c:23:1d:1b:c7:95:e8:18:b4:4c:6d:de:98:03:9e:6e.
Are you sure you want to continue connecting (yes/no)?

2.離開容器(ctrl+p+q)

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
ca8bd201ee4b        ansible/centos7-ansible   "/bin/bash"         2 hours ago         Up 2 hours                              ws

容器ID 使用鏡像 執行命令 創建時間 名字

  1. 連接正在運行的容器

    [root@localhost ~]# docker attach ws
    [root@ca8bd201ee4b ansible]#
  2. 退出容器
    [root@ca8bd201ee4b ansible]# exit
    exit
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

退出容器後,docker ps 已經看不到,使用-a參數

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                            PORTS               NAMES
ca8bd201ee4b        ansible/centos7-ansible   "/bin/bash"         2 hours ago         Exited (255) About a minute ago                       ws
  1. 啟動一個退出的容器
    [root@localhost ~]# docker start ws
    ws
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
    ca8bd201ee4b        ansible/centos7-ansible   "/bin/bash"         2 hours ago         Up 6 seconds                            ws

6 使用宿主機停止容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
ca8bd201ee4b        ansible/centos7-ansible   "/bin/bash"         2 hours ago         Up About a minute                       ws
[root@localhost ~]# docker stop ws
ws
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  1. 刪除容器(先停止再刪除)
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                        PORTS               NAMES
    ca8bd201ee4b        ansible/centos7-ansible   "/bin/bash"         2 hours ago         Exited (137) 46 seconds ago                       ws
    [root@localhost ~]# docker rm ca8bd201ee4b
    ca8bd201ee4b

參考https://mp.weixin.qq.com/s/sqsOraQmjNEP4OSPJi8g8Q

Docker入門實操-學習筆記