检查内核版本:需要大于3.0
[root@localhost admin]# uname -r3.10.0-693.el7.x86_64
第一步、切换成root用户并update yum
yum update
第二步、安装Docker
yum -y install docker-io
以下是运行结果
[root@localhost admin]# yum -y install docker-ioLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: centos.ustc.edu.cn * updates: centos.ustc.edu.cnResolving Dependencies--> Running transaction check---> Package docker.x86_64 2:1.13.1-68.gitdded712.el7.centos will be installed--> Processing Dependency: docker-common = 2:1.13.1-68.gitdded712.el7.centos for package: 2:docker-1.13.1-68.gitdded712.el7.centos.x86_64--> Processing Dependency: docker-client = 2:1.13.1-68.gitdded712.el7.centos for package: 2:docker-1.13.1-68.gitdded712.el7.centos.x86_64--> Running transaction check---> Package docker-client.x86_64 2:1.13.1-68.gitdded712.el7.centos will be installed---> Package docker-common.x86_64 2:1.13.1-68.gitdded712.el7.centos will be installed--> Processing Dependency: skopeo-containers >= 1:0.1.26-2 for package: 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64--> Processing Dependency: oci-umount >= 2:2.3.3-3 for package: 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64--> Processing Dependency: oci-systemd-hook >= 1:0.1.4-9 for package: 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64--> Processing Dependency: oci-register-machine >= 1:0-5.13 for package: 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64--> Processing Dependency: container-storage-setup >= 0.9.0-1 for package: 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64--> Processing Dependency: container-selinux >= 2:2.51-1 for package: 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64--> Running transaction check---> Package container-selinux.noarch 2:2.66-1.el7 will be installed---> Package container-storage-setup.noarch 0:0.10.0-1.gitdf0dcd5.el7 will be installed---> Package oci-register-machine.x86_64 1:0-6.git2b44233.el7 will be installed---> Package oci-systemd-hook.x86_64 1:0.1.16-1.git05bd9a0.el7 will be installed---> Package oci-umount.x86_64 2:2.3.3-3.gite3c9055.el7 will be installed---> Package skopeo-containers.x86_64 1:0.1.31-1.dev.gitae64ff7.el7.centos will be installed--> Finished Dependency ResolutionDependencies Resolved=================================================================================================================================================================================================== Package Arch Version Repository Size===================================================================================================================================================================================================Installing: docker x86_64 2:1.13.1-68.gitdded712.el7.centos extras 16 MInstalling for dependencies: container-selinux noarch 2:2.66-1.el7 extras 35 k container-storage-setup noarch 0.10.0-1.gitdf0dcd5.el7 extras 34 k docker-client x86_64 2:1.13.1-68.gitdded712.el7.centos extras 3.8 M docker-common x86_64 2:1.13.1-68.gitdded712.el7.centos extras 90 k oci-register-machine x86_64 1:0-6.git2b44233.el7 extras 1.1 M oci-systemd-hook x86_64 1:0.1.16-1.git05bd9a0.el7 extras 33 k oci-umount x86_64 2:2.3.3-3.gite3c9055.el7 extras 32 k skopeo-containers x86_64 1:0.1.31-1.dev.gitae64ff7.el7.centos extras 17 kTransaction Summary===================================================================================================================================================================================================Install 1 Package (+8 Dependent packages)Total download size: 21 MInstalled size: 73 MDownloading packages:(1/9): container-selinux-2.66-1.el7.noarch.rpm | 35 kB 00:00:00 (2/9): container-storage-setup-0.10.0-1.gitdf0dcd5.el7.noarch.rpm | 34 kB 00:00:00 (3/9): docker-common-1.13.1-68.gitdded712.el7.centos.x86_64.rpm | 90 kB 00:00:00 (4/9): oci-systemd-hook-0.1.16-1.git05bd9a0.el7.x86_64.rpm | 33 kB 00:00:00 (5/9): oci-umount-2.3.3-3.gite3c9055.el7.x86_64.rpm | 32 kB 00:00:00 (6/9): skopeo-containers-0.1.31-1.dev.gitae64ff7.el7.centos.x86_64.rpm | 17 kB 00:00:00 (7/9): oci-register-machine-0-6.git2b44233.el7.x86_64.rpm | 1.1 MB 00:00:01 (8/9): docker-client-1.13.1-68.gitdded712.el7.centos.x86_64.rpm | 3.8 MB 00:00:02 (9/9): docker-1.13.1-68.gitdded712.el7.centos.x86_64.rpm | 16 MB 00:00:13 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total 1.6 MB/s | 21 MB 00:00:13 Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : 1:skopeo-containers-0.1.31-1.dev.gitae64ff7.el7.centos.x86_64 1/9 Installing : 2:container-selinux-2.66-1.el7.noarch 2/9 Installing : 2:oci-umount-2.3.3-3.gite3c9055.el7.x86_64 3/9 Installing : 1:oci-register-machine-0-6.git2b44233.el7.x86_64 4/9 Installing : 1:oci-systemd-hook-0.1.16-1.git05bd9a0.el7.x86_64 5/9 Installing : container-storage-setup-0.10.0-1.gitdf0dcd5.el7.noarch 6/9 Installing : 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64 7/9 Installing : 2:docker-client-1.13.1-68.gitdded712.el7.centos.x86_64 8/9 Installing : 2:docker-1.13.1-68.gitdded712.el7.centos.x86_64 9/9 Verifying : container-storage-setup-0.10.0-1.gitdf0dcd5.el7.noarch 1/9 Verifying : 1:oci-systemd-hook-0.1.16-1.git05bd9a0.el7.x86_64 2/9 Verifying : 1:oci-register-machine-0-6.git2b44233.el7.x86_64 3/9 Verifying : 2:oci-umount-2.3.3-3.gite3c9055.el7.x86_64 4/9 Verifying : 2:docker-client-1.13.1-68.gitdded712.el7.centos.x86_64 5/9 Verifying : 2:container-selinux-2.66-1.el7.noarch 6/9 Verifying : 1:skopeo-containers-0.1.31-1.dev.gitae64ff7.el7.centos.x86_64 7/9 Verifying : 2:docker-1.13.1-68.gitdded712.el7.centos.x86_64 8/9 Verifying : 2:docker-common-1.13.1-68.gitdded712.el7.centos.x86_64 9/9 Installed: docker.x86_64 2:1.13.1-68.gitdded712.el7.centos Dependency Installed: container-selinux.noarch 2:2.66-1.el7 container-storage-setup.noarch 0:0.10.0-1.gitdf0dcd5.el7 docker-client.x86_64 2:1.13.1-68.gitdded712.el7.centos docker-common.x86_64 2:1.13.1-68.gitdded712.el7.centos oci-register-machine.x86_64 1:0-6.git2b44233.el7 oci-systemd-hook.x86_64 1:0.1.16-1.git05bd9a0.el7 oci-umount.x86_64 2:2.3.3-3.gite3c9055.el7 skopeo-containers.x86_64 1:0.1.31-1.dev.gitae64ff7.el7.centos Complete!
第三步、运行Docker
[root@localhost admin]# systemctl start docker
第四步、运行Hello-Word
[root@localhost admin]# docker run hello-worldUnable to find image 'hello-world:latest' locallyTrying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world9db2ca6ccae0: Pull complete Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdcStatus: Downloaded newer image for docker.io/hello-world:latestHello 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 bashShare 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@localhost admin]# systemctl status docker
查看docker命令
[root@localhost admin]# dockerUsage: docker COMMANDA self-sufficient runtime for containersOptions: --config string Location of client config files (default "/root/.docker") -D, --debug Enable debug mode --help Print usage -H, --host list Daemon socket(s) to connect to (default []) -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 quitManagement Commands: container Manage containers image Manage images 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 volume Manage volumesCommands: attach Attach 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 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 codesRun 'docker COMMAND --help' for more information on a command.
列出所有已存在的镜像
[root@localhost admin]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/hello-world latest 2cb0d9787c4d 4 weeks ago 1.85 kB
查询镜像:比如mongo
[root@localhost admin]# docker search mongoINDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATEDdocker.io docker.io/mongo MongoDB document databases provide high av... 4821 [OK] docker.io docker.io/mongo-express Web-based MongoDB admin interface, written... 279 [OK] docker.io docker.io/tutum/mongodb MongoDB Docker image – listens in port 270... 225 [OK]docker.io docker.io/mvertes/alpine-mongo light MongoDB container 78 [OK]docker.io docker.io/mongoclient/mongoclient Official docker image for Mongoclient, fea... 53 [OK]docker.io docker.io/bitnami/mongodb Bitnami MongoDB Docker Image 44 [OK]docker.io docker.io/frodenas/mongodb A Docker Image for MongoDB 17 [OK]docker.io docker.io/mongooseim/mongooseim Small docker image for MongooseIM - robust... 16 docker.io docker.io/cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... 8 [OK]docker.io docker.io/centos/mongodb-26-centos7 MongoDB NoSQL database server 5 docker.io docker.io/centos/mongodb-32-centos7 MongoDB NoSQL database server 5 docker.io docker.io/istepanov/mongodump Docker image with mongodump running as a c... 5 [OK]docker.io docker.io/eses/mongodb_exporter mongodb exporter for prometheus 4 [OK]docker.io docker.io/khezen/mongo MongoDB Docker image supporting RocksDB st... 4 [OK]docker.io docker.io/neowaylabs/mongodb-mms-agent This Docker image with MongoDB Monitoring ... 2 [OK]docker.io docker.io/19hz/mongo-container Mongodb replicaset for coreos 1 [OK]docker.io docker.io/centos/mongodb-34-centos7 MongoDB NoSQL database server 1 docker.io docker.io/centos/mongodb-36-centos7 MongoDB NoSQL database server 1 docker.io docker.io/circleci/mongo CircleCI images for MongoDB 1 [OK]docker.io docker.io/ekesken/mongo docker image for mongo that is configurabl... 1 [OK]docker.io docker.io/openshift/mongodb-24-centos7 DEPRECATED: A Centos7 based MongoDB v2.4 i... 1 docker.io docker.io/webhippie/mongodb Docker images for mongodb 1 [OK]docker.io docker.io/amd64/mongo MongoDB document databases provide high av... 0 docker.io docker.io/ansibleplaybookbundle/mongodb-apb An APB to deploy MongoDB. 0 [OK]docker.io docker.io/quilt/mongo MongoDB container for quilt.io 0 [OK]
拉取最新的镜像
[root@localhost admin]# docker pull mongoUsing default tag: latestTrying to pull repository docker.io/library/mongo ... latest: Pulling from docker.io/library/mongo8ee29e426c26: Pull complete 6e83b260b73b: Pull complete e26b65fd1143: Pull complete 40dca07f8222: Pull complete b420ae9e10b3: Pull complete f60a3ef68cf9: Pull complete 4a9eeb4f5b1b: Pull complete 2b2f33f1e783: Pull complete dbadcc13ac8f: Pull complete a532ead5a592: Pull complete 3f1579bbe71b: Pull complete f6c26165a602: Pull complete f2ea37fea6c8: Pull complete 0d33fd177178: Pull complete Digest: sha256:232dfea36769772372e1784c2248bba53c6bdf0893d82375a3b66c09962b5af9Status: Downloaded newer image for docker.io/mongo:latest
启动mongo镜像并查看启动情况
docker run --name some-mongo -d mongo:tag
--name 名字
-d 后台运行容器,并返回容器ID
-v /data/mongo:/data/mongo 将本地/data/mongo 数据目录 挂载到docker 对应的/data/mongo目录上
[root@localhost /]# docker run --name my_mongo -v /data/mongo:/data/mongo -d mongo6f5b2fdc120f997a6cd945a6aa547012ecad3618ab474376322b1b422292ef15[root@localhost /]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6f5b2fdc120f mongo "docker-entrypoint..." 4 seconds ago Up 3 seconds 27017/tcp my_mongo
进入容器并连接
[root@localhost /]# docker exec -it my_mongo bashroot@6f5b2fdc120f:/# mongoMongoDB shell version v4.0.1connecting to: mongodb://127.0.0.1:27017MongoDB server version: 4.0.1Welcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-userServer has startup warnings: 2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] 2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] 2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] 2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] 2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'2018-08-13T11:48:33.934+0000 I CONTROL [initandlisten] ---Enable MongoDB's free cloud-based monitoring service, which will then receive and displaymetrics about your deployment (disk utilization, CPU, operation statistics, etc).The monitoring data will be available on a MongoDB website with a unique URL accessible to youand anyone you share the URL with. MongoDB may use this information to make productimprovements and to suggest MongoDB products and deployment options to you.To enable free monitoring, run the following command: db.enableFreeMonitoring()To permanently disable this reminder, run the following command: db.disableFreeMonitoring()---> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB > exit bye
按Ctrl+P+Q进行退出容器
停止容器【stop后面是NAMES对应的名字】
[root@localhost admin]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdd310d6213a8 mongo "docker-entrypoint..." 23 minutes ago Up 23 minutes 0.0.0.0:27017->27017/tcp nifty_noyce[root@localhost admin]# docker stop nifty_noycenifty_noyce[root@localhost admin]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
查看停止的容器
docker ps -a
删除所有停止的容器
docker rm $(docker ps -a -q)
停止docker
[root@localhost /]# systemctl stop docker[root@localhost /]# systemctl status docker● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: inactive (dead) since Mon 2018-08-13 08:16:34 EDT; 8s ago Docs: http://docs.docker.com Process: 36006 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=0/SUCCESS) Main PID: 36006 (code=exited, status=0/SUCCESS)Aug 13 08:16:32 localhost.localdomain dockerd-current[36006]: 2018-08-13T12:16:32.872+0000 I CONTROL [signalProcessingThread] Shutting down free monitoringAug 13 08:16:32 localhost.localdomain dockerd-current[36006]: 2018-08-13T12:16:32.873+0000 I FTDC [signalProcessingThread] Shutting down full-time diagnostic data captureAug 13 08:16:32 localhost.localdomain dockerd-current[36006]: 2018-08-13T12:16:32.877+0000 I STORAGE [signalProcessingThread] WiredTigerKVEngine shutting downAug 13 08:16:32 localhost.localdomain dockerd-current[36006]: 2018-08-13T12:16:32.911+0000 I STORAGE [signalProcessingThread] shutdown: removing fs lock...Aug 13 08:16:32 localhost.localdomain dockerd-current[36006]: 2018-08-13T12:16:32.911+0000 I CONTROL [signalProcessingThread] now exitingAug 13 08:16:32 localhost.localdomain dockerd-current[36006]: 2018-08-13T12:16:32.911+0000 I CONTROL [signalProcessingThread] shutting down with code:0Aug 13 08:16:33 localhost.localdomain dockerd-current[36006]: time="2018-08-13T08:16:33.076314359-04:00" level=error msg="containerd: deleting container" error="exit status 1: \"con...ailed\\n\""Aug 13 08:16:33 localhost.localdomain dockerd-current[36006]: time="2018-08-13T08:16:33.205465662-04:00" level=warning msg="6f5b2fdc120f997a6cd945a6aa547012ecad3618ab474376322b1b422...d argument"Aug 13 08:16:33 localhost.localdomain dockerd-current[36006]: time="2018-08-13T08:16:33.253668366-04:00" level=info msg="stopping containerd after receiving terminated"Aug 13 08:16:34 localhost.localdomain systemd[1]: Stopped Docker Application Container Engine.Hint: Some lines were ellipsized, use -l to show in full.