CentOS 安装 Docker 卸载 Docker 及基本命令

版权声明:本文为老麻原创文章,未经老麻的博客博主允许不得转载。 https://blog.csdn.net/antma/article/details/80118608

1、安装Docker命令:yum install docker,安装详情:

[root@VM_4_84_centos ~]# yum install docker
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-53.git774336d.el7.centos will be installed
--> Processing Dependency: docker-common = 2:1.13.1-53.git774336d.el7.centos for package: 2:docker-1.13.1-53.git774336d.el7.centos.x86_64
--> Processing Dependency: docker-client = 2:1.13.1-53.git774336d.el7.centos for package: 2:docker-1.13.1-53.git774336d.el7.centos.x86_64
--> Running transaction check
---> Package docker-client.x86_64 2:1.13.1-53.git774336d.el7.centos will be installed
---> Package docker-common.x86_64 2:1.13.1-53.git774336d.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                        Arch                    Version                                               Repository               Size
============================================================================================================================================
Installing:
 docker                         x86_64                  2:1.13.1-53.git774336d.el7.centos                     extras                   16 M
Installing for dependencies:
 docker-client                  x86_64                  2:1.13.1-53.git774336d.el7.centos                     extras                  3.7 M
 docker-common                  x86_64                  2:1.13.1-53.git774336d.el7.centos                     extras                   86 k

Transaction Summary
============================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 20 M
Installed size: 69 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): docker-client-1.13.1-53.git774336d.el7.centos.x86_64.rpm                                                      | 3.7 MB  00:00:01     
(2/3): docker-common-1.13.1-53.git774336d.el7.centos.x86_64.rpm                                                      |  86 kB  00:00:00     
(3/3): docker-1.13.1-53.git774336d.el7.centos.x86_64.rpm                                                             |  16 MB  00:00:03     
--------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                       5.6 MB/s |  20 MB  00:00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:docker-common-1.13.1-53.git774336d.el7.centos.x86_64                                                                   1/3 
  Installing : 2:docker-client-1.13.1-53.git774336d.el7.centos.x86_64                                                                   2/3 
  Installing : 2:docker-1.13.1-53.git774336d.el7.centos.x86_64                                                                          3/3 
  Verifying  : 2:docker-common-1.13.1-53.git774336d.el7.centos.x86_64                                                                   1/3 
  Verifying  : 2:docker-1.13.1-53.git774336d.el7.centos.x86_64                                                                          2/3 
  Verifying  : 2:docker-client-1.13.1-53.git774336d.el7.centos.x86_64                                                                   3/3 

Installed:
  docker.x86_64 2:1.13.1-53.git774336d.el7.centos                                                                                           

Dependency Installed:
  docker-client.x86_64 2:1.13.1-53.git774336d.el7.centos               docker-common.x86_64 2:1.13.1-53.git774336d.el7.centos              

Complete!
[root@VM_4_84_centos ~]# 

安装成功!


将Docker加入/解除启动项

[root@VM_4_84_centos tmp]# systemctl enable docker.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@VM_4_84_centos tmp]# systemctl disable docker.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.
[root@VM_4_84_centos tmp]#


2、检查安装:docker version

[root@VM_4_84_centos ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: 
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@VM_4_84_centos ~]#

3、启动docker服务: systemctl start docker.service

[root@VM_4_84_centos ~]# systemctl start docker.service
[root@VM_4_84_centos ~]#


4、查看docker info:

[root@VM_4_84_centos ~]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: false
Logging Driver: journald
Cgroup Driver: systemd
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: docker-init
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: N/A (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: N/A (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
Kernel Version: 3.10.0-514.26.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 1.797 GiB
Name: VM_4_84_centos
ID: BHBE:PLTF:VVK6:FHYD:YXNO:Q55X:MBUO:72MV:OJFB:YLPQ:H3PT:A2OL
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
[root@VM_4_84_centos ~]#

5、运行 hello-world image: docker run hello-world

刚安装的docker没有hello-world镜像,run的时候会提示“Unable to find image 'hello-world:latest' locally”,然后会自己去下载这个镜像:

[root@VM_4_84_centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for docker.io/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:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

[root@VM_4_84_centos ~]# 

下载成功并运行提示:Hello from Docker!


6、docker ps  和 docker ps -a

docker ps:查看当前有哪些容器正在运行

docker ps -a :查看所有运行过的容器


7、删除images

docker rm  和 docker rmi 说明 :

rm  Remove one or more containers
rmi Remove one or more images

如果已有容器运行了,则不能删除images,比如要删除上面运行过的hello-world:

[root@VM_4_84_centos ~]# docker rmi e38bc07ac18e
Error response from daemon: conflict: unable to delete e38bc07ac18e (must be forced) - image is being used by stopped container d547cf8c432e
[root@VM_4_84_centos ~]# 

会提示不能删除,执行 docker ps -a 可以看到有一个ID为d547cf8c432e的容器:

[root@VM_4_84_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d547cf8c432e        hello-world         "/hello"            12 minutes ago      Exited (0) 12 minutes ago                       agitated_swanson
[root@VM_4_84_centos ~]#

然后先删除这个容器,再删除image既可,如下:

[root@VM_4_84_centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d547cf8c432e        hello-world         "/hello"            12 minutes ago      Exited (0) 12 minutes ago                       agitated_swanson
[root@VM_4_84_centos ~]# 
[root@VM_4_84_centos ~]# docker rm d547cf8c432
d547cf8c432
[root@VM_4_84_centos ~]# docker rmi e38bc07ac18e
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Deleted: sha256:e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96
Deleted: sha256:2b8cbd0846c5aeaa7265323e7cf085779eaf244ccbdd982c4931aef9be0d2faf
[root@VM_4_84_centos ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@VM_4_84_centos ~]#

8、删除container需要停止container,上面的hello-world 是运行完已经自动停止了,其他会有一直运行的container,停止:

     停止所有的container: docker stop $(docker ps -a -q) 

     删除所有的container: docker rm $(docker ps -a -q)


9、删除untagged images,即id为<None>的image:

     docker rmi $(docker images | grep "^<none>" | awk "{print $3}") 

     删除全部image

     docker rmi $(docker images -q)


10、Docker卸载

 列出安装过的包

[root@VM_4_84_centos tmp]# yum list installed | grep docker
docker.x86_64                    2:1.13.1-53.git774336d.el7.centos @extras      
docker-client.x86_64             2:1.13.1-53.git774336d.el7.centos @extras      
docker-common.x86_64             2:1.13.1-53.git774336d.el7.centos @extras      
[root@VM_4_84_centos tmp]#

 删除安装包

 yum -y remove docker.x86_64 docker-client.x86_64 docker-common.x86_64

 删除镜像/容器等

 $ rm -rf /var/lib/docker


猜你喜欢

转载自blog.csdn.net/antma/article/details/80118608