一、上次课回顾
二、Docker
容器是基于CentOS7.X来构建的,在一个CentOS系统中可以装很多个docker实例,docker1、docker2、docker3
image:镜像,比如说我们虚拟机安装CentOS的时候有iso镜像系统,这个镜像系统可以安装很多个实例,Container:容器,以进程的形式运行,一个容器中只能有一个进程;CentOS系统中可以安装无数个进程:mysql、nginx、http;然而容器中只能运行一个进程服务。
部署网址:https://docs.docker.com/get-started/
才过去没多久,容器官网已经做了更新:
Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries.(docker提供了一种方式安全的运行应用单独的在一个容器上,单独的把所有的依赖包都打包)
OS requirements:To install Docker Engine - Community, you need a maintained version od CentOS7. Archived versions aren’t supported or tested.
The centos-extras repository must be enabled. This repository is enabled by default, but if you have disabled it, you need to re-enable it.
The overlay2 storage driver is recommended.
Older versions of Docker were called docker or docker-engine. If these are installed, uninstall them, along with associated dependencies.
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
1、启动docker容器:
[root@hadoop ~]# systemctl start docker
2、启动之后查看容器状态:
[root@hadoop ~]# systemctl status docker
docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2020-04-06 22:52:55 EDT; 10s ago
Docs: https://docs.docker.com
Main PID: 17549 (dockerd)
Memory: 43.0M
CGroup: /system.slice/docker.service
└─17549 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/contai...
3、centos6.x和centos7.x的区别:
centos6.x中只能对于单个服务进行启动,而centos7.x中可以对多个服务进行操作:
[root@hadoop ~]# which docker
/usr/bin/docker
[root@hadoop ~]# docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:27:04 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:25:42 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
1、部分先后的卸载两个服务:
[root@hadoop ~]# rpm -e docker-ce-cli-19.03.8-3.el7.x86_64
error: Failed dependencies:
docker-ce-cli is needed by (installed) docker-ce-3:19.03.8-3.el7.x86_64
[root@hadoop ~]# rmp -e docker-ce-19.03.8-3.el7.x86_64
-bash: rmp: command not found
[root@hadoop ~]# rpm -e docker-ce-19.03.8-3.el7.x86_64
[root@hadoop ~]# rpm -e docker-ce-cli-19.03.8-3.el7.x86_64
2、彻底清除/var/lib/docker的文件残留:
rm -rf /var/lib/docker/*
3、再重新安装下服务即可:
yum install -y docker-ce docker-ce-cli containerd.io
1、查看命令帮助:docker --help,docker search nginx
[root@hadoop ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 12940 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1766 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 764 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 102
bitnami/nginx Bitnami nginx Docker Image 81 [OK]
2、我们知道镜像名字了,怎么download到本地呢,docker pull nginx,通过网络去下载:
[root@hadoop ~]# docker run --name ruozedata-nginx-g6-1 -d -p 881:80 nginx
d46f5df803f99669559817ba330fc86ea021789c45c6d80a76fad4422ff3eb33
[root@hadoop ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d46f5df803f9 nginx "nginx -g 'daemon of…" 58 seconds ago Up 57 seconds 0.0.0.0:881->80/tcp ruozedata-nginx-g6-1
2、打开网络端口进行查看:
144.34.179.161:881
3、进入容器实例:跟上的是docker的实例id,/bin/bash是跟上的解释器:
[root@hadoop ~]# docker exec -it d46f5df803f9 /bin/bash
root@d46f5df803f9:/#
4、容器就是一个很微小的linux系统:
[root@hadoop ~]# ps -ef|grep docker
root 19194 1 0 02:06 ? 00:00:08 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 19509 19194 0 02:23 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 881 -container-ip 172.17.0.2 -container-port 80
root 19515 17548 0 02:23 ? 00:00:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/moby/d46f5df803f99669559817ba330fc86ea021789c45c6d80a76fad4422ff3eb33 -address /run/containerd/containerd.sock -containerd-binary /usr/bin/containerd -runtime-root /var/run/docker/runtime-runc
root 19662 19618 0 02:32 pts/0 00:00:00 grep --color=auto docker
[root@hadoop ~]# docker exec -it d46f5df803f9 /bin/bash
1、创建目录,安装lrzsz服务
[root@hadoop ~]# mkdir -p docker/nginx/html
[root@hadoop ~]# cd docker/nginx/html
[root@hadoop html]# rz
-bash: rz: command not found
[root@hadoop html]# yum install -y lrzsz
2、生产上通过浏览器打开网页是否正常,也可以使用curl看返回是否正常(对于http网址而言它返回的是网页信息就说明没问题)
<html>
<body>
<p id="p1">hello ruoze</p>
<a href="http://www.ruozedata.com">www.ruozedata.com</a>
</body>
</html>
3、
[root@hadoop html]# ll
total 4
-rw-r--r-- 1 root root 118 Apr 7 02:59 index.html
4、开始运行:-d表示后台运行,-p表示映射端口号(80端口号映射为882),-v:把本地的文件或文件夹挂载到容器中,最后的参数ro或rw控制挂载是否可读写:
docker run --name ruozedata-nginx-g6-2 \
-d -p 882:80 \
-v $PWD/html:/usr/share/nginx/html:ro \
nginx:latest
docker run --name ruozedata-nginx-g6-2 -d -p 882:80 -v $PWD/html:/usr/share/nginx/html:ro nginx:latest
5、docker ps:查看进程是否启动了:
注意:一个docker运行的时候是以单进程的形式,运行mysql就不能运行nginx;
我们所有的镜像都是通过官方给download下来的:
[root@hadoop nginx]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:f9dfddf63636d84ef479d645ab5885156ae030f611a56f3a7ac7f2fdd86d7e4e
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@hadoop nginx]# 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.
- docker客户端和服务端都要安装好
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
- docker从docker hub上把镜像拉取下来:
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
- docker进程创建一个新的容器从镜像文件上
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/
学习容器最终的目的:J总公司http服务、nginx、tomcat都是用docker来用的,docker上的mysql用于开发人员、测试人员测试(生产mysql库不可能使用docker,在vm上装了容器,容器上部署mysql;这样多加了一层转发,性能会多消耗20%);确定涉不涉及生产上存储的组件,这种情况是不能使用mysql的:
1、docker下载mysql5.7
docker pull mysql:5.7
2、[root@hadoop001 html]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 413be204e9c3 7 days ago 456MB
3、显示所有曾经运行过的和没运行过的组件服务:(hello-world是运行完后即退出的一个组件服务)
[root@hadoop001 html]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4、注意:要指定mysql的版本,否则下载的还是最新版本:
[root@hadoop001 docker]# docker run --name ruozedata-mysql-g6-1 -e MYSQL_ROOT_PASSWORD=960210 -d mysql:5.7
c9a6acf62eb3766c91bb4fa432e7e3918ae4fb5d79b0ab636cfb54af3ba20fd7
5、检查进程:
[root@hadoop001 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9a6acf62eb3 mysql:5.7 "docker-entrypoint.s鈥 About a minute ago Up About a minute 3306/tcp, 33060/tcp ruozedata-mysql-g6-1
6、运行启动mysql
[root@hadoop001 ~]# docker exec -it c9a6acf62eb3 /bin/bash
root@c9a6acf62eb3:/# mysql -uroot -p
7、端口号没有映射出去是没有用的:
只能通过docker exec -it container_id
需要做一个端口号映射,将内部的端口号映射为内部的端口号,此时外部的dbeaver能够对它进行访问:
docker run --name ruozedata-mysql-g6-2 -e MYSQL_ROOT_PASSWORD=960210 -p 3306:3306 -d mysql:5.7
查看端口号:[root@hadoop001 ~]# netstat -nlp|grep 3306
tcp6 0 0 :::3306 :: LISTEN 1210/docker-proxy
[root@hadoop001 ~]# ps -ef|grep 1210
root 1210 31467 0 15:51 ? 00:00:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 3306 -container-ip 172.17.0.2 -container-port 3306
root 1381 1335 0 15:56 pts/0 00:00:00 grep --color=auto 1210
这个过程十分简单,如果他需要一个固定的数据库,exec进入到容器,自行创建一个数据库,
create database ruozedata;
grant all privileges on . to rz@’%’ identified by ‘960210’;
flush privileges;
生产环境的docker用docker有点难,mysql本来就在linux上,mysql塞到docker容器的镜像实例中,网络转换的消耗是很多的,进程挂了,只要磁盘文件还在是能够启动恢复的,在生产环境中,一些重要的组件还是不建议使用docker的;容器中转发网络的消耗差不多在20%
如何制作Dockerfile文件构建docker image?
1、[root@hadoop001 5.7]# ll
total 20
-rw-r–r-- 1 root root 12934 Apr 7 16:19 docker-entrypoint.sh
-rw-r–r-- 1 root root 3884 Apr 7 16:17 Dockerfile
VOLUME /var/lib/mysql -->表示挂载到这个文件夹下:
2、如下这句命令进行自行编译:一个点表示在当前路径:
[root@hadoop001 5.7]# docker build . -t mysql:rz
3、构建镜像的目的就是:单独做你差异化的东西即可:
编写Dockerfile的步骤:
1、From 基础镜像
2、MAINTAINER 维护者信息
3、RUN镜像的Linux操作命令:创建文件夹,安装包,安装组件
容器启动进行一些初始化的操作,
4、
每个Dockerfile文件只能有一个ENTRYPOINT,也可以指定多个,只有最后一个生效,就是做了一些初始化的动作(就像删空账号空密码,创建常规库)
5、EXPOSE指的是运行的端口号:比较有意思的是它暴露了两个端口
CMD启动进程服务
千万不要拿docker来跑docker,企业中比较用的多的是Kubernetes,指的数据本地化,DataNode和yarn的NodeManager是在一个机器上的;spark程序在做计算的时候,会有task的级别,
Data Locality: