1.1 docker介绍
https://www.cnblogs.com/xiaonq/p/10241045.html#i4
1. 什么是docker
- Docker 是应用最广泛的开源容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中
- docker实质就像虚拟机一样,就好像是一个具有独立操作系统的真实机器
- 虚拟机是有真正的linux内核的,真实需要通过 .ios 文件安装操作系统
- 而我们的docker共享linux宿主机内核,只有一个linux文件系统
- 容器:
- 容器是一个操作系统级别下的虚拟化技术,运行一个容器就行运行一个进程一样
- 容器依赖linux内核特性:Namespace(资源隔离)和Cgroups(资源限制)
2. docker与虚拟机比较
1.2 docker镜像
1. 什么是docker镜像
- docker镜像不包含Linux内核而又精简的Linux操作系统
- 我们安装操作西宫iso文件就可以理解解为用C语言写的安装包(微信安装包)
- 我们安装操作系统的过程就好像是我们微信(就好像我们安装微信一样)
- docker镜像也像.iso安装包,但是因为操作系统已经安装linux内核,所以这个 安装包可以直接使用(这个安装包只有linux文件系统,而内核直接使用linux系统本身的就行)
- docker 镜像是 docker 容器的静态视角,docker 容器是 docker 镜像的运行状态
- 程序和进程的区别
- docker镜像----》程序(存储在我们硬盘里的代码)
- 进程-----》把我们硬盘里的代码加载到内存中运行
- 容器只是对docker镜像的引用,如果docker镜像删除,此镜像创建的容器也都失效
2. 容器读写层
-
容器其实是在镜像的最上面加了一层读写层,在运行容器里文件改动时,会先从镜像里要写的文件复制到容器自己的文件系统中(读写层)。
-
如果容器删除了,最上面的读写层也就删除了,改动也就丢失了。
-
所以无论多少个容器共享一个镜像,所做的写操作都是从镜像的文件系统中复制过来操作的,并不会修改镜像的源文件
-
若想持久化这些改动,可以通过docker commit 将容器保存成一个新镜像
1.3 docker安装
1、docker安装参考官方文档
-
docker官方文档:https://docs.docker.com/
centos安装docker:https://docs.docker.com/install/linux/docker-ce/centos/
注:docker CE只支持 centos7 不支持centos6
2、docker安装
# 1)安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 2)添加Docker软件包源(否则doker安装的不是新版本)
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 3)安装Docker CE
yum install -y docker-ce
# 4)启动Docker服务并设置开机启动
systemctl start docker
systemctl enable docker
# 5)测试docker是否安装成功(hello-world是官方提供的一个测试镜像)
docker run hello-world
# 6)查看docker基本信息
docker info
docker version
3、docker简单使用(创建一个ngixn容器)
# 1、创建一个nginx容器
docker run -it nginx
# 2、查看docker运行的容器(可以获取到这个容器的id)
docker ps
# 3、访问这个容器
# 进入这个nginx容器(进入的文件系统和宿主机是完全隔离的,有自己独立的文件系统)
docker exec -it 73877e65c07d bash
# 4、查看当前容器的 IP
docker inspect 73877e65c07d # 73877e65c07d是通过docekr ps查看到的容器ID
curl 172.17.0.2 # 测试这个nginx容器是否可以访问
1.4 docker常用管命令
1.镜像常用管理命令
[root@linux-node1 /]# docker image --help # 查docker使用方法
[root@linux-node1 /]# docker image ls # 查看本地下载的所有镜像
[root@linux-node1 /]# docker image rm nginx # 删除这个镜像
[root@linux-node1 /]# docker image inspect nginx # 查看nginx镜像详细信息
2.运行容器常用管理命令
[root@linux-node1 /]# docker run -itd nginx # 在后台启动一个docker容器
[root@linux-node1 /]# docker inspect 5a3ad030a621 # 查看docker运行容器的详细信息
[root@linux-node1 /]# curl http://172.17.0.2 #
[root@linux-node1 /]# docker run -itd --name=web2 -p 192.168.56.11:8800:80 nginx
使用--itd参数(指定后台启动这个容器),--name=web2(指定这个web容器的名字叫web2)
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P: 随机端口映射,容器内部端口随机映射到主机的高端口
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
--name="nginx-lb": 为容器指定一个名称
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
[root@linux-node1 /]# docker exec -it c9b7a480ee68 bash # 进入docker环境
1.5 dockerfile编写和应用(真实企业应用)
- dockerfile的作用
- 利用官方原始进行构建我们自己的镜像
1.dockerfile最简单使用
# Dockerfile
FROM nginx
COPY index.html /usr/share/nginx/html/index.html
# index.html
<h1>Hello 1904 logo</h1>
- 使用dockerfile构建我们自己的镜像
[root@linux-node1 nginx_v1]# docker build -t nginx:1904 -f Dockerfile .
build # 构建指令
-t nginx:1904 # 指定镜像的版本号(镜像名称:tag)
[root@linux-node1 nginx_v1]# docker image ls # 查看我们构建的镜像
[root@linux-node1 nginx_v1]# docker run -itd --name 1904 -p 192.168.56.11:1904:80 nginx:1904 # 使用我们镜像运行一个docker容器
http://192.168.56.11:1904 # 通过宿主机的映射ip地址+端口号就可以访问docker容器
2.dockerfile的其他语法
'''1.DOckerfile常用命令 '''
FROM python:3.6 # 指定拉取镜像版本
ENV PYTHONUNBUFFERED 1 # 不缓冲stdin、stdout和stderr,直接把输出重定向到文件
MAINITAINER zhangsan # 指定作者
RUN mkdri /code # 运行的linux命令
WORKDIR /code # 指定项目工作根路径
ADD . /code/ # 将宿主机文件复制到镜像中
COPY dj.conf /etc/nginx/conf.d # docker内部文件拷贝
VOLUME ["/data1","/data2"] # 将宿主机文件夹挂载到容器中
EXPOSE 8080 # 暴露端口
CMD ["sh","/code/start.sh"] # 容器启动时要运行的命令
CMD ["python", "manage.py", "runserver", "0:8000"]
'''2.生成镜像并运行容器'''
docker build -t nginx:v1 -f Dockerfile-nginx . # 使用Dockerfile-nginx文件生成镜像 nginx:v1
docker push linux-node4.example.com/test/nginx:v1 # 推送镜像到harbor中
docker run -d -p 192.168.56.14:8000:8080 nginx:v1 # 运行docker容器
1.6 docker中网络部分讲解
1.网络常识问题
- 连接到 同一交换机的 相同网段 可以直接相互通信
2.NAT和PAT
3.docker服务如何发布给别人使用
1.7 docker配置反向代理
-
运行一个容器
- 我自己的宿主机ip :192.168.142.132
-
我想在运行一个web2
- –name= web2
- 192.168.142.132:82
docker run -itd --name=web1 -p 192.168.142.132:81:80 nginx:latest
http://192.168.142.132:81/
docker run -itd --name=web2 -p 192.168.142.132:82:80 nginx:latest
http://192.168.142.132:82/
[root@linux-node2 hm]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
202391fbfd2c nginx:latest "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 192.168.142.132:82->80/tcp web2
de83d9328165 nginx:latest "nginx -g 'daemon of…" 59 minutes ago Up 59 minutes 192.168.142.13
[root@linux-node2 hm]# docker exec -it web1 bash
root@de83d9328165:/# cd /usr/share/nginx/html
root@de83d9328165:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@de83d9328165:/usr/share/nginx/html# echo 'web1' > index.html #修改index.html里的内容为web1
root@de83d9328165:/usr/share/nginx/html# cat index.html #查看是否修改
web1
root@de83d9328165:/usr/share/nginx/html# exit
exit
[root@linux-node2 hm]# docker exec -it web2 bash
root@202391fbfd2c:/# cd /usr/share/nginx/html
root@202391fbfd2c:/usr/share/nginx/html# ls
50x.html index.html
root@202391fbfd2c:/usr/share/nginx/html# echo web2 > index.html
root@202391fbfd2c:/usr/share/nginx/html# cat index.html
web2
root@202391fbfd2c:/usr/share/nginx/html#
cd /etc/nginx/conf.d
vim default.conf
cp default.conf demo.conf
# 将default.conf中的内容拷贝一份到demo.conf中vim demo.conf
upstream my1904{
server 192.168.56.14:81;
server 192.168.56.14:82;
}
server {
listen 4444;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://my1904;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
- 用命令
nginx -t
查看文件内是否有错
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok #文件没有错
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl reload nginx
重启nginx- 如果页面有错误,查看nginx错误日志
- 打开Nginx配置文件
vim /etc/nginx/nginx.conf
- 找到错误日志路径
- 检查Nginx配置文件语法是否有误,没有错误则重启
nginx -t
4. 重启nginx
service nginx restart
- 运行加载某个页面,如果报错了直接进入错误日志中进行查看
vim /var/log/nginx/error.log