dockerfile的面试题

说明:上周四面试的时候,一公司发过来的面试题,需要提前做一下,然后对这两个面试题,进行一个整理和汇总。

1.编写Dockerfile

  • 编写 Dockerfile ,构建⼀个Docker镜像(不能包含 MySQL 服务端程序),完成以下需求
    • 镜像中包含⼀个 shell 脚本,容器启动后每隔 30s 收集 MySQL 数据库当前的连接数,将数据同时输出⾄ /data/log ⽂件(⽇志可以持久化保存)及标准输出中
    • 数据库IP、端⼝、⽤户及密码可以在容器启动时通过 -e 指定环境变量来修改
    • 要求容器启动后可以使⽤ docker logs container_name 和 docker exec -i -container_name tail -f /data/log 两种⽅式查看⽇志信息
    • 每次输出的信息格式如下 [2019-01-01 00:00:00] Number of active DB connections is 10.

1.1 编写脚本

#!/bin/bash
# Author:zfl
while true
do
    MYSQL_USER=$MYSQL_USER
    MYSQL_PASSWD=$MYSQL_PASSWD
    MYSQL_IP=$MYSQL_IP
    MYSQL_PORT=$MYSQL_PORT
    TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"`
    connect_counts=`mysql -u${
    MYSQL_USER} -p${
    MYSQL_PASSWD} -h${
    MYSQL_IP} -P${
    MYSQL_PORT} -e  'show status'|grep Threads_connected|awk '{print $2}'`
    echo "[$TIMESTAMP] Number of active DB connections is $connect_counts." >> /data/log
    echo "[$TIMESTAMP] Number of active DB connections is $connect_counts." >> /dev/stdout
    sleep 30
done

1.2 准备需要的mysql安装rpm包

# 因为没有service所以只需要安装客户端
root@k8s-node02(192.168.1.12)/data/mysql_connect/mysql-server-8.0.25>ll
total 52M
 mysql-community-client-8.0.25-1.el7.x86_64.rpm
 mysql-community-libs-compat-8.0.25-1.el7.x86_64.rpm
 mysql-community-libs-8.0.25-1.el7.x86_64.rpm
 mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
 mysql-community-common-8.0.25-1.el7.x86_64.rpm
root@k8s-node02(192.168.1.12)/data/mysql_connect/mysql-server-8.0.25>

1.3 编写dockerfile

