目录
一、安装
参考菜鸟教程 CentOS Docker 安装 | 菜鸟教程
1.执行自动安装
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
结果:
# Executing docker install script, commit: 26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c
Warning: the "docker" command appears to already exist on this system.
If you already have Docker installed, this script can cause trouble, which is
why we're displaying this warning and provide the opportunity to cancel the
installation.
If you installed the current Docker package using this script and are using it
again to update Docker, you can safely ignore this message.
You may press Ctrl+C now to abort this script.
+ sleep 20
+ sh -c 'yum install -y -q yum-utils'
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
+ sh -c 'yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo'
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
+ '[' stable '!=' stable ']'
+ sh -c 'yum makecache'
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
jenkins | 2.9 kB 00:00:00
mysql-connectors-community | 2.5 kB 00:00:00
mysql-tools-community | 2.5 kB 00:00:00
mysql57-community | 2.5 kB 00:00:00
nginx | 2.9 kB 00:00:00
nodesource | 2.5 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/23): base/7/x86_64/filelists_db | 7.1 MB 00:00:00
(2/23): epel/x86_64/prestodelta | 75 B 00:00:00
(3/23): base/7/x86_64/other_db | 2.6 MB 00:00:00
(4/23): epel/x86_64/other_db | 3.3 MB 00:00:00
(5/23): extras/7/x86_64/filelists_db | 217 kB 00:00:00
(6/23): docker-ce-stable/x86_64/filelists_db | 21 kB 00:00:00
(7/23): extras/7/x86_64/other_db | 125 kB 00:00:00
(8/23): docker-ce-stable/x86_64/other_db | 114 kB 00:00:00
(9/23): epel/x86_64/filelists_db | 12 MB 00:00:00
(10/23): mysql-connectors-community/x86_64/other_db | 17 kB 00:00:00
(11/23): mysql-tools-community/x86_64/other_db | 17 kB 00:00:00
(12/23): mysql-connectors-community/x86_64/filelists_db | 84 kB 00:00:00
(13/23): mysql57-community/x86_64/other_db | 58 kB 00:00:00
(14/23): mysql57-community/x86_64/filelists_db | 1.0 MB 00:00:00
(15/23): mysql-tools-community/x86_64/filelists_db | 266 kB 00:00:00
(16/23): jenkins/other_db | 249 kB 00:00:01
(17/23): nginx/x86_64/filelists_db | 76 kB 00:00:01
(18/23): updates/7/x86_64/filelists_db | 1.7 MB 00:00:00
(19/23): jenkins/filelists_db | 95 kB 00:00:01
(20/23): updates/7/x86_64/other_db | 250 kB 00:00:00
(21/23): nodesource/x86_64/filelists_db | 642 kB 00:00:01
(22/23): nodesource/x86_64/other_db | 6.4 kB 00:00:01
(23/23): nginx/x86_64/other_db | 31 kB 00:00:02
Loading mirror speeds from cached hostfile
Metadata Cache Created
+ '[' -n '' ']'
+ sh -c 'yum install -y -q docker-ce'
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
/usr/bin/dockerd has not been configured as an alternative for dockerd
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker your-user
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
2.验证
(1)启动和关闭docker
# 启动docker
sudo systemctl start docker
# 关闭docker服务
sudo systemctl stop docker
(2)执行验证
sudo docker run hello-world
验证结果:
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/
3. 配置镜像加速器
(1)打开文件 /etc/docker/daemon.json
改前是这样的(默认镜像地址):
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
(2)登陆阿里云-控制台-容器镜像服务配置
复制加速器地址,配置到 /etc/docker/daemon.json 中
4. 设置开机自启
# 开启自启
sudo systemctl enable docker
# 关闭自启
sudo systemctl disable docker
二、常用命令
# 搜镜像
docker search mysql
# 从Docker Hub查找所有镜像名包含mysql,并且收藏数大于10的镜像
docker search -s 10 mysql
# 查看已下载镜像
docker images
# 删除容器
docker rm <容器名称 or ID>
# 进入容器
docker exec -it <容器名称 or ID> bash
# 退出容器
exit
# 查看正在运行的容器
docker ps
# 查看所有容器
docker ps -a
# 删除所有容器
docker rm $(docker ps -a -q)
# 启动、停止、杀死指定容器
docker start <容器名称 or ID>
docker stop <容器名称 or ID>
docker kill <容器名称 or ID>
# 后台运行 docker run -d <其他参数>
docker run -d -p 127.0.0.1:3306:3306 <容器名称 or ID>
# 容器自动启动
# 1.创建容器时自动启动
docker run --restart=always <容器名称 or ID>
# 2.创建时未指定,可通过update命令设置
docker update --restart=always <容器名称 or ID>
# 暴露端口
# 1. 映射 指定地址的 3306 到 容器3306
docker -p 127.0.0.1:3306:3306
# 2. 映射 指定地址的 随机端口 到 容器3306
docker -p 127.0.0.1::3306
# 3. 映射 本机的 3306 到 容器3306
docker -p 3306:3306
# 映射 本机数据卷(文件夹)到 容器(文件夹)-> 本机目录:容器目录
docker -v /home/data:/opt/data
参考:linux安装docker 让docker 容器开机自动启动
三、使用GUI管理Docker
1.安装GUI
使用 Portainer 作为容器的 GUI 管理方案,就可以在浏览器中管理Docker
官方地址:https://portainer.io/install.html
安装命令如下,什么是docker volume??
# 创建主机上的的容器公共可操作卷
docker volume create portainer_data
# 运行Portainer容器
docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
# docker启动时自动启动该容器
docker update --restart=always <容器名称 or ID>
访问你的 IP:9000 即可进入容器管理页面。
2.使用GUI
1.安装mysql -- 参考 Portainer 安装MySQL并开启远程访问
MYSQL_DATABASE (选配)
MYSQL_ROOT_PASSWORD (必须配)
注意都是大写
用mysql8.0连接时遇到了问题:
解决办法:docker安装 mysql 8.0后,客户端连接不上_容器安装mysql客户端-CSDN博客
原因:(上面为第二次安装)
因为我第一次安装的是mysql的最后一个版本即8.0,所以 -> 最简单解决docker mysql8 登陆认证失败的方法 - 简书
其他可能我没踩到的坑:
Docker搭建Mysql教程 与踩坑问题解决 - 学习二 - 简书
https://www.cnblogs.com/yy-cola/p/11226924.html
四、比较一下
使用前 486
使用docker中mysql,占用内存为(634-486)
使用主机中的mysql,占用内存为(656-463)
可能由于主机中mysql里有数据,所以好像差的不是很多呢,,
不过docker镜像里的容器类型挺多的,可以多用一下这种沙箱式的开发,开启小船之旅GOGOGO