Docker 是一个开放源代码软件,是一个开放平台,用于开发应用、交付(shipping)应用、运行应用。 Docker允许用户将基础设施(Infrastructure)中的应用单独分割出来,形成更小的颗粒(容器),从而提高交付软件的速度。[1]
Docker容器与虚拟机类似,但二者在原理上不同。容器是将操作系统层虚拟化,虚拟机则是虚拟化硬件,因此容器更具有便携性、高效地利用服务器。 容器更多的用于表示 软件的一个标准化单元。由于容器的标准化,因此它可以无视基础设施(Infrastructure)的差异,部署到任何一个地方。另外,Docker也为容器提供更强的业界的隔离兼容。[2]
Docker 利用Linux核心中的资源分离机制,例如cgroups,以及Linux核心名字空间(namespaces),来创建独立的容器(containers)。这可以在单一Linux实体下运作,避免引导一个虚拟机造成的额外负担[3]。Linux核心对名字空间的支持完全隔离了工作环境中应用程序的视野,包括行程树、网络、用户ID与挂载文件系统,而核心的cgroup提供资源隔离,包括CPU、存储器、block I/O与网络。从0.9版本起,Dockers在使用抽象虚拟是经由libvirt的LXC与systemd - nspawn提供界面的基础上,开始包括libcontainer库做为以自己的方式开始直接使用由Linux核心提供的虚拟化的设施
安装docker 卸载旧版本较旧的Docker版本称为docker或docker-engine。如果已安装这些程序,请卸载它们以及相关的依赖项。
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
如果yum报告未安装这些软件包,则可以。
的内容(/var/lib/docker/包括图像,容器,卷和网络)被保留。Docker Engine软件包现在称为docker-ce。
使用存储库安装在新主机上首次安装Docker Engine之前,需要设置Docker存储库。之后,您可以从存储库安装和更新Docker。
设置存储库 安装yum-utils软件包(提供yum-config-manager 实用程序)并设置稳定的存储库。
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安装Docker引擎
安装最新版本的Docker Engine和容器,或转到下一步以安装特定版本:
sudo yum install docker-ce docker-ce-cli containerd.io
如果提示您接受GPG密钥,请验证指纹是否匹配 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
,如果是,则接受它。
启动Docker。
sudo systemctl start docker
通过运行hello-world 映像来验证是否正确安装了Docker Engine 。
sudo docker run hello-world
执行结果如下:
[root@localhost ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
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/
说明docker已经安装成功啦!
docker中安装mysql 使用docker安装mysqlhttps://hub.docker.com/_/mysql?tab=description
安装mysql镜像
docker pull mysql
启动mysql服务
sudo docker run --name first-mysql -p 3307:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql
参数标题run运行一个容器–name后面是这个镜像的名称-p 3307:3306表示在这个容器中使用3306端口(第二个)映射到本机的端口号也为3307(第一个)-d表示使用守护进程运行,即服务挂在后台
查看运行状态
docker ps
客户端连接MySQL
使用IDEA连接
因为我服务器的ip是172.16.184.5,连接配置如下,密码是上面设置的123456
docker run -it --rm mysql mysql --port=3307 -h172.16.184.5 -uroot -p123456
参数标题-h172.16.184.5172.16.184.5
修改成你的ip-urootroot
修改成你要登录的用户名-p123456123456
登录密码–port=3307访问端口3307
mysql基本操作
- 显示数据库列表。
show databases;
- 显示库中的数据表:
use mysql;
show tables;
- 显示数据表的结构:
describe 表名;
- 建库:
create database 库名;
- 建表:
use 库名;
create table 表名 (字段设定列表);
- 删库和删表(谨慎操作):
drop database 库名;
drop table 表名;
- 将表中记录清空:
delete from 表名;
- 显示表中的记录:
select * from 表名
docker其他操作
显示正在运行的containers
docker ps
停止containers
docker stop first-mysql
运行已经stop的containers
docker start first-mysql
进入containers中
docker exec -it first-mysql bash
first-mysql
为容器名称。
docker ps -a
删除containers(注意⚠️)
docker rm first-mysql
总结
使用docker配置mysql很方便,特别是可以同时配置多个mysql服务,这就很强了!
参考或引用- https://zh.wikipedia.org/wiki/Docker
- https://docs.docker.com/engine/install/centos/