root@k8s-node02(192.168.1.12)/data/mysql_connect>cat Dockerfile 
FROM centos:7.9
LABEL NAME="ZFL"
ADD  mysql-server-8.0.25.tar.gz /root
COPY mysql_connect.sh /root
RUN  yum install /root/mysql-server-8.0.25/*.rpm -y
RUN  chmod +x /root/mysql_connect.sh
ENV TimeZone=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
ENV MYSQL_USER "root"
ENV MYSQL_PASSWD "" 
ENV MYSQL_IP ""
ENV MYSQL_PORT "3306"
CMD ["/root/mysql_connect.sh"]

root@k8s-node02(192.168.1.12)/data/mysql_connect>

1.4构造镜像

root@k8s-node02(192.168.1.12)/data/mysql_connect>docker build -t mysql_connect:v3 ./
Sending build context to Docker daemon  54.02MB


root@k8s-node02(192.168.1.12)/data/mysql_connect>di
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             SIZE
mysql_connect                                                     v3                  2c08ff6e43b0        6 seconds ago       548MB

1.5 运行mysql_connect服务

测试

​ 在192.168.1.11 安装了mysql的服务

 docker run -itd  --name mysql_conn \
 -e MYSQL_USER="root"  \
 -e MYSQL_PASSWD="AAAaaa111." \
 -e MYSQL_IP="192.168.1.11" \
 -e MYSQL_PORT="3306"    \
 -v /data/log:/data/log \
 mysql_connect:v3
root@k8s-node02(192.168.1.12)/data/mysql_connect>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
13202a213321        mysql_connect:v3    "/root/mysql_connect…"   2 seconds ago       Up 1 second                             mysql_conn
root@k8s-node02(192.168.1.12)/data/mysql_connect>docker logs -f mysql_conn
mysql: [Warning] Using a password on the command line interface can be insecure.
[2023-11-28 15:44:26] Number of active DB connections is 1.

root@k8s-node02(192.168.1.12)/data/mysql_connect> docker exec -i -t mysql_conn tail -f /data/log 
[2023-11-28 15:44:26] Number of active DB connections is 1.
[2023-11-28 15:44:56] Number of active DB connections is 1.
[2023-11-28 15:45:26] Number of active DB connections is 1.
[2023-11-28 15:45:56] Number of active DB connections is 1.

###持久化存储
root@k8s-node02(192.168.1.12)/data>tail -f 10 log
[2023-11-28 15:44:26] Number of active DB connections is 1.
[2023-11-28 15:44:56] Number of active DB connections is 1.
[2023-11-28 15:45:26] Number of active DB connections is 1.
[2023-11-28 15:45:56] Number of active DB connections is 1.
[2023-11-28 15:46:26] Number of active DB connections is 1.
[2023-11-28 15:46:56] Number of active DB connections is 1.
[2023-11-28 15:47:26] Number of active DB connections is 1.
[2023-11-28 15:47:56] Number of active DB connections is 1.
[2023-11-28 15:48:26] Number of active DB connections is 1.

2.编写Docker-compose

编写 docker-compose.yml 脚本,使⽤题⽬1中构建的镜像及 MySQL 镜像启动服务

2.1 编写docker-compose

version: "3"
services:
  mysql:
    container_name: mysql
    image: mysql:latest
    restart: always 
    ports:
      - 3306:3306          
    environment:
      MYSQL_ROOT_PASSWORD: AAAaaa111.
    volumes:
      - /root/docker-compose/mysql/conf/:/etc/mysql/
      - /root/docker-compose/mysql/mysql_data/:/var/lib/mysql/
    networks:
      - test_net
###############
  mysql_conn:
    container_name: mysql_conn1
    image: mysql_connect:v3
    restart: always
    environment:
      MYSQL_USER: "root"
      MYSQL_PASSWD: "AAAaaa111."
      MYSQL_IP: "192.168.1.11"
      MYSQL_PORT: "3306"
    volumes:
      - /data/log:/data/log
    networks:
      - test_net
#########################################
networks:
  test_net:
    name: test_net
    driver: bridge
    ipam:
      config:
      - subnet: "172.200.0.0/16"

# 执行docker-copmose
root@k8s-node02(192.168.1.12)~/mysql>up -d
[+] Running 2/2
 ✔ Container mysql_conn1  Started                                                                                                                                       0.8s 
 ✔ Container mysql        Started                                                                                                                                       0.7s 
root@k8s-node02(192.168.1.12)~/mysql>dps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
mysql               mysql:5.6.38        "docker-entrypoint.s…"   mysql               7 seconds ago       Up 6 seconds        0.0.0.0:3306->3306/tcp
mysql_conn1         mysql_connect:v3    "/root/mysql_connect…"   mysql_conn          7 seconds ago       Up 6 seconds        
root@k8s-node02(192.168.1.12)~/mysql>

2.2 测试

root@k8s-node02(192.168.1.12)/data>cat log 
[2023-11-28 16:17:31] Number of active DB connections is 1.
[2023-11-28 16:18:01] Number of active DB connections is 1.
[2023-11-28 16:18:31] Number of active DB connections is 1.
root@k8s-node02(192.168.1.12)/data>

root@k8s-node02(192.168.1.12)/data>docker logs -f   mysql_conn1
mysql: [Warning] Using a password on the command line interface can be insecure.
[2023-11-28 16:17:31] Number of active DB connections is 1.
root@k8s-node02(192.168.1.12)/data> docker exec -i -t mysql_conn1 tail -f /data/log 
[2023-11-28 16:17:31] Number of active DB connections is 1.
[2023-11-28 16:18:01] Number of active DB connections is 1.
[2023-11-28 16:18:31] Number of active DB connections is 1.
[2023-11-28 16:19:01] Number of active DB connections is 1.
[2023-11-28 16:19:31] Number of active DB connections is 1.
[2023-11-28 16:20:01] Number of active DB connections is 1.
[2023-11-28 16:20:31] Number of active DB connections is 1.
[2023-11-28 16:21:02] Number of active DB connections is 1.

相关推荐

  1. dockerfile面试

    2023-12-05 19:40:08       35 阅读
  2. Mybatis面试

    2023-12-05 19:40:08       7 阅读
  3. CSS3面试

    2023-12-05 19:40:08       37 阅读
  4. 数据库常见面试

    2023-12-05 19:40:08       28 阅读
  5. MySQL[常见面试]

    2023-12-05 19:40:08       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 19:40:08       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 19:40:08       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 19:40:08       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 19:40:08       18 阅读

热门阅读

  1. 备忘录模式-C++实现

    2023-12-05 19:40:08       37 阅读
  2. Oracle行转列,列转行使用实例

    2023-12-05 19:40:08       30 阅读
  3. docker 切换镜像源

    2023-12-05 19:40:08       30 阅读
  4. 前端实现token无感刷新的原因和步骤

    2023-12-05 19:40:08       34 阅读
  5. LeetCode刷题笔记第71题:简化路径

    2023-12-05 19:40:08       29 阅读
  6. sql 条件统计,count+if+sum

    2023-12-05 19:40:08       33 阅读
  7. 解析sql的数据流向关系获取

    2023-12-05 19:40:08       22 阅读
  8. Diary16-Word标题与目录设计

    2023-12-05 19:40:08       36 阅读