docker⼊门知识总结
DOCKER
1.概述
1.1 为什么要使⽤docker
每次发布项⽬都要重新部署服务器,是否已经让你厌恶?项⽬发布能不能连同运⾏环境⼀起⾃动化部署?为了解决这个问题,docker出现了。
docker的灵感来⾃于集装箱,重点在于隔离机制,通过隔离机制能做到避免多个应⽤的端⼝冲突,实现单台服务器部署多个运⾏环境。
1.2 docker历史
诞⽣于2010年,⼏个搞IT的热⾎青年⾛到⼀起创办了dotCloud公司;
起初由于代码不开源,⾏业内并没有太多⼈使⽤docker;
2013年,docker决定开放源代码,随后使⽤docker的⼈越来越多;
docker是⼀种虚拟技术,但⼜不同于VMware,它是⼀种容器技术,⽐虚拟机要轻量得多。
1.3 ⽂档地址和仓库地址
2.安装
2.1 Docker基本组成
2.2 基本概念
镜像(Image)docker镜像好⽐是⼀个模板,可以通过这个模板来创建容器服务,通过这个镜像可以创建多个容器(最终服务运⾏或者项⽬运⾏就是在容器中的)。
容器(container)docker利⽤容器技术,独⽴运⾏⼀个或者⼀组应⽤,通过镜像来创建。
仓库(repository)存放镜像的地⽅,分为公有仓库和私有仓库。
2.3 安装步骤
环境:centOS 7
# 以下内容为操作系统具体信息
uname -r
3.10.0-1160.el7.x86_64
cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
# 1.卸载旧版本docker
[root@localhost ~]# yum remove docker \
>                  docker-client \
>                  docker-client-latest \
>                  docker-common \
>                  docker-latest \
>                  docker-latest-logrotate \
>                  docker-logrotate \
>                  docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
# 2.安装yum相关的安装包
[root@localhost lib]# yum install yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun
* extras: mirrors.aliyun
* updates: mirrors.aliyun
Package yum-utils-1.1.31-54.arch already installed and latest version
Nothing to do
# 3.设置镜像仓库(此处最好⽤国内镜像,国外镜像速度不稳定)
[root@localhost lib]# yum-config-manager \
> --add-repo \
> mirrors.aliyun/docker-ce/linux/po
Loaded plugins: fastestmirror
adding repo from: mirrors.aliyun/docker-ce/linux/po
grabbing file mirrors.aliyun/docker-ce/linux/po to /pos.po
repo saved to /pos.po
# 4.安装docker相关安装包
[root@localhost lib]# yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun
* extras: mirrors.aliyun
* updates: mirrors.aliyun
docker-ce-stable                                                                                                                                        | 3.5 kB  00:00:00
(1/2): docker-ce-stable/7/x86_64/primary_db                                                                                                              |  55 kB  00:00:03
(2/2): docker-ce-stable/7/x86_64/updateinfo                                                                                                              |  55 B  00:00:03
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.4.3-3.1.el7 will be installed
-
-> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.4.3-3.1.el7.x86_64
--> Processing Dependency: libseccomp for package: containerd.io-1.4.3-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:20.10.3-3.el7 will be installed
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-20.10.3-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:20.10.3-3.el7 will be installed
--> Running transaction check
---> arch 2:2.119.2-1.911c772.el7_8 will be installed
---> Package docker-ce-rootless-extras.x86_64 0:20.10.3-3.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-20.10.3-3.el7.x86_64
-
-> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-20.10.3-3.el7.x86_64
---> Package libseccomp.x86_64 0:2.3.1-4.el7 will be installed
--> Running transaction check
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> Running transaction check
-
--> Package fuse3-libs.x86_64 0:3.6.1-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================================================================================================ Package                                          Arch                          Version                                          Repository                              Size
================================================================================================================================================================================ Installing:
containerd.io                                    x86_64                        1.4.3-3.1.el7                                    docker-ce-stable                        33 M
docker-ce                                        x86_64                        3:20.10.3-3.el7                                  docker-ce-stable                        27 M
docker-ce-cli                                    x86_64                        1:20.10.3-3.el7                                  docker-ce-stable                        33 M
Installing for dependencies:
container-selinux                                noarch                        2:2.119.2-1.911c772.el7_8                        extras                                  40 k
docker-ce-rootless-extras                        x86_64                        20.10.3-3.el7                                    docker-ce-stable                        9.0 M
fuse-overlayfs                                  x86_64                        0.7.2-6.el7_8                                    extras                                  54 k
fuse3-libs                                      x86_64                        3.6.1-4.el7                                      extras                                  82 k
libseccomp                                      x86_64                        2.3.1-4.el7                                      base                                    56 k
slirp4netns                                      x86_64                        0.4.3-4.el7_8                                    extras                                  81 k
Transaction Summary
================================================================================================================================================================================ Install  3 Packages (+6 Dependent packages)
Total download size: 102 M
Installed size: 423 M
Is this ok [y/d/N]: y
Downloading packages:
(1/9): container-selinux-2.119.2-1.911c772.arch.rpm                                                                                              |  40 kB  00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-20.10.3-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEYB/s |  30 MB  00:00:20 ETA
Public key for docker-ce-20.10.3-3.el7.x86_64.rpm is not installed
(2/9): docker-ce-20.10.3-3.el7.x86_64.rpm                                                                                                                |  27 MB  00:00:08
(3/9): docker-ce-cli-20.10.3-3.el7.x86_64.rpm                                                                                                            |  33 MB  00:00:09
(4/9): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm                                                                                                          |  54 kB  00:00:00
(5/9): slirp4netns-0.4.3-4.el7_8.x86_64.rpm                                                                                                              |  81 kB  00:00:00
(6/9): fuse3-libs-3.6.1-4.el7.x86_64.rpm                                                                                                                |  82 kB  00:00:00
(7/9): libseccomp-2.3.1-4.el7.x86_64.rpm                                                                                                                |  56 kB  00:00:00
(8/9): docker-ce-rootless-extras-20.10.3-3.el7.x86_64.rpm                                                                                                | 9.0 MB  00:00:02
(9/9): containerd.io-1.4.3-3.1.el7.x86_64.rpm                                                                                                            |  33 MB  00:00:25
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                          4.0 MB/s | 102 MB  00:00:25
Retrieving key from download.docker/linux/centos/gpg
Importing GPG key 0x621E9F35:
Userid    : "Docker Release (CE rpm) <docker@docker>"
Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
From      : download.docker/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libseccomp-2.3.1-4.el7.x86_64                                                                                                                                1/9
Installing : 2:container-selinux-2.119.2-1.911c772.arch                                                                                                          2/9
Installing : containerd.io-1.4.3-3.1.el7.x86_64                                                                                                                          3/9
Installing : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                                                            4/9
Installing : 1:docker-ce-cli-20.10.3-3.el7.x86_64                                                                                                                        5/9
Installing : fuse3-libs-3.6.1-4.el7.x86_64                                                                                                                                6/9
Installing : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                                                          7/9
Installing : 3:docker-ce-20.10.3-3.el7.x86_64                                                                                                                            8/9
Installing : docker-ce-rootless-extras-20.10.3-3.el7.x86_64                                                                                                              9/9
Verifying  : fuse3-libs-3.6.1-4.el7.x86_64                                                                                                                                1/9
Verifying  : 3:docker-ce-20.10.3-3.el7.x86_64                                                                                                                            2/9
Verifying  : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                                                          3/9
Verifying  : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                                                            4/9
Verifying  : 2:container-selinux-2.119.2-1.911c772.arch                                                                                                          5/9
Verifying  : libseccomp-2.3.1-4.el7.x86_64                                                                                                                                6/9
Verifying  : containerd.io-1.4.3-3.1.el7.x86_64                                                                                                                          7/9
Verifying  : docker-ce-rootless-extras-20.10.3-3.el7.x86_64                                                                                                              8/9
Verifying  : 1:docker-ce-cli-20.10.3-3.el7.x86_64                                                                                                                        9/9
Installed:
containerd.io.x86_64 0:1.4.3-3.1.el7                      docker-ce.x86_64 3:20.10.3-3.el7                      docker-ce-cli.x86_64 1:20.10.3-3.el7
Dependency Installed:
libseccomp.x86_64 0:2.3.1-4.el7                    slirp4netns.x86_64 0:0.4.3-4.el7_8
Complete!
# 5.查看docker 版本
[root@localhost lib]# docker version
Client: Docker Engine - Community
Version:          20.10.3
API version:      1.41
Go version:        go1.13.15
Git commit:        48d30b5
Built:            Fri Jan 29 14:34:14 2021
OS/Arch:          linux/amd64
Context:          default
Experimental:      true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# 6.启动docker
[root@localhost lib]# systemctl start docker
# 7.运⾏hello-world镜像
[root@localhost lib]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:95ddb6c31407e84e91a986b004aee40975cb0bda14b5949f6faac5d2deadb4b9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
hub.docker/
For more examples and ideas, visit:
docs.docker/get-started/
# 8.查看hello-world镜像
[root@localhost docker]# docker images
REPOSITORY    TAG      IMAGE ID      CREATED        SIZE
hello-world  latest    bf756fb1ae65  13 months ago  13.3kB
如需卸载可执⾏以下命令
yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
2.4 docker为什么⽐虚拟机快
1. docker有着⽐虚拟机更少的抽象层
2. docker利⽤宿主机的内核,⽽VMware相当于重装⼀台电脑
3.命令
3.1 帮助命令
[root@localhost ~]# docker info
Client:
Context:    default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
Server:
Containers: 2
Running: 0
Paused: 0
Stopped: 2
Images: 1
Server Version: 20.10.3
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
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: io.containerd.runc.ainerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 991.3MiB
Name: localhost.localdomain
ID: J6FX:JPAO:CN55:Z27Y:35FS:7LIE:NNJX:FDPN:REID:74WW:GT66:OZJM
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version:          20.10.3
API version:      1.41
Go version:        go1.13.15
Git commit:        48d30b5
Built:            Fri Jan 29 14:34:14 2021
OS/Arch:          linux/amd64
Context:          default
Experimental:      true
Server: Docker Engine - Community
Engine:
Version:          20.10.3
API version:      1.41 (minimum version 1.12)
Go version:      go1.13.15
Git commit:      46229ca
Built:            Fri Jan 29 14:32:37 2021
OS/Arch:          linux/amd64
Experimental:    false
containerd:
Version:          1.4.3
GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version:          1.0.0-rc92
GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version:          0.19.0
GitCommit:        de40ad0
[root@localhost ~]# docker --help
Usage:  docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string      Location of client config files (default "/root/.docker")
-c, --context string    Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")  -D, --debug              Enable debug mode
-H, --host list          Daemon socket(s) to connect to
-l, --log-level string  Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls                Use TLS; implied by --tlsverify
--tlscacert string  Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string    Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify          Use TLS and verify the remote
-v, --version            Print version information and quit
Management Commands:
app*        Docker App (Docker Inc., v0.9.1-beta3)
builder    Manage builds
buildx*    Build with BuildKit (Docker Inc., v0.5.1-docker)
config      Manage Docker configs
container  Manage containers
context    Manage contexts
image      Manage images
manifest    Manage Docker image manifests and manifest lists
network    Manage networks
node        Manage Swarm nodes
plugin      Manage plugins
secret      Manage Docker secrets
service    Manage services
stack      Manage Docker stacks
swarm      Manage Swarm
system      Manage Docker
trust      Manage trust on Docker images
volume      Manage volumes
Commands:
attach      Attach local standard input, output, and error streams to a running container
build      Build an image from a Dockerfile
commit      Create a new image from a container's changes
cp          Copy files/folders between a container and the local filesystem
create      Create a new container
diff        Inspect changes to files or directories on a container's filesystem
events      Get real time events from the server
exec        Run a command in a running container
export      Export a container's filesystem as a tar archive
history    Show the history of an image
images      List images
import      Import the contents from a tarball to create a filesystem image
info        Display system-wide information
inspect    Return low-level information on Docker objects
kill        Kill one or more running containers
load        Load an image from a tar archive or STDIN
login      Log in to a Docker registry
logout      Log out from a Docker registry
logs        Fetch the logs of a container
pause      Pause all processes within one or more containers
port        List port mappings or a specific mapping for the container
ps          List containers
pull        Pull an image or a repository from a registry
push        Push an image or a repository to a registry
rename      Rename a container
restart    Restart one or more containers
rm          Remove one or more containers
rmi        Remove one or more images
run        Run a command in a new container
save        Save one or more images to a tar archive (streamed to STDOUT by default)
search      Search the Docker Hub for images
start      Start one or more stopped containers
stats      Display a live stream of container(s) resource usage statistics
stop        Stop one or more running containers
tag        Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top        Display the running processes of a container
unpause    Unpause all processes within one or more containers
update      Update configuration of one or more containers
version    Show the Docker version information
wait        Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at docs.docker/go/guides/
3.2 镜像命令
以安装mysql为例进⾏操作:
# docker search 命令可以搜索镜像
[root@localhost ~]# docker search mysql
NAME                              DESCRIPTION                                    STARS    OFFICIAL  AUTOMATED mysql                            MySQL is a widely used, open-source relation…  10534    [OK]
mariadb                          MariaDB is a community-developed fork of MyS…  3934      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…  773                  [OK] percona                          Percona Server is a fork of the MySQL relati…  527      [OK]
centos/mysql-57-centos7          MySQL 5.7 SQL database server                  86
mysql/mysql-cluster              Experimental MySQL Cluster Docker images. Cr (79)
centurylink/mysql                Image containing mysql. Optimized to be link…  59                  [OK] bitnami/mysql                    Bitnami MySQL Docker Image                      48                  [OK]
deitch/mysql-backup              REPLACED! Please use hub.docker/r…  41                  [OK] databack/mysql-backup            Back up mysql anywhere!        38
prom/mysqld-exporter                                                              37                  [OK]
tutum/mysql                      Base docker image to run a MySQL database se (35)
schickling/mysql-backup-s3        Backup MySQL to S3 (supports   29                  [OK] linuxserver/mysql                A Mysql container, brought to you by LinuxSe (27)
centos/mysql-56-centos7          MySQL 5.6 SQL database server                  20
circleci/mysql                    MySQL is a widely used, open-source relation (20)
mysql/mysql-router                MySQL Router provides transparent routing be (18)
arey/mysql-client                Run a MySQL client from a docker container      17                  [OK] fradelg/mysql-cron-backup        MySQL/MariaDB database backup using   11                  [OK] yloeffler/mysql-backup            This image runs mysqldump to backup   7                    [OK] openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image (6)
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici (3)
ansibleplaybookbundle/mysql-apb  An APB which deploys RHSCL MySQL                2                    [OK] jelastic/mysql                    An image of the MySQL database server mainta (1)
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…  1                    [OK]
# docker pull命令可以拉取镜像
# docker pull mysql等同于docker pull mysql:latest
# 还可以指定版本进⾏拉取docker pull mysql:5.7
[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
45b42c59be33: Pull complete
b4f790bd91da: Pull complete
325ae51788e9: Pull complete
adcb9439d751: Pull complete
174c7fe16c78: Pull complete
698058ef136c: Pull complete
4690143a669e: Pull complete
f7599a246fd6: Pull complete
35a55bf0c196: Downloading [============================>                      ]  63.76MB/113.1MB 790ac54f4c47: Download complete
18602acc97e1: Download complete
365caa3500d0: Waiting
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
45b42c59be33: Already exists # docker的强⼤之处在于采⽤了这种分层机制,上⼀次命令中已经拉取了最新版本的mysql,即便以后再拉取其他版本的mysql,可重⽤层的镜像⽆需重新拉取。
b4f790bd91da: Already exists
325ae51788e9: Already exists
adcb9439d751: Already exists
174c7fe16c78: Already exists
698058ef136c: Already exists
4690143a669e: Already exists
66676c1ab9b3: Pull complete
25ebf78a38b6: Downloading [==============================================>    ]  101.3MB/108.4MB
349a839d5e27: Download complete
40b03e3e5980: Download complete
# docker images 列出所有已拉取的镜像
[root@localhost ~]# docker images
REPOSITORY    TAG      IMAGE ID      CREATED        SIZE
mysql        5.7      5f47254ca581  2 weeks ago    449MB
mysql        latest    2933adc350f3  2 weeks ago    546MB
hello-world  latest    bf756fb1ae65  13 months ago  13.3kB
# 如果想删除已拉取的镜像只需执⾏docker rmi 【IMAGE ID】即可
[root@localhost ~]# docker rmi 5f47254ca581
Untagged: mysql:5.7
Untagged: mysql@sha256:853105ad984a9fe87dd109be6756e1fbdba8b003b303d88ac0dda6b455f36556
Deleted: sha256:5f47254ca5817f99cdd387ce7345d43e770e0682a4c81b62776f3347551b1d85
Deleted: sha256:63f5d2725ff0ecffe0a7345e749d39b269a8cef04984661f0f4e752869b9fbb1
Deleted: sha256:acbe85abff4e7bbdd75a1f56ee9a095a72fcba4c226d0194d46b9a8471b1fe18
Deleted: sha256:b851a484b18c5d3d25497260c111631ae3adf924eb10baa533b2a5b03b339d1a
Deleted: sha256:b5133b076285236e7fd98c42c1f18f57e2b4ed98daaed7b0afb3b98b804d6f25
# 可以看到执⾏了上述命令后,成功删除了mysql:5.7
[root@localhost ~]# docker images
REPOSITORY    TAG      IMAGE ID      CREATED        SIZE
mysql        latest    2933adc350f3  2 weeks ago    546MB
hello-world  latest    bf756fb1ae65  13 months ago  13.3kB
# 此外还可以以传参的形式进⾏批量删除,以下命令将返回所有已拉取镜像的IMAGE ID,将此命令以变量形式传⼊docker rmi,继续进⾏下⾯的操作
[root@localhost ~]# docker images -qa
2933adc350f3
bf756fb1ae65
[root@localhost ~]# docker rmi $(docker images -qa)
Untagged: mysql:latest
Untagged: mysql@sha256:b1cc887ed32cc6c2f217b12703bd05f503f2037892c8bb226047fe5dff85a109
Deleted: sha256:2933adc350f3b62c05a66f700fba68ef93997d67263121250ec7848c50dcf3f5
Deleted: sha256:78b6531a3acdad2154a839ac1ec9ae2677632cc834bd996e75317f6e35717834
Deleted: sha256:f0c1c423000a6848e30ae3249c25b16f678167e56b4bb3013445b2ad1d179e8c
Deleted: sha256:4386f82820992c927b924177ed3e4c2ffd477d4db7a63539ac76fd09ee36cd89
Deleted: sha256:d7494c9168a11444d8b135********ace7393452f08f878686eec45122ee56c1
Deleted: sha256:08dbcab3fe630e39bbabaa9f0ae72ec6d100bf1e400ebb4b7f04151b18bca89c
Deleted: sha256:c3f78dcd6bcc4c156554296323e0eed74a4d2d93b304be15f55c1ef62dd06e0a
Deleted: sha256:f89b66495a65489290c8edb71e0dbf9e3d0d6213b82cebc2554b271599f2f99d
Deleted: sha256:1918839317d9988ff5e0168e336717e32820af1e77c3121297efc73a387ecdc5
Deleted: sha256:1d2bcd52664a92805e5f49d94d3649323dd0f5682ae3e1380fa07b7a54d6ceb0
Deleted: sha256:787de05fee96c7ba99e49f17d72aec68769a7373a8881a27917bdbf83dca58e8
Deleted: sha256:eb82f9a2fbd7a4a0fdfbe40b5e77a995ccf73ab91364d90f4db820fd59dbf63b
Deleted: sha256:9eb82f04c782ef3f5ca25911e60d75e441ce0fe82e49f0dbf02c81a3161d1300
Error response from daemon: conflict: unable to delete bf756fb1ae65 (must be forced) - image is being used by stopped container 0c7703f6e04d # 由于hello-world已被使⽤,若仍想删除需要使⽤-f强制删除[root@localhost ~]# docker images
REPOSITORY    TAG      IMAGE ID      CREATED        SIZE
hello-world  latest    bf756fb1ae65  13 months ago  13.3kB # 未删除成功
[root@localhost ~]# docker rmi -f $(docker images -qa)
Untagged: hello-world:latest
Untagged: hello-world@sha256:95ddb6c31407e84e91a986b004aee40975cb0bda14b5949f6faac5d2deadb4b9
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b # 加上-f参数后,删除成功
[root@localhost ~]# docker images
REPOSITORY  TAG      IMAGE ID  CREATED  SIZE
[root@localhost ~]#
3.3 容器命令
注意:镜像是创建容器的前提。
# 下载⼀个centos镜像并以docker进⾏安装
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Downloading [=====================>                            ]  31.63MB/75.18MB
[root@localhost ~]# docker imagesdocker进入容器
REPOSITORY  TAG      IMAGE ID      CREATED        SIZE
centos      latest    300e315adb2f  2 months ago  209MB
[root@localhost ~]# docker run -it centos /bin/bash
[root@7769890c1d06 /]# 此处的主机名已经变成新安装的centos,说明已经进⼊了centos容器
# doker内部的centos系统和宿主机是完全隔离的
[root@7769890c1d06 /]# exit
exit # 执⾏exit命令可以从容器回到宿主机,此时容器也会停⽌运⾏,如果希望退出容器但不停⽌容器运⾏可以【CTRL+P+Q】
[root@localhost ~]#
# docker ps可以查看正在运⾏的容器,⽽docker ps -a可以查看所有(包括正在运⾏和运⾏过的容器)
[root@localhost ~]# docker ps  -a
CONTAINER ID  IMAGE          COMMAND      CREATED            STATUS                            PORTS    NAMES
7769890c1d06  centos        "/bin/bash"  5 minutes ago      Exited (127) About a minute ago            ecstatic_johnson
b62415cd50c2  bf756fb1ae65  "/hello"      About an hour ago  Exited (0) About an hour ago                quizzical_cray
0c7703f6e04d  bf756fb1ae65  "/hello"      17 hours ago        Exited (0) 17 hours ago                    zealous_poitras
# 如果需要删除容器,⽅法和删除镜像类似docker rm 【CONTAINER ID】或者传参进⾏批量删除
[root@localhost ~]# docker rm $(docker ps -qa) 可以看到,执⾏此命令会连同运⾏的历史容器信息⼀同删除,当然此处也可以使⽤docker ps -qa | xargs docker rm来进⾏批量删除
7769890c1d06
b62415cd50c2
0c7703f6e04d
[root@localhost ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND  CREATED  STATUS    PORTS    NAMES
[root@localhost ~]#
# 容器启动停⽌命令
# docker start/stop/restart/kill 【CONTAINER ID】
[root@localhost ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND      CREATED          STATUS                      PORTS    NAMES
e887b4b948ff  centos    "/bin/bash"  57 seconds ago  Exited (0) 22 seconds ago            goofy_mendel
[root@localhost ~]# docker start e887b4b948ff
e887b4b948ff
[root@localhost ~]# docker ps
CONTAINER ID  IMAGE    COMMAND      CREATED              STATUS        PORTS    NAMES
e887b4b948ff  centos    "/bin/bash"  About a minute ago  Up 2 seconds            goofy_mendel
[root@localhost ~]# docker stop e887b4b948ff
e887b4b948ff

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。