Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
centos7安装docker
[root@localhost ~]# yum -y install docker
启动docker服务,并设置为开机自启动
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
一、docker基本命令介绍
attach 连接到一个正在运行的容器
build 从Dockerfile中构建一个映像
commit 从容器的更改中创建一个新的映像
cp 在容器和本地文件系统之间复制文件/文件夹
create 创建一个新容器
diff 检查容器文件系统上的更改
events 从服务器获取实时事件
exec 在运行的容器中运行一个命令
export 将容器的文件系统导出为tar存档文件
history 展示了一个形象的历史
images 图像列表
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 40960efd7b8f 4 days ago 108.4 MB
import 从tarball导入内容,以创建文件系统映像
[root@localhost ~]# docker inspect c2
[
{
"Id": "6454cb56e12f4209771e17cbcd866a7e5225665177be761f51ca8c797c30fe66",
"Created": "2017-11-09T06:28:27.825868355Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12737,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-11-09T06:33:45.36921727Z",
"FinishedAt": "2017-11-09T06:33:05.320232823Z"
},
"Image": "sha256:40960efd7b8f44ed5cafee61c189a8f4db39838848d41861898f56c29565266e",
"ResolvConfPath": "/var/lib/docker/containers/6454cb56e12f4209771e17cbcd866a7e5225665177be761f51ca8c797c30fe66/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6454cb56e12f4209771e17cbcd866a7e5225665177be761f51ca8c797c30fe66/hostname",
"HostsPath": "/var/lib/docker/containers/6454cb56e12f4209771e17cbcd866a7e5225665177be761f51ca8c797c30fe66/hosts",
"LogPath": "",
"Name": "/c2",
"RestartCount": 0,
"Driver": "devicemapper",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "docker-runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "8",
"DeviceName": "docker-253:0-8953729-1bc788c4a859fae3ebc08593c382a04e537a2c3fa95c4a18955ed68e0ac16e58",
"DeviceSize": "10737418240"
}
},
"Mounts": [],
"Config": {
"Hostname": "6454cb56e12f",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.13.6-1~stretch",
"NJS_VERSION=1.13.6.0.1.14-1~stretch"
],
"Cmd": [
"/bin/bash"
],
"Image": "docker.io/nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers \[email protected]\u003e"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "aa2c902263ae520cbca01afdc83d933876801f91719d2bc1a2ffaa733672f21e",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/aa2c902263ae",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f3c3bf88afc548d5eed29cb3e5435f92f8ef9292673ba8e76b9c24c441283f1b",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f31a67fae354241108d5ed523e86676ed9ed0b0f5f3a1fac06c104be41cfa659",
"EndpointID": "f3c3bf88afc548d5eed29cb3e5435f92f8ef9292673ba8e76b9c24c441283f1b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
使用以下快捷键完成容器退出,并使其保持在后台运行
ctrl+p+q
kill 杀死一个或多个运行容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6454cb56e12f docker.io/nginx "/bin/bash" About a minute ago Up About a minute 80/tcp c1
pull 从注册表中提取镜像或存储库
[root@localhost ~]# docker pull docker.io/nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
bc95e04b23c0: Pull complete
a21d9ee25fc3: Pull complete
9bda7d5afd39: Pull complete
Digest: sha256:9fca103a62af6db7f188ac3376c60927db41f88b8d2354bf02d2290a672dc425
push 将镜像或存储库推送到注册中心
[root@localhost ~]# docker rename c1 c2
restart 重新启动一个容器
[root@localhost ~]# docker run -it --name=c1 docker.io/nginx /bin/bash
root@6454cb56e12f:/#
root@6454cb56e12f:/#
save 保存一个或多个镜像到tar存档文件(默认情况下,流到STDOUT)
search 在Docker中心搜索镜像
[root@localhost ~]# docker search centos7
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 103 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 15
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 12
docker.io docker.io/centos/php-56-centos7 Platform for building and running PHP 5.6 ... 10
s
ervice 管理Docker服务
[root@localhost ~]# docker start c2
stats 显示容器(s)资源使用统计的实时流
[root@localhost ~]# docker stop c2
c2
swarm 管理Docker群集
[root@localhost ~]# docker top c2
UID PID PPID C STIME TTY TIME CMD
root 12573 12565 0 14:28 pts/1 00:00:00 /bin/bas
unpause 在一个或多个容器内暂停所有进程docker -v 显示版本
[root@localhost ~]# docker -v
Docker version 1.12.6, build 85d7426/1.12.6
二、docker 打包镜像系列
使用tar命令打包自己的镜像
[root@localhost ~]# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf centos7-base.tar /