CentOS 安裝 Docker 解除安裝 Docker 及基本命令
1、安裝Docker命令:yum install docker,安裝詳情:
[[email protected]_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!
[ [email protected]_4_84_centos ~]#
安裝成功!
將Docker加入/解除啟動項
[[email protected]_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.
[[email protected]_4_84_centos tmp]# systemctl disable docker.service
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.
[ [email protected]_4_84_centos tmp]#
2、檢查安裝:docker version
[[email protected]_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?
[[email protected] _4_84_centos ~]#
3、啟動docker服務: systemctl start docker.service
[[email protected]_4_84_centos ~]# systemctl start docker.service
[[email protected]_4_84_centos ~]#
4、檢視docker info:
[[email protected]_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)
[[email protected]_4_84_centos ~]#
5、執行 hello-world image: docker run hello-world
剛安裝的docker沒有hello-world映象,run的時候會提示“Unable to find image 'hello-world:latest' locally”,然後會自己去下載這個映象:
[[email protected]_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/
[[email protected]_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:
[[email protected]_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
[[email protected]_4_84_centos ~]#
會提示不能刪除,執行 docker ps -a 可以看到有一個ID為d547cf8c432e的容器:
[[email protected]_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
[[email protected]_4_84_centos ~]#
然後先刪除這個容器,再刪除image既可,如下:
[[email protected]_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
[[email protected]_4_84_centos ~]#
[[email protected]VM_4_84_centos ~]# docker rm d547cf8c432
d547cf8c432
[[email protected]_4_84_centos ~]# docker rmi e38bc07ac18e
Untagged: docker.io/hello-world:latest
Untagged: docker.io/[email protected]:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Deleted: sha256:e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96
Deleted: sha256:2b8cbd0846c5aeaa7265323e7cf085779eaf244ccbdd982c4931aef9be0d2faf
[[email protected]_4_84_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[[email protected]_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解除安裝
列出安裝過的包
[[email protected]_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
[[email protected]_4_84_centos tmp]#
刪除安裝包
yum -y remove docker.x86_64 docker-client.x86_64 docker-common.x86_64
刪除映象/容器等
$ rm -rf /var/lib/docker