dockerfile的学习笔记

55 阅读2分钟

一、简介与背景




二、安装docker与dockerfile




三、dockerfile



# 基于官方的CentOS 7镜像

FROM centos:7

# 配置阿里云镜像源

RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

  
  


# 安装必要的工具

RUN yum install -y \

mysql \

mysql-devel \

mysql-server \

net-tools \

&& yum clean all

  


# 将自定义的my.cnf配置文件复制到容器中

COPY my.cnf /etc/my.cnf

  
  


# 将收集MySQL连接数的脚本复制到容器中

COPY collect_connections.sh /usr/local/bin/collect_connections.sh

RUN chmod +x /usr/local/bin/collect_connections.sh

  


# 定义环境变量

# ENV MYSQL_HOST=localhost

ENV MYSQL_PORT=3306

ENV MYSQL_USER=root

ENV MYSQL_PASSWORD=1234567

  


# 暴露端口

EXPOSE 3306

  


# 启动脚本,用于启动定时任务

CMD ["/usr/local/bin/collect_connections.sh"]




四、相关配套文件

  1. my.cnf配置文件
[mysqld]
#
# Settings configured by the user
#

# Sets how the table names are stored and compared. Default: 0
lower_case_table_names = 0
# Sets whether queries should be logged
general_log = 0
# general_log_file = /var/lib/mysql/data/mysql-query.log
general_log_file = /var/log/mysql/mysql.log

slow_query_log = ON
slow_query_log_file = /var/log/mysql/mysql-query.log
long_query_time = 1
log_output = FILE

# The maximum permitted number of simultaneous client connections. Default: 151
max_connections = 1500
#sql_mode='NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES'

# In case the native AIO is broken. Default: 1
# See http://help.directadmin.com/item.php?id=529
innodb_use_native_aio = 1

[myisamchk]
# The minimum/maximum lengths of the word to be included in a FULLTEXT index. Default: 4/20
#
# To ensure that myisamchk and the server use the same values for full-text
# parameters, we placed them in both sections.
ft_min_word_len = 4
ft_max_word_len = 20

[mysqldump] #注意账号密码需要写在此标签下
user=root
password=1234567


  1. collect_connections.sh
#!/bin/bash

# 每30秒收集一次MySQL连接数
while true; do
# 获取当前时间
current_time=$(date +"%Y-%m-%d %H:%M:%S")
# 获取MySQL连接数
connections=$(mysql -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'Threads_connected';" | grep Threads_connected | awk '{print $2}')
# 输出到标准输出和日志文件
echo "[$current_time] Number of active DB connections is $connections." | tee -a /data/mysql/log/mysql_connections.log
# 等待30秒
sleep 30
done




五、构建镜像

# 构建镜像
docker build -t mysql-connection-logger .

'''

说明

-t, --tag: 为生成的镜像指定标签(通常是 name:tag 格式)。
-f, --file: 指定 Dockerfile 的路径(默认是当前目录下的 Dockerfile)。
--build-arg: 设置构建时的变量。
--no-cache: 构建时不使用缓存。
--rm: 构建完成后删除中间容器(默认是启用的)。
--pull: 始终尝试从远程仓库拉取最新版本的基础镜像。
-q, --quiet: 只输出构建的镜像 ID。
. ,表示当前目录

'''




六、运行容器与观察


# 运行容器
docker run -d --name mysql-logger \
-e MYSQL_HOST=localhost \
-e MYSQL_PORT=3306 \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=1234567 \
mysql-connection-logger

#
docker run -d --name mysql-logger \
-e MYSQL_PORT=3306 \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=1234567 \
-v /data/mysql/log:/var/mysql/log \
-v /data/mysql/data:/var/lib/mysql \
mysql-connection-logger

  


#
docker run -d --name mysql-logger mysql-connection-logger




七、补充

# ###
# 查看日志
# ###
#
docker logs -f mysql-connection-logger



#
docker exec -it mysql-connection-logger tail -f /data/mysql/log