Docker基礎篇——容器的簡單操作

確保docker已經就緒

[root@localhost docker]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.8
 Storage Driver: devicemapper
  Pool Name: docker-253:0-69362259-pool
  Pool Blocksize: 65.54kB
  Base Device Size: 10.74GB
  Backing Filesystem: xfs
  Udev Sync Supported: true
  Data file: /dev/loop0
  Metadata file: /dev/loop2
  Data loop file: /var/lib/docker/devicemapper/devicemapper/data
  Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
  Data Space Used: 10.94MB
  Data Space Total: 107.4GB
  Data Space Available: 9.634GB
  Metadata Space Used: 581.6kB
  Metadata Space Total: 2.147GB
  Metadata Space Available: 2.147GB
  Thin Pool Minimum Free Space: 10.74GB
  Deferred Removal Enabled: true
  Deferred Deletion Enabled: true
  Deferred Deleted Device Count: 0
  Library Version: 1.02.158-RHEL7 (2019-05-13)
 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-123.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 979.9MiB
 Name: localhost.localdomain
 ID: Y3EJ:KMOB:KWLB:6D3L:XBS6:RG2H:MPHN:WIIG:AE6A:KJPT:Q3LR:5LKH
 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
 Live Restore Enabled: false

運行第一個容器

sudo docker run -i -t redhat /bin/bash
  • -i :保證容器中STDIN是開啓的;
  • -t:告訴docker爲要創建的容器分配一個爲tty終端;
  • redhat:說明使用的是redhat鏡像,這是一個基礎鏡像,docker會檢查本地是否存在redhat鏡像,如果不存在則會從Docker Hub上下載並保存。
  • docker在文件系統內部使用這個鏡像創建一個新的容器,該容器擁有自己的網略、IP地址,以及一個用來和宿主機進行通信的橋接網絡接口,在容器中運行/bin/bash啓動一個Bash shell。
[root@localhost docker]# docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
5bed26d33875: Pulling fs layer 
f11b29a9c730: Downloading 
930bda195c84: Download complete 
78bf9a5ad49e: Download complete 
docker: Get https://registry-1.docker.io/v2/library/ubuntu/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fubuntu%3Apull&service=registry.docker.io: net/http: TLS handshake timeout.
See 'docker run --help'.

請求超時,可能使鏡像沒選對或者網速太慢,修改鏡像源,使用阿里雲鏡像源;
/etc/docker/daemon.json

{
"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"]
}

重新啓動docker

systemctl restart docker.service 
docker run -i -t ubuntu /bin/bash

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
5bed26d33875: Pull complete 
f11b29a9c730: Pull complete 
930bda195c84: Pull complete 
78bf9a5ad49e: Pull complete 
Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d
Status: Downloaded newer image for ubuntu:latest

使用第一個容器

退出容器

exit

查看宿主機當前系統容器列表

docker ps -a # 查看所有容器
docker ps # 只能查看正在運行的容器

容器命令

docker會給創建的容器自動生成一個隨機的名稱,也可以指定一個名稱:

[root@localhost docker]# docker run --name bob_the_container -i -t ubuntu /bin/bash
root@4f43e992c2bc:/# exit
exit
[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
4f43e992c2bc        ubuntu              "/bin/bash"         15 seconds ago      Exited (0) 2 seconds ago                         bob_the_container
81014d1bbf16        ubuntu              "/bin/bash"         12 minutes ago      Exited (100) 4 minutes ago                       wonderful_wilson

一個合法的容器名只能包含以下字符:a-z,A-Z,0-9,下劃線,圓點,橫線。容器命名必須是唯一的,如果想要使用容器名稱已經存在,則可以用docker rm命令刪除已有的同名容器後再來創建。

重新啓動已經停止的容器

docker start bob_the_container
# 或者使用容器id來指定
docker start 4f43e992c2bc

附着到容器上

重新啓動後會運行一個交互式會話shell,此外也可以使用docker attach 容器名/容器id重新附着到該容器的會話上;

創建守護式容器

docker run --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
a684b624df6257b8be73ef098ec03aa78aaecf8ae16b4535df37514d0fb1fccb

docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
a684b624df62        ubuntu              "/bin/sh -c 'while t…"   21 seconds ago      Up 18 seconds                                     daemon_dave
4f43e992c2bc        ubuntu              "/bin/bash"              10 minutes ago      Exited (0) 10 minutes ago                         bob_the_container
81014d1bbf16        ubuntu              "/bin/bash"              22 minutes ago      Exited (100) 15 minutes ago                       wonderful_wilson

容器內部都在幹什麼

[root@localhost docker]# docker logs daemon_dave
hello world
hello world
hello world
hello world
hello world
hello world
hello world

# 可以使用-f來監控docker日誌
 docker logs -f  daemon_dave
# 使用-t 爲每條日誌加上時間戳
 docker logs -ft  daemon_dave

查看容器內的進程

 docker top daemon_dave
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                84752               84733               0                   17:15               ?                   00:00:00            /bin/sh -c while true; do echo hello world; sleep 1; done
root                85219               84752               0                   17:20               ?                   00:00:00            sleep 1

停止守護式容器

docker stop daemon_dave

自動重新啓動容器

docker run --restart=always --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

標誌on-failure表示只有當容器的退出代碼爲非0值的時候纔會自動啓動,還可以設置重啓次數:

# 當容器退出代碼爲非0時,最多重啓5次
--restart=on-failure:5

深入容器

可以使用docker inspect來獲取更多的容器信息

[root@localhost docker]# docker inspect daemon_dave
[
    {
        "Id": "a684b624df6257b8be73ef098ec03aa78aaecf8ae16b4535df37514d0fb1fccb",
        "Created": "2020-04-04T10:15:21.563956999Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true; do echo hello world; sleep 1; done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2020-04-04T10:15:23.317426519Z",
            "FinishedAt": "2020-04-04T10:22:17.086870381Z"
        },

也可以使用-f或者--format標誌來選定查看結果:

[root@localhost docker]# docker inspect --format='{{ .State.Running }}' daemon_dave
false
[root@localhost docker]# docker inspect --format='{{ .NetworkSettings.IPAddress }}' daemon_dave

刪除容器

docker rm 容器id/容器名

如何刪除所有容器

docker rm `docker ps -a -q`
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章