用数据卷挂载:代码目录
用数据卷挂载:配置文件,配置文件目录
用数据卷挂载:数据目录(数据库)
还可以用于日志
nginx:alpine镜像
配置文件子: /app/docker/restart/conf/nginx/conf.d/ /etc/nginx/conf.d/
配置文件主: /app/docker/restart/conf/nginx/nginx.conf /etc/nginx/nginx.conf
站点目录: /app/docker/restart/code/ /app/code/restart/
mkdir -p /app/docker/conf/nginx/conf.d/ /app/docker/code/restart/
docker run -d --name "oldboy_restart_volume_v1" -p 80:80 \
-v /app/docker/restart/conf/nginx/conf.d/:/etc/nginx/conf.d/ \
-v /app/docker/restart/conf/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /app/docker/restart/code/:/app/code/restart/ \
--restart=always \
nginx:alpine
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~* \.(js|css)$ {
expires 30d;
}
}
1. 创建数据卷
docker volume create oldboylogdata
oldboylogdata
[root@docker01 ~]# docker volume create oldboy_data
oldboy_data
2. 查看数据卷
[root@docker01 ~]# ll /var/lib/docker/volumes/
总用量 24
brw------- 1 root root 8, 3 2024-04-18 08:27 backingFsBlockDev
-rw------- 1 root root 32768 2024-04-18 09:04 metadata.db
drwx-----x 3 root root 19 2024-04-18 09:04 oldboy_data
[root@docker01 ~]# ll /var/lib/docker/volumes/oldboy_data/
总用量 0
drwxr-xr-x 2 root root 6 2024-04-18 09:04 _data
[root@docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local oldboy_data
3. 挂载数据卷
[root@docker01 ~]# docker run -d --name "oldboy_volume_space" -v oldboy_data:/var/log/nginx/ nginx:alpine
811963109e713eab0066ab9e359741a6bd728372e643d8ca4230b4c38585cdde
[root@docker01 ~]# ll /var/lib/docker/volumes/oldboy_data/_data/
总用量 0
lrwxrwxrwx 1 root root 11 2021-12-30 03:29 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 2021-12-30 03:29 error.log -> /dev/stderr
[root@docker01 ~]# docker logs oldboy_volume_space
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/04/18 01:08:38 [notice] 1#1: using the "epoll" event method
2024/04/18 01:08:38 [notice] 1#1: nginx/1.21.5
2024/04/18 01:08:38 [notice] 1#1: built by gcc 10.3.1 20211027 (Alpine 10.3.1_git20211027)
2024/04/18 01:08:38 [notice] 1#1: OS: Linux 3.10.0-1160.108.1.el7.x86_64
2024/04/18 01:08:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/04/18 01:08:38 [notice] 1#1: start worker processes
2024/04/18 01:08:38 [notice] 1#1: start worker process 32
2024/04/18 01:08:38 [notice] 1#1: start worker process 33
2024/04/18 01:08:38 [notice] 1#1: start worker process 34
2024/04/18 01:08:38 [notice] 1#1: start worker process 35
⚠ 注意:
nginx容器的日志默认是输出到docker标准输出和标准错误输出的,而不是存放在文件中
未来查看日志的时候,不用进入到容器,然后tail/less使用命令查看
直接使用 docker logs 容器id或名字 就可以看日志.,相当于查看access.log或error.log
[root@docker01 ~]# docker logs -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Aliases:
docker container logs, docker logs
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)
[root@docker01 ~]# docker logs -f -n 100 oldboy_volume_space
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/04/18 01:08:38 [notice] 1#1: using the "epoll" event method
2024/04/18 01:08:38 [notice] 1#1: nginx/1.21.5
2024/04/18 01:08:38 [notice] 1#1: built by gcc 10.3.1 20211027 (Alpine 10.3.1_git20211027)
2024/04/18 01:08:38 [notice] 1#1: OS: Linux 3.10.0-1160.108.1.el7.x86_64
2024/04/18 01:08:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/04/18 01:08:38 [notice] 1#1: start worker processes
2024/04/18 01:08:38 [notice] 1#1: start worker process 32
2024/04/18 01:08:38 [notice] 1#1: start worker process 33
2024/04/18 01:08:38 [notice] 1#1: start worker process 34
2024/04/18 01:08:38 [notice] 1#1: start worker process 35
容器时区问题:
apk update \
&& apk add tzdata \
&& cp /usr/share/zoneinfo/Asia/shanghai /etc/localtime \
&& echo "Asia/Shanghai" >/etc/timezone
docker run -d --name "nginx_vol_oldboylogdatav2" -p :80 -v :/var/log/nginx/ nginx:1.20.2-alpine
更多数据卷用法:https://docs.docker.com/storage/volumes/
熟练掌握挂载指定的目录或文件到容器中即可
-v
docker volume
docker 镜像,容器,端口映射,数据卷挂载
目标:创建一个tengine镜像(手动),自动(Dockerfile)
1. 下载并启动ubuntu:20.04 容器 叫 ubt_tengine_2.3.3
2. 配置apt源
3. 下载软件包
4. 编译安装3步曲 ./configure ; make ; make install
5. 启动与测试
6. 清理痕迹
7. 生成镜像
docker run -itd --name "oldboy_tengine_bird_v1" ubuntu:20.04 /bin/bash
docker exec -it oldboy_tengine_bird_v1 /bin/bash
ll /etc/apt/sources.list
sed -ri 's#archive.ubuntu.com|security.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list
apt update #生成apt缓存
1.安装常用软件
apt install -y vim curl
2.下载软件包(手动下载然后从docker cp到容器中)
http://tengine.taobao.org/download/tengine-2.3.3.tar.gz
[root@docker01 ~]# docker cp tengine-2.3.3.tar.gz oldboy_tengine_bird_v1:/tmp/
Successfully copied 2.85MB to oldboy_tengine_bird_v1:/tmp/
安装依赖
./configure #进行编译安装的配置-->Makefile 用于编译.
make #编译(根据Makefile配置进行编译)
make install #创建目录,复制文件ՎՎʢ
1. 安装依赖
apt install -y libssl-dev make gcc pcre2-utils libpcre3-dev zlib1g-dev
#openssl-devel
2. 进行配置(指定用户,指定安装目录,添加或开启模块)
解压源码包,进入解压目录(略)
#--prefix=/app/tools/tengine-2.3.3/ tengine的安装目录,安装到系统的那个目录中,自动创建.
./configure --prefix=/app/tools/tengine-2.3.3/ \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_mp4_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=modules/ngx_http_upstream_check_module/ \
--add-module=modules/ngx_http_upstream_session_sticky_module
echo $? #上一个命令的执行结果 ,如果是0表示成功,非0就是失败
3. make编译
make -j 1
4. make install编译安装
make install
5. 检查
/app/tools/tengine-2.3.3/sbin/nginx -V
6. 创建软连接
ln -s /app/tools/tengine-2.3.3/ /app/tools/tengine
温馨提示: 如何知晓编译安装的依赖? 李导答:你别安装然后直接去编译.
温馨提示: echo $? #上一个命令的执行结果 ,如果是0表示成功,非0就是失败
1. 添加用户nginx
2. 创建安装目录软连接
3. nginx命令软连接到/sbin/下面.
4. 启动与本地测试.
1. 添加用户nginx
groupadd nginx
useradd -s /sbin/nologin -g nginx nginx
2. 创建安装目录软连接
ln -s /app/tools/tengine-2.3.3/ /app/tools/tengine
3. nginx命令软连接到/sbin/下面.
ln -s /app/tools/tengine/sbin/nginx /sbin
nginx -V
4. 启动与本地测试.
nginx #启动后会在后台运行.
echo docker-teninge.oldboylinux.cn >/app/tools/tengine/html/index.html
curl localhost
显示:
docker-teninge.oldboylinux.cn
1. 清理代码包,解压目录
rm -fr /tmp/*
2. 清理apt/yum缓存
rm -fr /var/cache/*
3. 其他的清理,逐步测试
docker commit ubt_tengine_2.3.3 tengine:2.3.3-v1_not_you
[root@docker01 ~]# docker commit oldboy_tengine_bird_v1 tengine:oldboy_bird_v1
sha256:82245c5b67084cd7e05579bc9464c1c4a804a1b4cdcd5240fb5b4a3f650333b0
[root@docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tengine oldboy_bird_v1 82245c5b6708 12 seconds ago 382MB
nginx alpine_restart_v1 db5dff70a976 20 hours ago 23.5MB
nginx alpine cc44224bfe20 2 years ago 23.5MB
nginx latest 605c77e624dd 2 years ago 141MB
ubuntu 20.04 ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
1. 启动服务注意这里要加上 nginx -g 'daemon off;'
docker run -d --name "tengine-2.3.3-not_opt_v1" p 80:80 tengine:2.3.3-v1_not_you nginx -g 'daemon
off;'
2. 检查
curl 10.0.0.81:80
docker-teninge.oldboylinux.cn
说明:
nginx -g用于在命令行中指定配置文件中的选项/指令,要以分号结尾
daemon off 关闭守护进程模式,前台运行
/app/tools/tengine/logs/ 存放在容器中
关于日志的处理
方案01 日志目录挂载到宿主机的某个目录中.
方法02 把日志软连接到/dev/stdout 和/dev/stderr中,未来可以通过docker logs 查看日志
ln -s /dev/stdout /app/tools/tengine/logs/access.log
访问日志 stdout标准输出
ln -s /dev/sdterr /app/tools/tengine/logs/error.log
修改完成后记得重启容器.
错误日志 stderr错误输出
只要有访问日志生成就会输出到屏幕,就可以通过docker logs 查看logs -f 查看
日志方式 | |
---|---|
创建软连接到/dev/stderr 和/dev/stdout | 通过docker logs查看日志 |
数据卷挂载到宿主机的某个目录中 | 灵活,多种情况的各种日志推荐使用这个 |
rm -f /app/tools/tengine/logs/*
ln -s /dev/stdout /app/tools/tengine/logs/access.log
ln -s /dev/stderr /app/tools/tengine/logs/error.log
应用场景:通过1个文件Dockerfile,docker build可以自动化生成自定义 镜像
为何使用Dockerfile:
Dockerfile用于根据要求自动创建 镜像
mkdir -p /app/docker/dockerfile/01-centos-ngx
cd /app/docker/dockerfile/01-centos-ngx/
vim Dockerfile
FROM centos:7
LABEL author="oldboylidao996"
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum -y install nginx
RUN rm -rf /usr/share/nginx/html/index.html
RUN echo 'oldboyedu linux' > /usr/share/nginx/html/index.html
CMD ["nginx","-g","daemon off;"]
[root@docker01 01-centos-ngx]# docker build -t centos:nginx_v1 .
[+] Building 80.1s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 422B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 16.7s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/6] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 25.7s
=> => resolve docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.3s
=> => sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 1.20kB / 1.20kB 0.0s
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B 0.0s
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB 0.0s
=> => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB 17.7s
=> => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 7.5s
=> [2/6] RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 1.0s
=> [3/6] RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 0.4s
=> [4/6] RUN yum -y install nginx 34.9s
=> [5/6] RUN rm -rf /usr/share/nginx/html/index.html 0.3s
=> [6/6] RUN echo 'oldboyedu linux' > /usr/share/nginx/html/index.html 0.4s
=> exporting to image 0.7s
=> => exporting layers 0.7s
=> => writing image sha256:2c6e8cced25e3cd0e3de14b090160e4c7930b3ae90dd83f07a7408073f3bc05e 0.0s
=> => naming to docker.io/library/centos:nginx_v1 0.0s
[root@docker01 01-centos-ngx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos nginx_v1 2c6e8cced25e 10 minutes ago 523MB
[root@docker01 01-centos-ngx]# docker run -d --rm -p 81:80 centos:nginx_v1
FROM nginx:alpine
LABEL author="oldboylidao996"
RUN rm -rf /usr/share/nginx/html/index.html
RUN echo 'docker file oldboyedu linux' > /usr/share/nginx/html/index.html
CMD ["nginx","-g","daemon off;"]
####################################################
2. 根据Dockerfile构建镜像
docker build -t nginx:diy_img_test_v1 .
注意: .表示 Dockerfile在当前目录下面 -t 就是给自定义镜像命名.
3. 运行
docker run -d --name 'nginx-1.20.1-centos7-01' -p 81:80 nginx:1.20.2-centos7
4. 调试
Dockerfile指令 | 含义 | 应用 | 建议 |
---|---|---|---|
Dockerfile开头部分 | |||
FROM | 指定基本镜像类似于 docker pull 下载镜像 | FROM ubuntu:20.04 | 尽量少写ubuntu或ubuntu:latest,尽量指定具体的版本 |
LABEL | 用于指定容器的属性信息,作者,个人联系方式(邮件)… | LABEL maintainer=“lidao996 youjiu_linux@qq.com” | 推荐使用LABEL,不推荐使用下面的MAINTAINER |
MAINTAINER | 不再使用,推荐使用LABEL 个人信息 | ||
ENV | 用于创建Dockerfile中使用的变量 | ENV Tengine_Version空格2.3.3 | 软件版本可以创建使用变量 |
Dockerfile中间处理部分 | |||
RUN | 制作镜像过程中需要的执行命令,通常系统配置,服务配置,部署。但不能出现阻塞当前终端的命令 | RUN 系统命令即可. | 不建议使用连续多个RUN,合并连续多个RUN |
ADD | 可以把指定文件或目录拷贝到容器中(指定目录), 会解压压缩包 .相对于当前目录 | ADD restart.tar.gz空格/app/code/restart/ | 拷贝压缩包使用 |
COPY | 可以把指定文件或目录拷贝到容器中(指定目录),不支持自动解压.相对于当前目录 | COPY nginx.conf空格/etc/nginx/nginx.conf | 拷贝文件或目录 |
WORKDIR | 指定 容器 的默认工作目录 | WORKDIR /app/code/restart/ ADD restart.tar.gz空 | 一般用于配合ADD,COPY需要书写容器中路径指令.Dockerfile中使用相对路径操作容器. |
VOLUME | 挂载数据卷 | VOLUME /usr/share/nginx/html | 创建随机数据卷挂载容器的目录.未来推荐docker run的时候指定 -v即可 |
Dockerfile结尾部分书写的内容 | |||
EXPOSE | 指定镜像要对外暴露的端口 | EXPOSE 80 | 用于指定一个或多个容器的端口.未来这个端口可以被-P识别.xxxx:80 |
CMD | 用于指定容器的入口命令.入口命令可以在docker run的时候替换==运行镜像启动容器的时候,容器默认运行的命令是什么 | CMD [“命令”,“参数01”,“参数02”]CMD [“nginx”,“-g”,“daemon off;”] | 大部分都会使用CMD |
ENTRYPOINT | 用于指定容器的入口命令.无法被docker run替换, dockerrun指定的时候仅仅作为 entrypoint命令的参数而已 | ENTRYPOINT [“executable”,“param1”, “param2”] | 使用不多 |
更多说明:https://docs.docker.com/reference/dockerfile/
CMD和ENTRYPOINT区别 | 共同点 | 区别 |
---|---|---|
CMD | 运行容器的时候默认运行CMD或ENTRYPOINT后面的命令 | run的时候替换,如果指定了命令内容,cmd内容就会被替换 |
ENTRYPOINT | 运行容器的时候默认运行CMD或ENTRYPOINT后面的命令 | run的时候,如果指定了命令内容,entrypoint命令的参数而已 |
CMD ["nginx","-g","daemon off;"]
ENTRYPOINT ["nginx","-g","daemon off;"]
docker run -d test:ngx
共同点: 默认运行CMD或ENTRYPOINT后面的命令.
CMD ["nginx","-g","daemon off;"]
ENTRYPOINT ["nginx","-g","daemon off;"]
docker run -d test:ngx sleep 999
使用的是CMD形式
CMD ["nginx","-g","daemon off;"] 不会运行而运行sleep 999
使用的是ENTRYPOINT形式
ENTRYPOINT ["nginx","-g","daemon off;"] 运行 nginx -g "daemon off;" sleep 999
手动实现创建tengine镜像
1)流程说明
2)手动编译安装tengine
a)1.下载并启动ubuntu:20.04 容器叫ubt_tengine 2.3.3
b)2.配置apt源
c)3.下载软件包
d)4.编译安装3步曲
e)5.收尾,启动,测试
f)6.清理镜像(略)
g)7.生成镜像
h)8.运行容器
i)9.关于日志(了解)
3)小结
1. 目录准备
tengine.2.3.3.tar.gz 源码包
bird.tar.gz 代码包
2. 书写Dockerfile
#1. 基本信息
FROM ubuntu:20.04
LABEL author="lidao996" \
url="www.oldboyedu.com"
#2. 传输软件包
ADD tengine-2.3.3.tar.gz /tmp/
#3. 环境准备
RUN sed -ri 's#archive.ubuntu.com|security.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list \
&& apt update \
&& apt install -y libssl-dev make gcc pcre2-utils libpcre3-dev zlib1g-dev \
&& cd /tmp/tengine-2.3.3/ \
&& ./configure --prefix=/app/tools/tengine-2.3.3/ \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_mp4_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=modules/ngx_http_upstream_check_module/ \
--add-module=modules/ngx_http_upstream_session_sticky_module \
&& make -j 1 \
&& make install \
&& ln -s /app/tools/tengine-2.3.3/sbin/nginx /sbin/ \
&& ln -s /app/tools/tengine-2.3.3/ /app/tools/tengine \
&& useradd -s /sbin/nologin nginx
#4. 传输代码
ADD bird.tar.gz /app/tools/tengine/html/
#5. 清理
RUN rm -fr /tmp/* /var/cache/*
#6. 设置暴露80端口和入口指令
EXPOSE 80
CMD [ "nginx","-g","daemon off;" ]
3. 构建镜像
docker build -t "tengine:2.3.3-v1-dockerfile" .
4. 启动
docker run -d --name "tengine-2.3.3-dockerfile-v1" -p 80:80 tengine:2.3.3-v1-dockerfile
数据卷挂载
手动自定义镜像
自动化创建镜像:Dockerfile:FROM,LABEL,RUN,ADD/COPY,EXPOSE,CMD
作业: