Docker 实战系列:接入生产场景,快速拉起服务

📅 2026/8/7 13:03:56
Docker 实战系列:接入生产场景,快速拉起服务
Mysql 容器化安装进入 mysql 的镜像网站查找 mysql 的镜像mysql dockerhub官网选择使用最多的 5.7 版本拉取镜像drwVM-0-14-ubuntu:~$dockerpull mysql:5.75.7: Pulling from library/mysql df9a4d85569b: Pull complete ffc89e9dfd88: Pull complete 90986bb8de6e: Pull complete e9f03a1c24ce: Pull complete 43d05e938198: Pull complete 1c56c3d4ce74: Pull complete ae71319cb779: Pull complete 68c3898c2015: Pull complete 6b95a940e7b6: Pull complete 20e4dcae4c69: Pull complete 064b2d298fba: Pull complete 96889c47a44a: Download complete 9eb6a52b8441: Download complete Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb Status: Downloaded newer imageformysql:5.7 docker.io/library/mysql:5.7启动一个 mysql并指定密码和端口开放dockerrun--namemysql-eMYSQL_ROOT_PASSWORDyourpassword-p3306:3306-dmysql:5.7egdrwVM-0-14-ubuntu:~$dockerrun--namemysql-eMYSQL_ROOT_PASSWORDDairenwen1092-p3306:3306-dmysql:5.7 02f1cc5416a5cf454f97d59b173d72dcdc9556218a55f83036ff3c880ece3267进入容器登录可以看到已经可以正常登录 mysql 了drwVM-0-14-ubuntu:~$ dockerexec-it mysql bash bash-4.2# mysql -u root -pEnter password: Welcometothe MySQL monitor.Commandsendwith;or\g.Your MySQL connection idis2Server version:5.7.44MySQL Community Server(GPL)Copyright(c)2000,2023,Oracleand/orits affiliates.Oracleisa registered trademarkofOracle Corporationand/orits affiliates.Other names may be trademarksoftheir respective owners.Typehelp;or\hforhelp.Type\ctoclear thecurrentinput statement.mysqlshowdatabases;--------------------|Database|--------------------|information_schema||mysql||performance_schema||sys|--------------------4rowsinset(0.00sec)mysqlRedis 容器化安装下载 redis 镜像drwVM-0-14-ubuntu:~$dockerpull redis:8.08.0: Pulling from library/redis 205c6f67f159: Pull complete 8c8d18d687a1: Pull complete cc56679b8a07: Pull complete 4f4fb700ef54: Pull complete c99b74b46dd2: Pull complete da539b676105: Pull complete b07b7caef816: Pull complete 68d9479f5b3d: Download complete bc43288660d1: Download complete Digest: sha256:c6a3c149aae23f911f2f4dbd9d144718f802046b6f1706d4abf4ff84cc2792aa Status: Downloaded newer imageforredis:8.0 docker.io/library/redis:8.0启动 redis 容器drwVM-0-14-ubuntu:~$dockerrun-d--nameredis-p8300:6379 redis:8.0 9671848c75fac867b04f0e3b130eccc677919f4ece6e876c101464a0448e5442进入容器设置一个 keydrwVM-0-14-ubuntu:~$ dockerexec-it redis bash root9671848c75fa:/data# redis-cli127.0.0.1:6379ping PONG127.0.0.1:6379setkey1 helloredis OK127.0.0.1:6379get key1helloredis127.0.0.1:6379制作 C 容器下载 Ubuntu 镜像root139-159-150-152:/data/myworkdir/container# docker pull ubuntu:22.0422.04: Pulling from library/ubuntu 2ab09b027e7f: Pull complete Digest: sha256:67211c14fa74f070d27cc59d69a7fa9aeff8e28ea118ef3babc295a0428a6d21 Status: Downloaded newer imageforubuntu:22.04 docker.io/library/ubuntu:22.04启动容器root139-159-150-152:/data/myworkdir/container# docker run --name mygcc -it ubuntu:22.04 bashroot50d527b8343e:/#配置国内镜像源加速root50d527b8343e:/# sed -i s//.*archive.ubuntu.com//mirrors.ustc.edu.cng /etc/apt/sources.listroot50d527b8343e:/#安装 gcc vimaptupdateaptinstallgccvim-y编辑代码root50d527b8343e:/# mkdir srcroot50d527b8343e:/# cd srcroot50d527b8343e:/src# vim demo.c输入以下内容#includestdio.hintmain(){printf(hello docker!\n);return0;}编译代码root50d527b8343e:/src# gcc demo.c -o demo在容器中运行root50d527b8343e:/src# ./demohello docker!退出容器root50d527b8343e:/src# exitexit这个时候容器已经退出如果想再次进入使用docker restart就可以恢复容器清理容器释放空间dockerstop mygccdockerrmmygcc容器资源更新运行一个nginxdockerrun-d--namemynginx-p8086:80 nginx:1.24.0我们可以通过docker top查看容器中有哪些进程添加aux可以看到占用的内存和CPU信息drwVM-0-14-ubuntu:~$dockertopmynginx auxUSERPID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root33319980.00.189326144? Ss22:400:00 nginx: master process nginx-gdaemon off;message33320760.00.093202740? S22:400:00 nginx: worker process message33320770.00.093202740? S22:400:00 nginx: worker process message33320780.00.093202740? S22:400:00 nginx: worker process message33320790.00.093202740? S22:400:00 nginx: worker process通过docker stats可以看到资源的动态变化drwVM-0-14-ubuntu:~$dockerstats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 6b2d1942351c mynginx0.00%5.012MiB /3.636GiB0.13% 726B / 126B1.2MB /12.3kB5通过docker update更新docker的最大内存drwVM-0-14-ubuntu:~$dockerupdate-m100m --memory-swap 10m mynginx Error response from daemon: Minimum memoryswap limit should be larger than memory limit, see usage系统提示设置的内存应该大于已经limit的内存这说明在docker run的时候需要提前设置内存否则不方便调整常见问题docker create、docker start和docker run有什么区别docker create从Docker映像创建一个全新的容器。但不会立即运行它。docker start将启动任何已停止的容器。如果使用docker create命令创建容器则可以使用此命令启动它。docker run是创建和启动的组合如果docker run在本地镜像仓库找不到上述映像它可以从Docker Hub中提取映像。docker import和docker load有什么区别想要了解docker load与docker import命令的区别还必须知道docker save与docker export命令docker save images_name将一个镜像导出为文件。再使用docker load命令将文件导入为一个镜像会保存该镜像的所有历史记录。比docker export命令导出的文件大会保存镜像的所有历史记录。docker export container_id将一个容器导出为文件再使用docker import命令将容器导入成为一个新的镜像但是相比docker save命令容器文件会丢失所有元数据和历史记录仅保存容器当时的状态相当于虚拟机快照。docker rm docker rmi docker prune的差异docker rm删除一个或多个容器docker rmi删除一个或多个镜像docker prune用来删除不再使用的docker对象