惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

宋浩志的博客

Windows安装nacos Edge用户登录一直转圈 SpringBoot整合Socket 使用coding持续集成SpringBoot项目 使用acme.sh来申请SSL证书 CentOS安装Nginx 使用Windows内置虚拟机Hyper-v安装CentOS ruoyi-vue项目集成flyway实现自动创建表 vue学习笔记四-列表渲染 vue学习笔记三条件渲染 vue学习笔记二模板语法 vue学习笔记一创建项目
Docker常用命令记录
宋浩志 · 2022-09-07 · via 宋浩志的博客

本篇文章以安装MySQL数据库为例子,记录Docker常用命令

1
yum install docker -y

docker容器互联

新建网络

1
docker network create my-net

列出所有网络

1
docker network ls

将容器连接到网络

1
docker network connect my-net mysql

断开容器的网络

1
docker network disconnect my-net mysql

删除一个或多个网络

1
docker network rm my-net

docker客户端

容器使用

搜索镜像

这里我们搜索mysql镜像

1
docker search mysql

获取MySQL镜像

下载需要的版本docker pull mysql:tag

tag代表版本号,没有代表是lastest

1
docker pull docker.io/mysql:5.7.26

列出本地镜像

1
docker images [OPTIONS] [REPOSITORY[:TAG]]

OPTIONS说明

  • -a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);

  • –digests :显示镜像的摘要信息;

  • -f :显示满足条件的镜像;

  • –format :指定返回值的模板文件;

  • –no-trunc :显示完整的镜像信息;

  • -q :只显示镜像ID。

创建 MySQL 数据目录

1
mkdir -p /opt/mysql

启动MySQL实例

1
docker run --name mysql -p 3306:3306 --privileged=true -v /opt/mysql/log:/var/log/mysql -v /opt/mysql/conf:/etc/mysql -v /opt/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --net my-net --restart=unless-stopped -d mysql:5.7.26

Tips

  • -e MYSQL_ROOT_PASSWORD=123456命令: 指定MySQL的登录密码为 123456

  • -v /opt/mysql/data:/var/lib/mysql 命令: 将宿主机的目录 /opt/mysql 挂载到容器内部的目录 /var/lib/mysql,默认情况下 MySQL 将向 /opt/mysql 写入其数据文件。

  • -v /opt/mysql/conf:/etc/mysql命令:MySQL配置文件存放位置

  • -v /opt/mysql/log:/var/log/mysql命令:MySQL日志文件

  • --net my-net命令: 将该容器加入到 my-net 网络,连接到 my-net 网络的任何其他容器都可以访问 mysql 容器上的所有端口。

  • --restart=unless-stopped命令:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器

  • -d命令:表示后台运行

  • -p 3306:3306命令:端口映射

  • --privileged=true命令:container内的root拥有真正的root权限

查看docker容器运行情况

1
docker ps

查看所有的容器

1
docker ps -a

启动一个已停止的容器

1
docker start器id

停止一个容器

1
docker stop <容器 ID>

停止的容器可以通过 docker restart 重启:

1
docker restart <容器 ID>

进入MySQL容器

1
docker exec -it mysql /bin/bash

Tips

  • -i: 交互式操作。
  • -t: 终端。
  • mysql: mysql 镜像。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

退出容器

1
exit

参考

https://www.runoob.com/docker/docker-tutorial.html

https://docs.halo.run/getting-started/install/other/docker-mysql

https://www.yiibai.com/docker/docker-introduction.html

https://www.jianshu.com/p/68ec752f0454