Docker
Docker
Install Docker Desktop on Linux
环境:CentOS-7
1、卸载旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine```
2、安装方式
- 为了方便安装和升级任务,采用建立Docker仓库的方式安装
- download the RPM package and install it manually and manage upgrades completely manually on systems with no access to the internet 没网
- 在测试开发环境、采用自动化便捷的脚本方式安装
建立Docker仓库
yum install -y yum-utils #Install the yum-utils package
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo #国外的
http://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo #华为云
安装docker引擎
yum makecache fast #更新一下yum软件包索引
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin #ce社区 ee企业
Start Docker
systemctl start docker
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:e18f0a777aefabe047a671ab3ec3eed05414477c951ab1a6f352a06974245fe7
Status: Downloaded newer image for 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 bash
Share 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/
问题:出现 Pulling fs layer
解决:systemctl restart docker
重启docker服务
卸载docker
yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
设置镜像加速器(华为云)
登录华为云网站、注册云账户》点击右上角控制台》点击左上角服务列表》点击容器镜像服务SWR>添加镜像加速器
以root用户登录容器引擎所在的虚拟机
修改“/etc/docker/daemon.json”文件(如果没有,可以手动创建),在该文件内添加如下内容:
vi /etc/docker/daemon.json
{
"registry-mirrors":["https://cb2a62827...98366cf39f77.mirror.swr.myhuaweicloud.com" ]
}
systemctl daemon-reload
systemctl restart docker
如果重启失败,则检查操作系统其他位置(如:/etc/sysconfig/docker、/etc/default/docker)是否配置了registry-mirrors参数,删除此参数并重启容器引擎即可
什么是容器
容器是机器上的沙箱进程,它与主机上的所有其他进程隔离:
- 是镜像的可运行实例,可以使用 DockerAPI 或 CLI 创建、启动、停止、移动或删除容器
- 可以在本地机器、虚拟机上运行或部署到云中
- 是可移植的(可以在任何操作系统上运行)
- 与其他容器隔离,并运行自己的软件、二进制文件和配置。
什么是镜像
当运行一个容器时,它使用一个独立的文件系统。这个自定义文件系统由一个容器映像提供。因为映像包含容器的文件系统,所以它必须包含运行应用程序所需的所有东西——所有依赖项、配置、脚本、二进制文件等等。该映像还包含容器的其他配置,如环境变量、要运行的默认命令和其他元数据。
在本指南的后面,您将更深入地研究图像,包括分层、最佳实践等主题。
将一个app装入容器
获取app
git clone https://github.com/docker/getting-started.git
创建镜像
为了构建容器映像,需要使用 Dockerfile。Dockerfile 只是一个没有文件扩展名的文本文件。Dockerfile 包含 Docker 用于创建容器映像的指令脚本
cd /path/to/app
touch Dockerfile
# add the following contents to the Dockerfile:
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
cd /path/to/app
docker build -t getting-started . #Build the container image
Start
docker run -dp 3000:3000 getting-started
# After a few seconds, open your web browser to http://localhost:3000.
Docker常用命令
帮助命令
docker version # 显示docker的版本信息
docker info
docker --help
文档地址:Reference documentation | Docker Documentation
镜像命令
docker images 产看本地所有镜像
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
node 12-alpine 00881b4ed7f5 19 months ago 88.9MB
REPOSITORY 镜像仓库源
TAG 标签
IMAGE ID id
CREATED 创建时间
SIZE 大小
docker images --help
[root@localhost /]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
docker search 搜素镜像
[root@localhost /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13424 [OK]
mariadb MariaDB Server is a high performing open sou… 5118 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 673 [OK]
percona Percona Server is a fork of the MySQL relati… 592 [OK]
bitnami/mysql Bitnami MySQL Docker Image 78 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 74
linuxserver/mysql-workbench 45
ubuntu/mysql MySQL open source fast, stable, multi-thread… 38
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 37
circleci/mysql MySQL is a widely used, open-source relation… 28
google/mysql MySQL server for Google Compute Engine 21 [OK]
rapidfort/mysql RapidFort optimized, hardened image for MySQL 13
bitnami/mysqld-exporter 4
ibmcom/mysql-s390x Docker image for mysql-s390x 2
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
vitess/mysqlctld vitess/mysqlctld 1 [OK]
hashicorp/mysql-portworx-demo 0
docksal/mysql MySQL service images for Docksal - https://d… 0
rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 0
mirantis/mysql 0
cimg/mysql 0
drud/mysql 0
silintl/mysql-backup-restore Simple docker image to perform mysql backups… 0 [OK]
corpusops/mysql https://github.com/corpusops/docker-images/ 0
drud/mysql-local-57 ddev mysql local container 0
docker pull mysql #下载镜像
docker pull mysql:5.7 #指定版本
删除镜像
docker rmi -f $(docker images -aq)
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
node 12-alpine 00881b4ed7f5 19 months ago 88.9MB
mysql latest 8457e9155715 20 months ago 546MB
[root@localhost /]# docker rmi -f feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:e18f0a777aefabe047a671ab3ec3eed05414477c951ab1a6f352a06974245fe7
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
容器命令
容器以镜像为基础运行
下载一个centos镜像
docker pull centos
启动
docker run [可选参数] image
#参数
--name = "Name" 容器名字
-d 后台方式
-it 交互式运行
-p 制动端口 主机端口:容器端口
-P 随机指定端口
# 启动、进入容器
[root@localhost /]# docker run -it centos /bin/bash
[root@33ecabd1756c /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@33ecabd1756c /]# exit #退出
exit
列出所有的运行容器
docker ps
列出正在运行的容器
-a 列出正在运行的容器+历史运行过的
-n = ? 最近几个
-q 只显示容器的编号
退出容器
exit 容器停止并退出
ctrl + p + q 容器不停止并退出
删除容器
docker rm 容器id -f 强制删除运行的容器
docker rm -f $(docker ps -aq) #删除所有
docker ps -a -q |xargs docker rm #删除所有
启动和停止容器
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
其他命令
后台启动 docker run -d ID
查看日志 docker logs
进程信息 docker top ID
显示容器信息 docker inspect ID
进入当前正在运行的容器 docker exec -it ID bashshell // docker attach ID
前者进入后开启一个新终端、后者进入正在执行的终端
容器内文件拷贝到主机上 docker cp ID:路径/文件 主机目录
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~