Linux centos7安装docker
安装前准备
需要切换到root目录
卸载旧docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
需要的安装包
yum install -y yum-utils
设置镜像的仓库
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云镜像
#https://download.docker.com/linux/centos/docker-ce.repo #国外镜像
安装docker相关的engine docker-ce 社区 docker-ee企业版已经被收购改名Mirantis Container Runtime
yum install docker-ce docker-ce-cli containerd.io
启动docker
systemctl start docker
启动命令
run hello-world #docker run image 代表一个具体的应用hello-world代表测试应用’
卸载docker依赖
yum remove docker-ce docker-ce-cli containerd.io
删除目录配置资源
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
配置阿里云加速器
2、配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://qkuacsvp.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
3、docker run image #docker run 原理
Basic principles of Docker ( has yet to be supplemented)
docker common commands
help common
docker version #show docker version
docker info #show docker system
docker common --help #help common
Docker common
[root@localhost ~]# docker images #show all of the mirrors from localhost
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
#optional
-a, --all
-q, --quiet
Docker search
root@localhost ~]# docker search sql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
sqlpad/sqlpad Run SQL in your browser and chart the result… 33
nouchka/sqlite3 Sqlite3 command line in a docker container 32
#docker search sql --filter=START=3000 #the search result of Starts is over 3000
Docker pull images
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi
docker rmi -f imageId
docker rmi -f imageId imageId imageId imageId # del multiple images
docker rmi -f $(docker images -aq) #recursive del all images
start new container
docker run [optional parameter] image
# parameter description
--name="Name" #container name tomcat01 tomcat02 ,to distinguish container
-d #run on background
-it #interactive operation,check inside things from container
-p #scan container port -p 8080:8080
-p #ip:host-port:container-port
-p #hostport:container-port (common)
-p #container-port
container-port
-p assign random port
#test,start and into container
[root@localhost ~]# docker run -it centos /bin/bash
[root@4a8e7a35a49b /]# ls #check inside centos from container,base version,many commond is not complete
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#exit container and back to the host
[root@4a8e7a35a49b /]# exit
exit
[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos
Desktop Downloads Music Public use_aliyun_yum_repo.sh
List all of running container
# docker ps command
# list currently running container
-a # list currently running container + bring the historical run container
-n=? # list lasted created container
-q # only list container's No.
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a8e7a35a49b centos "/bin/bash" 30 minutes ago Exited (0) 29 minutes ago elegant_albattani
3e972d42e4bf centos "/bin/bash" 35 minutes ago Exited (0) 30 minutes ago kind_bohr
016789d6e155 centos "/bin/bash" 35 minutes ago Exited (0) 35 minutes ago practical_bose
0ef80be1e75b centos "/bin/bash" 38 minutes ago Exited (130) 35 minutes ago angry_hellman
dbb26cae80d4 hello-world "/hello" 16 hours ago Exited (0) 16 hours ago quizzical_ganguly
exit container
exit #exit container directly and stop the container
Ctrl + P + Q #exit container directly without stop the container
delete container
docker rm image-id #delete the specified image,can't delete running images
docker rm -f $(docker ps -aq) # delete all images force
docker ps -a -q|xargs docker rm # delete all
start and stop container operation
docker start image-id
docker restart image-id
docker stop image-id
docker kill image-id
the other useful command
run image on backstage
[root@localhost ~]# docker run -d centos
bfb6b37caa863967eb12e4a64e4dea7a41f59e675285fa86a32e45613b461209
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#问题:docker ps ,发现centos停止了
#常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就好立刻停止,就是没有程序了
check the logs
[root@localhost ~]# docker logs -f -t --tail image-id
"docker logs" requires exactly 1 argument.
See 'docker logs --help'.
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
[root@localhost ~]# docker logs -f -t --tail image-id
"docker logs" requires exactly 1 argument.
See 'docker logs --help'.
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
#write the shell js
docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3216332e7596 centos "/bin/sh -c 'while t…" About a minute ago Up About a minute sleepy_shockley
#show logs
-tf #show logs
--tail number #show logs branches
#example
docker logs -tf --tail 10 dce7b86171bf
check progress-info from the container
#command docker top container-id
[root@localhost ~]# docker top 3216332e7596
UID PID PPID C STIME TTY TIME
root 2867 2845 0 06:19 ? 00:00:00
root 3075 2867 0 06:22 ? 00:00:00
check meta-data of mirror
docker inspect 3216332e7596
[
{
"Id": "3216332e75964cbaa6735e8dd13b26fd3fd9e0fbd4ab9e6abef25b8e8a312de7",
"Created": "2022-01-18T22:19:35.15714527Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo kuangshen;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2867,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-18T22:19:35.589002572Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/3216332e75964cbaa6735e8dd13b26fd3fd9e0fbd4ab9e6abef25b8e8a312de7/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3216332e75964cbaa6735e8dd13b26fd3fd9e0fbd4ab9e6abef25b8e8a312de7/hostname",
"HostsPath": "/var/lib/docker/containers/3216332e75964cbaa6735e8dd13b26fd3fd9e0fbd4ab9e6abef25b8e8a312de7/hosts",
"LogPath": "/var/lib/docker/containers/3216332e75964cbaa6735e8dd13b26fd3fd9e0fbd4ab9e6abef25b8e8a312de7/3216332e75964cbaa6735e8dd13b26fd3fd9e0fbd4ab9e6abef25b8e8a312de7-json.log",
"Name": "/sleepy_shockley",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/4d755eafeeb0c7d32641044ebc35e9495fb1018b73f1afa4c0b16e0437004600-init/diff:/var/lib/docker/overlay2/d2d2ab36738cce0d47d14c511a1c72491da564fd13af65a3be6285e2b33d0cb9/diff",
"MergedDir": "/var/lib/docker/overlay2/4d755eafeeb0c7d32641044ebc35e9495fb1018b73f1afa4c0b16e0437004600/merged",
"UpperDir": "/var/lib/docker/overlay2/4d755eafeeb0c7d32641044ebc35e9495fb1018b73f1afa4c0b16e0437004600/diff",
"WorkDir": "/var/lib/docker/overlay2/4d755eafeeb0c7d32641044ebc35e9495fb1018b73f1afa4c0b16e0437004600/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3216332e7596",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo kuangshen;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "bc262f8c86bfad0297721f427736c5eef747275d6e5d8cd7cd119b904eee00c5",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/bc262f8c86bf",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "216315e78caddf8c42a273912e5319f029f0a6b378d135e66e2f93f31e84f360",
"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": "a1a989c143ad335d2f12bf4d33721c957fada9f91ce3c6a3cd3d96c700105529",
"EndpointID": "216315e78caddf8c42a273912e5319f029f0a6b378d135e66e2f93f31e84f360",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
enter running container
#通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#command
docker exec -it container-id bashShell
#test
#way1
[root@localhost ~]# docker exec -it 3216332e7596 /bin/bash
[root@3216332e7596 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 22:19 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1
root 1038 0 0 22:36 pts/0 00:00:00 /bin/bash
root 1110 1 0 22:37 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sle
root 1111 1038 0 22:37 pts/0 00:00:00 ps -ef
[root@3216332e7596 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#way2
docker attach container-id
[root@localhost ~]# docker attach b4d98498143a
[root@b4d98498143a /]#
#docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach #进入容器正在执行的终端,不会启动新的进程
#copy doc to host from container
[root@b4d98498143a /]# touch test.tt
[root@localhost ~]# docker cp b4d98498143a:test.tt /home
#拷贝是一个手动的过程,未来我们使用 -v 卷的技术,可以实现
#Summarise
attach Attach to a running container #当前shell下attach连接指定运行镜像
build Build an image from a Dockerfile #通过Dockerfile定制镜像
commit Create a new image from a containers changes #提交当前容器为新的镜像
cp Copy files/folders from a container to a HOSTDIR or to STDOUT #从容器中拷贝指定文件或者目录到宿主机中
create Create a new container #创建一个新的容器,同run 但不启动容器
diff Inspect changes on a containers filesystem #查看docker容器变化
events Get real time events from the server#从docker服务获取容器实时事件
exec Run a command in a running container#在已存在的容器上运行命令
export Export a containers filesystem as a tar archive #导出容器的内容流作为一个tar归档文件(对应import)
history Show the history of an image #展示一个镜像形成历史
images List images #列出系统当前镜像
import Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像(对应export)
info Display system-wide information #显示系统相关信息
inspect Return low-level information on a container or image #查看容器详细信息
kill Kill a running container #kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包中加载一个镜像(对应save)
login Register or log in to a Docker registry#注册或者登陆一个docker源服务器
logout Log out from a Docker registry #从当前Docker registry退出
logs Fetch the logs of a container #输出当前容器日志信息
pause Pause all processes within a container#暂停容器
port List port mappings or a specific mapping for the CONTAINER #查看映射端口对应的容器内部源端口
ps List containers #列出容器列表
pull Pull an image or a repository from a registry #从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to a registry #推送指定镜像或者库镜像至docker源服务器
ename Rename a container #重命名容器
restart Restart a running container #重启运行的容器
rm Remove one or more containers #移除一个或者多个容器
rmi Remove one or more images #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除)
run Run a command in a new container #创建一个新的容器并运行一个命令
save Save an image(s) to a tar archive#保存一个镜像为一个tar包(对应load)
search Search the Docker Hub for images #在docker hub中搜索镜像
start Start one or more stopped containers#启动容器
stats Display a live stream of container(s) resource usage statistics #统计容器使用资源
stop Stop a running container #停止容器
tag Tag an image into a repository #给源中镜像打标签
top Display the running processes of a container #查看容器中运行的进程信息
unpause Unpause all processes within a container #取消暂停容器
version Show the Docker version information#查看容器版本号
wait Block until a container stops, then print its exit code #截取容器停止时的退出状态值