Docker容器的基础操作
发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,概述Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机
千家信息网最后更新 2025年01月23日Docker容器的基础操作
概述
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
安装Docker
配置阿里云base源和epel源,安装Docker环境依赖
[root@youxi1 ~]# cd /etc/yum.repos.d/[root@youxi1 yum.repos.d]# mkdir back[root@youxi1 yum.repos.d]# mv *.repo back/ //备份源[root@youxi1 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo[root@youxi1 yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo[root@youxi1 yum.repos.d]# yum clean all //清空yum缓存[root@youxi1 yum.repos.d]# yum list //生成缓存[root@youxi1 yum.repos.d]# cd[root@youxi1 ~]# yum -y install yum-utils device-mapper-persistent-data lvm2 //安装依赖[root@youxi1 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //配置阿里云的Docker的yum源 |
安装Docker,启动并设置开机自启
[root@youxi1 ~]# yum -y install docker-ce docker-ce-cli containerd.io[root@youxi1 ~]# systemctl start docker[root@youxi1 ~]# systemctl enable dockerCreated symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.[root@youxi1 ~]# docker version //显示Docker版本信息Client: Docker Engine - Community Version: 19.03.2 API version: 1.40 Go version: go1.12.8 Git commit: 6a30dfc Built: Thu Aug 29 05:28:55 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.2 API version: 1.40 (minimum version 1.12) Go version: go1.12.8 Git commit: 6a30dfc Built: Thu Aug 29 05:27:34 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.6 GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc: Version: 1.0.0-rc8 GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f docker-init: Version: 0.18.0 GitCommit: fec3683[root@youxi1 ~]# docker info //查看Docker信息,包括镜像和容器数Client: Debug Mode: false Server: Containers: 0 //容器 Running: 0 //运行 Paused: 0 //暂停 Stopped: 0 //关闭 Images: 0 //镜像 Server Version: 19.03.2 //版本 Storage Driver: overlay2 //存储启动程序 Backing Filesystem: xfs //备份文件系统格式 Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-862.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 1.936GiB Name: youxi1 ID: FR4U:DHB6:QGST:B3SB:NHKD:GDBX:PZL3:2RSX:SUMM:WFZO:KPPU:W423 Docker Root Dir: /var/lib/docker //Docker的主目录 Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false |
一、查看本地容器进程
[root@docker ~]# docker ps -a #显示所有容器进程CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9bd32b3b2ad0 hello-world "/hello" 20 hours ago Exited (0) 20 hours ago lucid_murdockfb2d81c98cd2 hello-world "/hello" 3 weeks ago Exited (0) 3 weeks ago great_bartik[root@docker ~]# docker ps #显示当前运行的容器进程CONTAINER ID IMAGE
二、容器启动命令
[root@docker ~]# docker run --helpUsage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]OPTIONS:选项-i : 表示启动一个可交互的容器,并持续打开标准输入-t : 表示使用终端关联到容器的标准输入输出上-d : 表示将容器放置后台运行--rm : 退出后即删除容器--name : 表示定义容器唯一名称IMAGE: 表示要运行的镜像COMMAND : 表示启动容器时运行的命令
三、启动一个交互式的容器
[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest cc0abc535e36 13 days ago 5.59MBzhoumingkang/alpine v3.10.3 cc0abc535e36 13 days ago 5.59MBalpine 3.9.4 055936d39205 8 months ago 5.53MBhello-world latest fce289e99eb9 12 months ago 1.84kB[root@docker ~]# docker run -ti -- name mingkang-test alpine:latest /bin/sh/ # ip addr1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever18: eth0@if19: mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:07:05:02 brd ff:ff:ff:ff:ff:ff inet 172.7.5.2/24 brd 172.7.5.255 scope global eth0 valid_lft forever preferred_lft forever/ # cat /etc/issue Welcome to Alpine Linux 3.11Kernel \r on an \m (\l)[root@docker ~]# docker ps #查看当前运行的容器进程CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES08b02f026afe alpine:latest "/bin/sh" 7 seconds ago Up 5 seconds mingkang-test
四、容器退出后即删除--rm(docker ps -a 查询不了记录)
[root@docker ~]# docker run --rm alpine:latest /bin/echo hellohello
五、以后台的形式运行一个容器-d
[root@docker ~]# docker run -d --name mingkang-test3 alpine:latest /bin/sleep 3000744b31fbaadfc9605d5dbe8d302ef9bd3de8208142aa00c1feffd86804347b01[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES744b31fbaadf alpine:latest "/bin/sleep 3000" 18 seconds ago Up 17 seconds mingkang-test3
六、进入一个容器exec
[root@docker ~]# docker exec -ti 744b31fbaadf /bin/sh #长长的字符串为容器ID/ # psPID USER TIME COMMAND 1 root 0:00 /bin/sleep 3000 6 root 0:00 /bin/sh 11 root 0:00 ps/ #
七、停止/重启/启动一个容器(stop/restart/start)
[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES744b31fbaadf alpine:latest "/bin/sleep 3000" 3 minutes ago Up 3 minutes mingkang-test3[root@docker ~]# docker stop 744b31fbaadf #容器的ID号744b31fbaadf[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
八、删除docker ps 的内容
[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES744b31fbaadf alpine:latest "/bin/sleep 3000" 12 minutes ago Exited (137) 8 minutes ago mingkang-test308b02f026afe alpine:latest "/bin/sh" 25 minutes ago Exited (0) 23 minutes ago mingkang-test56760413d596 alpine:latest "/bin/sh" 28 minutes ago Exited (0) 27 minutes ago mingkang_testeae500caa715 alpine:latest "/bin/sh" 34 minutes ago Exited (0) 29 minutes ago vigilant_boydc6fed43d8a09 alpine:latest "/bin/sh" 34 minutes ago Exited (0) 34 minutes ago keen_volhard058aee8ef1eb alpine:latest "/bin/sh" 36 minutes ago Exited (0) 35 minutes ago quirky_morse6d81bca654ab alpine:latest "/bin/sh" 37 minutes ago Exited (127) 36 minutes ago practical_torvaldsadc1a16a7bf6 alpine:latest "/bin/sh" 39 minutes ago Exited (0) 38 minutes ago unruffled_carson20a5432e3266 alpine:latest "/bin/sh" 39 minutes ago Exited (0) 39 minutes ago elated_ellis08bbe9c13d26 alpine:latest "/bin/sh" 40 minutes ago Exited (0) 40 minutes ago nifty_kalam9bd32b3b2ad0 hello-world "/hello" 21 hours ago Exited (0) 21 hours ago lucid_murdockfb2d81c98cd2 hello-world "/hello" 3 weeks ago Exited (0) 3 weeks ago great_bartik[root@docker ~]# docker rm keen_volhard quirky_morse practical_torvalds unruffled_carson elated_ellis nifty_kalam lucid_murdock great_bartikkeen_volhardquirky_morsepractical_torvaldsunruffled_carsonelated_ellisnifty_kalamlucid_murdockgreat_bartik[root@docker ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES744b31fbaadf alpine:latest "/bin/sleep 3000" 13 minutes ago Exited (137) 9 minutes ago mingkang-test308b02f026afe alpine:latest "/bin/sh" 26 minutes ago Exited (0) 24 minutes ago mingkang-test56760413d596 alpine:latest "/bin/sh" 28 minutes ago Exited (0) 28 minutes ago mingkang_test备注:如需删除在运行的容器,使用docker rm -f 容器名称/容器ID循环全部删除:for i in `docker ps -a | grep -i exited|awk '{print $1}'`;do docker rm -f $i;done
九、容器的提交(把已写入内容的容器提交为镜像)
[root@docker ~]# docker run -d --name myalpine alpine:latest /bin/sleep 30000 #后台长时间运行一个容器3ccd27be4d19a60f317d187ee99d7fcd76d368dccb0326f8789d6617fb39f457[root@docker ~]# docker ps #查看运行的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3ccd27be4d19 alpine:latest "/bin/sleep 30000" 6 seconds ago Up 4 seconds myalpine[root@docker ~]# docker exec -ti myalpine /bin/sh #以交互的方式进入容器/ # lsbin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var/ # touch hello.txt/ # echo "hello world" > hello.txt / # cat hello.txt hello world[root@docker ~]# docker commit -p myalpine myalpine:latest_with_hello.txt#把名称为myalpine的容器提交为一个新的镜像sha256:3ce9b8b899ba2018c01ca88fba6c24da6ff1287905a4ff4c60e968f129a22863[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyalpine latest_with_hello.txt 3ce9b8b899ba 20 seconds ago 5.59MBalpine latest cc0abc535e36 13 days ago 5.59MBzhoumingkang/alpine v3.10.3 cc0abc535e36 13 days ago 5.59MBalpine 3.9.4 055936d39205 8 months ago 5.53MBhello-world latest fce289e99eb9 12 months ago 1.84kB#启动该myalpin:latest_with_hello.txt 镜像容器后,发现文件已经存在。
十、容器的导出与加载
导出myalpine:latest_with_hello.txt-----[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyalpine latest_with_hello.txt 3ce9b8b899ba 20 minutes ago 5.59MBalpine latest cc0abc535e36 13 days ago 5.59MBzhoumingkang/alpine v3.10.3 cc0abc535e36 13 days ago 5.59MBalpine 3.9.4 055936d39205 8 months ago 5.53MBhello-world latest fce289e99eb9 12 months ago 1.84kB[root@docker ~]# docker save myalpine:latest_with_hello.txt > myalpine-latest_with_hello.txt.tar[root@docker ~]# lsanaconda-ks.cfg myalpine-latest_with_hello.txt.tar-----删除刚刚导出的镜像[root@docker ~]# docker rmi myalpine:latest_with_hello.txtUntagged: myalpine:latest_with_hello.txtDeleted: sha256:3ce9b8b899ba2018c01ca88fba6c24da6ff1287905a4ff4c60e968f129a22863Deleted: sha256:1d89a6f64295ef6889fef9ae917281e94d4d9e5c9b3eb98d5cc25c129f34f4b3[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest cc0abc535e36 13 days ago 5.59MBzhoumingkang/alpine v3.10.3 cc0abc535e36 13 days ago 5.59MBalpine 3.9.4 055936d39205 8 months ago 5.53MBhello-world latest fce289e99eb9 12 months ago 1.84kB-----重新加载该镜像[root@docker ~]# docker load < myalpine-latest_with_hello.txt.tar e63a9b4f5484: Loading layer [==================================================>] 3.584kB/3.584kBLoaded image: myalpine:latest_with_hello.txt[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyalpine latest_with_hello.txt 3ce9b8b899ba 22 minutes ago 5.59MBalpine latest cc0abc535e36 13 days ago 5.59MBzhoumingkang/alpine v3.10.3 cc0abc535e36 13 days ago 5.59MBalpine 3.9.4 055936d39205 8 months ago 5.53MBhello-world latest fce289e99eb9 12 months ago 1.84kB
十一、容器的日志查看
[root@docker ~]# docker run 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 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/get-started/[root@docker ~]# docker ps -a | grep -i hello #查看曾经运行的进程1c5c26a209d4 hello-world:latest "/hello" 12 seconds ago Exited (0) 10 seconds ago angry_ellis[root@docker ~]# docker logs 1c5c26a209d4 #查看该容器运行的日志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 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/get-started/[root@docker ~]#
容器
运行
镜像
进程
名称
后台
信息
内容
命令
备份
文件
日志
机器
标准
版本
缓存
阿里
应用
输入
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全班会记录简短
网络安全与信息化中心
违反网络安全法的行为
宁畅服务器广东代理商
地铁软件开发怎么样
租用数据库
通信网络安全 参考文献
网络技术主管负责什么工作
unix mysql数据库搬迁
南京协企网络技术有限公司
股票分析软件开发商
重庆fil服务器加盟
svn 软件开发
用友u8怎么调出服务器图标
数据库什么是完全函数依赖
网络安全教案目的
专业技术人员网络安全知识内容
北京服务器供应商
宁波银行软件开发工资待遇
ios横屏小游戏服务器
软件开发的项目简介
猎头数据库来源
数据库安全防护图片
四川省天驱企业级服务器云空间
山东大千网络技术
学校网络安全经验介绍
广州国内网络技术开发公司
sql数据库打印语句
软件开发薪资参考
什么是网络安全最大风险