最近因为日常的开发工作,导致在本地电脑上安装了过多的和开发相关的组件。在某些场景下,当把所有的功能都打开时,电脑直接卡成幻灯片,极度影响办公效率。现在刚好有时间,准备将本地环境整理整理。
本地机器上目前和开发相关的服务有:
MySQL数据库
Kafka集群
Consul服务
Apollo服务(这个不重要)
计划将这些服务打成一个镜像,按照实际需要启动或者停止相关功能。
首先,找一个基础镜像包。由于我们大部分应用还是基于jdk的,所以使用jdk的镜像作为基础镜像是个不错的选择。
我用了 adoptopenjdk:8u275-b01-jre-hotspot-focal
。由于公司现在生产环境还停留在jdk8的时代,所以也只能使用jdk8的版本作为基础版本来构建了。
(base) ➜ DOCKER pwd
/Users/jingxuan/workspace/DOCKER
(base) ➜ DOCKER cat Dockerfile
FROM adoptopenjdk:8u275-b01-jre-hotspot-focal
(base) ➜ DOCKER ls -ls
total 8
8 -rw-r--r-- 1 jingxuan staff 48 1 8 14:19 Dockerfile
(base) ➜ DOCKER docker build -t jingxuan/devtools .
[+] Building 7.7s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 90B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/adoptopenjdk:8u275-b01-jre-hotspot-focal 7.6s
=> [1/1] FROM docker.io/library/adoptopenjdk:8u275-b01-jre-hotspot-focal@sha256:06511ce39fd4b724359d7c53498116f5 0.0s
=> => resolve docker.io/library/adoptopenjdk:8u275-b01-jre-hotspot-focal@sha256:06511ce39fd4b724359d7c53498116f5 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:3c45cd9ea47af2131b6c3b1fd42e47ee24bbe4e8881258da70e8a4a53e097f6d 0.0s
=> => naming to docker.io/jingxuan/devtools
按照上面的步骤,基础镜像包就够建好了。用命令看一下本地是否已经生成对应镜像文件:
(base) ➜ DOCKER docker image ls | grep jingxuan
jingxuan/devtools latest 3c45cd9ea47a 6 weeks ago 228MB
在Dockerfile
中增加如下内容,完成mysql
服务的安装:
# MySQL Service
RUN echo "deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "mysql-server mysql-server/root_password password root" | debconf-set-selections
RUN echo "mysql-server mysql-server/root_password_again password root" | debconf-set-selections
RUN apt-get update && \
apt-get -y install mysql-server-5.7 && \
mkdir -p /var/lib/mysql && \
mkdir -p /var/run/mysqld && \
mkdir -p /var/log/mysql && \
chown -R mysql:mysql /var/lib/mysql && \
chown -R mysql:mysql /var/run/mysqld && \
chown -R mysql:mysql /var/log/mysql
RUN sed -i -e "$ a [client]\n\n[mysql]\n\n[mysqld]" /etc/mysql/my.cnf && \
sed -i -e "s/\(\[client\]\)/\1\ndefault-character-set = utf8/g" /etc/mysql/my.cnf && \
sed -i -e "s/\(\[mysql\]\)/\1\ndefault-character-set = utf8/g" /etc/mysql/my.cnf && \
sed -i -e "s/\(\[mysqld\]\)/\1\ninit_connect='SET NAMES utf8'\ncharacter-set-server = utf8\ncollation-server=utf8_unicode_ci\nbind-address = 0.0.0.0/g" /etc/mysql/my.cnf
VOLUME /var/lib/mysql
COPY ./mysql-startup.sh /root/mysql-startup.sh
RUN chmod +x /root/mysql-startup.sh
ENTRYPOINT ["/root/mysql-startup.sh"]
看起来挺长的,但其实主要就是通过apt-get
安装mysql
服务,并对服务进行初始化。
mysql-startup.sh
是一个shell脚本,主要是为了设置root
账户的密码以及方便面远程登录使用:
#! /bin/bash
set -e
MYSQL_ROOT_PWD=${MYSQL_ROOT_PWD:-"mysql1234"}
MYSQL_USER=${MYSQL_USER:-""}
MYSQL_USER_PWD=${MYSQL_USER_PWD:-""}
MYSQL_USER_DB=${MYSQL_USER_DB:-""}
echo "[i] Setting up new power user credentials."
service mysql start $ sleep 10
echo "[i] Setting root new password."
mysql --user=root --password=root -e "UPDATE mysql.user set authentication_string=password('$MYSQL_ROOT_PWD') where user='root'; FLUSH PRIVILEGES;"
echo "[i] Setting root remote password."
mysql --user=root --password=$MYSQL_ROOT_PWD -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$MYSQL_ROOT_PWD' WITH GRANT OPTION; FLUSH PRIVILEGES;"
if [ -n "$MYSQL_USER_DB" ]; then
echo "[i] Creating datebase: $MYSQL_USER_DB"
mysql --user=root --password=$MYSQL_ROOT_PWD -e "CREATE DATABASE IF NOT EXISTS \`$MYSQL_USER_DB\` CHARACTER SET utf8 COLLATE utf8_general_ci; FLUSH PRIVILEGES;"
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_USER_PWD" ]; then
echo "[i] Create new User: $MYSQL_USER with password $MYSQL_USER_PWD for new database $MYSQL_USER_DB."
mysql --user=root --password=$MYSQL_ROOT_PWD -e "GRANT ALL PRIVILEGES ON \`$MYSQL_USER_DB\`.* TO '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_USER_PWD' WITH GRANT OPTION; FLUSH PRIVILEGES;"
else
echo "[i] Don\`t need to create new User."
fi
else
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_USER_PWD" ]; then
echo "[i] Create new User: $MYSQL_USER with password $MYSQL_USER_PWD for all database."
mysql --user=root --password=$MYSQL_ROOT_PWD -e "GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_USER_PWD' WITH GRANT OPTION; FLUSH PRIVILEGES;"
else
echo "[i] Don\`t need to create new User."
fi
fi
killall mysqld
sleep 5
echo "[i] Setting end,have fun."
exec "$@"
#Kafka Service
ENV SCALA_VERSION 2.11
ENV KAFKA_VERSION 0.10.1.0
ENV KAFKA_HOME /etc/kafka_"$SCALA_VERSION"-"$KAFKA_VERSION"
# Install Kafka, Zookeeper and other needed things
RUN apt-get install -y zookeeper wget supervisor dnsutils && \
wget -q https://archive.apache.org/dist/kafka/"$KAFKA_VERSION"/kafka_"$SCALA_VERSION"-"$KAFKA_VERSION".tgz -O /tmp/kafka_"$SCALA_VERSION"-"$KAFKA_VERSION".tgz && \
tar xfz /tmp/kafka_"$SCALA_VERSION"-"$KAFKA_VERSION".tgz -C /etc && \
mv /etc/kafka_"$SCALA_VERSION"-"$KAFKA_VERSION"/ /etc/kafka/ && \
rm /tmp/kafka_"$SCALA_VERSION"-"$KAFKA_VERSION".tgz
RUN sed -i -e "s/#listeners=PLAINTEXT:\/\/:9092/listeners=PLAINTEXT:\/\/0.0.0.0:9092/g" "$KAFKA_HOME"/config/server.properties
EXPOSE 2181 9092
主要是下载kafka,同时修改一下配置文件,使用本地localhost:9092
作为服务端口。
RUN wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip -O /tmp/consul.zip && \
unzip /tmp/consul.zip -d /usr/local/bin/ && \
rm /tmp/consul.zip
使用上面命令将consul下载到镜像中,并存放在可执行的位置就可以了。
EXPOSE 3306 2181 9092 8500
稍微整理下,最终的Dockerfile
文件如下所示:
FROM adoptopenjdk:8u275-b01-jre-hotspot-focal
RUN echo "mysql-server mysql-server/root_password password mysql1234" | debconf-set-selections
RUN echo "mysql-server mysql-server/root_password_again password mysql1234" | debconf-set-selections
RUN echo "deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse" >> /etc/apt/sources.list
RUN apt-get update && \
apt-get install -y wget unzip && \
# 安装mysql
apt-get -y install mysql-server-5.7 && \
# 安装zookeeper
apt-get install -y zookeeper
RUN mkdir -p /var/lib/mysql && \
mkdir -p /var/run/mysqld && \
mkdir -p /var/log/mysql && \
chown -R mysql:mysql /var/lib/mysql && \
chown -R mysql:mysql /var/run/mysqld && \
chown -R mysql:mysql /var/log/mysql
RUN sed -i -e "$ a [client]\n\n[mysql]\n\n[mysqld]" /etc/mysql/my.cnf && \
sed -i -e "s/\(\[client\]\)/\1\ndefault-character-set = utf8/g" /etc/mysql/my.cnf && \
sed -i -e "s/\(\[mysql\]\)/\1\ndefault-character-set = utf8/g" /etc/mysql/my.cnf && \
sed -i -e "s/\(\[mysqld\]\)/\1\ninit_connect='SET NAMES utf8'\ncharacter-set-server = utf8\ncollation-server=utf8_unicode_ci\nbind-address = 0.0.0.0/g" /etc/mysql/my.cnf
RUN wget https://archive.apache.org/dist/kafka/0.10.1.0/kafka_2.11-0.10.1.0.tgz -O /tmp/kafka.tgz && \
tar xfz /tmp/kafka.tgz -C /etc && \
mv /etc/kafka_2.11-0.10.1.0/ /etc/kafka/ && \
rm /tmp/kafka.tgz
RUN sed -i -e "s/#listeners=PLAINTEXT:\/\/:9092/listeners=PLAINTEXT:\/\/0.0.0.0bde:9092/g" /etc/kafka/config/server.properties
RUN wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip -O /tmp/consul.zip && \
unzip /tmp/consul.zip -d /usr/local/bin/ && \
rm /tmp/consul.zip
VOLUME /var/lib/mysql
EXPOSE 3306 2181 9092 8500
使用如下命令在本地启动:
~ docker run --name devtools -d -p 3306:3306 -p 2181:2181 -p 9092:9092 -p 8500:8500 -v /Users/jingxuan/docker/mysql/:/var/lib/mysql/ jingxuan/devtools
为了方便使用,可以将各个服务的启动和停止命令使用别名保存,主要的命令如下:
docker
docker start devtools
docker stop devtools
mysql
docker exec devtools service mysql start
docker exec devtools service mysql stop
zookeeper
docker exec devtools bash /usr/share/zookeeper/bin/zkServer.sh start
docker exec devtools bash /usr/share/zookeeper/bin/zkServer.sh stop
kafka
docker exec devtools bash /etc/kafka/bin/kafka-server-start.sh /etc/kafka/config/server.properties >/dev/null 2>&1 &
docker exec devtools bash /etc/kafka/bin/kafka-server-stop.sh &
consul
docker exec devtools consul agent -dev -node=machine > /dev/null 2>&1 &
docker exec devtools killall consul
配置如下的别名:
# Devtools
alias devstart="docker start devtools"
alias devstop="docker stop devtools"
alias devdbstart="docker start devtools && docker exec devtools service mysql start > /dev/null 2>&1"
alias devdbstop="docker exec devtools service mysql stop"
alias devzkstart="docker start devtools && docker exec devtools bash /usr/share/zookeeper/bin/zkServer.sh start"
alias devzkstop="docker exec devtools bash /usr/share/zookeeper/bin/zkServer.sh stop"
alias devkafkastart="docker start devtools && docker exec devtools bash /usr/share/zookeeper/bin/zkServer.sh start && docker exec devtools bash /etc/kafka/bin/kafka-server-start.sh /etc/kafka/config/server.properties > /dev/null 2>&1 &"
alias devkafkastop="docker exec devtools bash /etc/kafka/bin/kafka-server-stop.sh && docker exec devtools bash /usr/share/zookeeper/bin/zkServer.sh stop"
alias devconsulstart="docker start devtools && docker exec devtools consul agent -dev -node=machine > /dev/null 2>&1 &"
alias devconsulstop="docker exec devtools killall consul"
这样以后需要启动何种服务,只需要在terminal中输入对应别名就可以了。