docker的安装及基础操作与镜像构建
仓库配置及安装启动
1 2 3 4 5 6 | [root@localhost ~] # yum install -y yum-utils device-mapper-persistent-data lvm2 [root@localhost ~] # yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@localhost ~] # yum makecache fast [root@localhost ~] # yum -y install docker-ce [root@localhost ~] # service docker start Redirecting to /bin/systemctl start docker.service |
版本查看
1 2 | [root@localhost ~] # docker -v Docker version 18.09.0, build 4d60db4 |
镜像下载
1 2 3 4 5 6 | [root@localhost ~] # docker pull alpine Using default tag: latest latest: Pulling from library /alpine 4fe2ade4980c: Pull complete Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528 Status: Downloaded newer image for alpine:latest |
查看的本地镜像
1 2 3 | [root@localhost ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 196d12cf6ab1 3 months ago 4.41MB |
镜像启动成容器
1 2 | [root@localhost ~] # docker run -it alpine sh / # |
容器里创建一个大小为20M
1 2 3 4 5 | / # dd if=/dev/zero of=chenxi bs=10M count=2 2+0 records in 2+0 records out / # ls -l chenxi -rw-r--r-- 1 root root 20971520 Dec 12 04:31 chenxi |
用快捷键Ctrl+p,q退出容器,根据此运行的容器制作镜像
1 2 3 4 5 6 7 8 9 | [root@localhost ~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 292d32b4b3a1 alpine "sh" 15 minutes ago Up 15 minutes condescending_wu [root@localhost ~] # docker commit 292d32b4b3a1 chenxi:test sha256:cfcb3e42b392ac74fb8984f339916cbd062a968f6398af47e24d55dbb4364152 [root@localhost ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE chenxi test cfcb3e42b392 13 seconds ago 25.4MB alpine latest 196d12cf6ab1 3 months ago 4.41MB |
查看镜像层
1 2 3 4 5 6 7 8 9 | [root@localhost ~] # docker history alpine:latest IMAGE CREATED CREATED BY SIZE COMMENT 196d12cf6ab1 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B <missing> 3 months ago /bin/sh -c #(nop) ADD file:25c10b1d1b41d46a1… 4.41MB [root@localhost ~] # docker history chenxi:test IMAGE CREATED CREATED BY SIZE COMMENT cfcb3e42b392 2 minutes ago sh 21MB 196d12cf6ab1 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B <missing> 3 months ago /bin/sh -c #(nop) ADD file:25c10b1d1b41d46a1… 4.41MB |
使用dockerfil简单构建tomcat镜像文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | [root@localhost ~] # ls /tomcat/ apache-tomcat-9.0.13. tar .gz Dockerfile [root@localhost ~] # cat /tomcat/Dockerfile FROM centos RUN mkdir /tomcat && yum -y install java COPY apache-tomcat-9.0.13. tar .gz /tomcat RUN tar -zvxf /tomcat/apache-tomcat-9 .0.13. tar .gz -C /usr/local/ EXPOSE 8080 CMD [ "/usr/local/apache-tomcat-9.0.13/bin/catalina.sh" , "run" ] [root@localhost tomcat] # docker build -f Dockerfile -t tomcat:chenxi . Sending build context to Docker daemon 9.991MB Step 1 /6 : FROM centos ---> 1e1148e4cc2c Step 2 /6 : RUN mkdir /tomcat && yum -y install java ---> Using cache ---> 05e991a5d3f7 Step 3 /6 : COPY apache-tomcat-9.0.13. tar .gz /tomcat ---> Using cache ---> 3dffdd201d5f Step 4 /6 : RUN tar -zvxf /tomcat/apache-tomcat-9 .0.13. tar .gz -C /usr/local/ ---> Using cache ---> 1b8021e0f9fb Step 5 /6 : EXPOSE 8080 ---> Using cache ---> a3d6bc853d8c Step 6 /6 : CMD [ "/usr/local/apache-tomcat-9.0.13/bin/catalina.sh" , "run" ] ---> Using cache ---> 3969b96f4e53 Successfully built 3969b96f4e53 Successfully tagged tomcat:chenxi |
查看构建的容器镜像并启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@localhost tomcat] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 7 3969b96f4e53 7 minutes ago 445MB tomcat chenxi 3969b96f4e53 7 minutes ago 445MB <none> <none> 2b60cff33319 2 days ago 435MB <none> <none> 0717bf973613 2 days ago 435MB centos latest 1e1148e4cc2c 8 days ago 202MB [root@localhost tomcat] # docker run -d --name chenxi -p 8080:8080 tomcat:chenxi a7713ecd235158cdc7c276d057a8fb06561002f119d80b6ee882810e1d3c8488 [root@localhost tomcat] # ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users :(( "sshd" ,pid=1022,fd=3))LISTEN 0 100 127.0.0.1:25 *:* users :(( "master" ,pid=1293,fd=13))LISTEN 0 128 :::8080 :::* users :(( "docker-proxy" ,pid=2259,fd=4))LISTEN 0 128 :::22 :::* users :(( "sshd" ,pid=1022,fd=4))LISTEN 0 100 ::1:25 :::* users :(( "master" ,pid=1293,fd=14)) |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