使用Docker快速部署各类服务

时间:2024-07-24 19:08:14

使用Docker快速部署各类服务

一键安装Docker

#Centos环境
wget -O- https://gitee.com/iubest/dinstall/raw/master/install.sh |sh

快速部署Mysql

docker run -d  -p 3306:3306 --restart=always --name=mysql5.7 \
-e MYSQL_ROOT_PASSWORD=1qaz@WSX \
-e MYSQL_DATABASE=testdb \
-e TZ=Asia/Shanghai mysql:5.7 \
--character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci

快速部署DB2

#部署
docker run -itd --name mydb2 --privileged=true -p 55000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=1qaz@WSX -e DBNAME=testdb -v /root/db2:/database ibmcom/db2
#访问
docker exec -ti mydb2 bash -c "su - db2inst1"
db2 connect to testdb
#连接信息
db2inst1 testdb 55000 1qaz@WSX

快速部署ES

#配置环境
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
grep "vm.max_map_count" /etc/sysctl.conf
sysctl -p
docker run -d -v /home/es/data:/usr/share/elasticsearch/data -v /home/es/logs:/usr/share/elasticsearch/logs -p 9200:9200 --name='es' elasticsearch:6.4.2
#第一次无法启动,需要授权
chmod -R 777 /home/es
docker start es

快速部署Jenkins

使用官方镜像

docker run \
-u root \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v /opt/jenkins-data:/var/jenkins_home \
--name myjenkins\
jenkinsci/blueocean

使用自定义镜像

docker run \
-u root \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v /opt/jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
--name myjenkins\
registry.cn-shanghai.aliyuncs.com/tzwind/jenkins/goodjenkins
#cat /opt/jenkins-data/secrets/initialAdminPassword

快速部署Gitlab中文版

#创建数据目录,需要挂载到docker
mkdir -p /data/gitlab/etc
mkdir -p /data/gitlab/log
mkdir -p /data/gitlab/data #下载和运行镜像
docker run \
--detach \
--publish 8443:443 \
--publish 8080:80 \
--name gitlab \
--restart unless-stopped \
--volume /data/gitlab/etc:/etc/gitlab \
--volume /data/gitlab/log:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
beginor/gitlab-ce:10.7.5-ce.0 #访问(用户名root)
http://ip:8080

快速部署Artifactory

#创建数据目录:
mkdir -p /home/wind/artifactory6_data #给目录加权限:
chmod -R 777 /home/wind/artifactory6_data #使用官方镜像启动:
docker run --name artifactory -d --restart=always \
-v /home/wind/artifactory6_data:/var/opt/jfrog/artifactory \
-p 8081:8081 docker.bintray.io/jfrog/artifactory-oss:6.9.0 #使用个人镜像启动(推荐):
docker run --name artifactory -d --restart=always \
-v /home/wind/artifactory6_data:/var/opt/jfrog/artifactory \
-p 8081:8081 limitless-docker.pkg.coding.net/easyblog/wind/artifactory-oss:6.9.0

快速部署Zabbix

启动一个空的Mysql服务器实例

docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-d mysql:5.7 \
--character-set-server=utf8 --collation-server=utf8_bin

启动Zabbix server实例,并关联

docker run --name zabbix-server-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
--link mysql-server:mysql \
-p 10051:10051 \
-d zabbix/zabbix-server-mysql:latest

启动Zabbix web,并关联

docker run --name zabbix-web-nginx-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server \
-p 8080:80 \
-d zabbix/zabbix-web-nginx-mysql:latest
访问你的IP:8080
帐号密码:Admin/zabbix

快速部署Flink

#启动临时容器
docker run -itd --rm --name=temp -h flink-jm flink:1.11-scala_2.11-java8 jobmanager
#复制配置文件到本地
docker cp temp:/opt/flink/conf /tmp/flink_docker
#删除临时容器
docker stop temp
#创建虚拟网桥
docker network create flinknet
#启动flink job
docker run -itd --name flink-jm -h flink-jm -p 8090:8081 -v /tmp/flink_docker/conf:/opt/flink/conf --network flinknet flink:1.11-scala_2.11-java8 jobmanager
#启动flink task ro参数表示只读,必须有
docker run -itd --name flink-tm -h flink-tm -v /tmp/flink_docker/conf:/opt/flink/conf:ro --network flinknet flink:1.11-scala_2.11-java8 taskmanager

Docker-compose

安装Docker-compose

#方法1:
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
#sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
#查看版本
docker-compose --version
#docker-compose version 1.27.4, build 40524192 #方法2:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum -y install docker-compose
#查看版本
docker-compose --version
#docker-compose version 1.18.0, build 8dd22a9

快速部署个人博客

cat <<EOF> deploy.yaml
version: '3'
services:
mysql:
container_name: mymysql
image: mysql:5.7
command: [ #使用 command 可以覆盖容器启动后默认执行的命令,必须在environment前面。
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
]
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_ROOT_HOST: '%'
MYSQL_DATABASE: halodb
TZ: Asia/Shanghai
ports:
- "3306:3306"
volumes:
- ./mysql_data:/var/lib/mysql
restart: always app:
container_name: myapp
image: limitless-docker.pkg.coding.net/easyblog/wind/haloblog:ok
restart: always
ports:
- "80:8090"
depends_on:
- mysql
EOF
#启动
docker-compose -f deploy.yaml up -d

快速部署Gitlib中文版

cat > deploy-gitlab.yaml << 'EOF'
version: '3'
services:
web:
image: 'twang2218/gitlab-ce-zh:10.5'
restart: always
hostname: '192.168.75.145'
environment:
TZ: 'Asia/Shanghai'
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.75.145:8080'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
unicorn['port'] = 8888
nginx['listen_port'] = 8080
ports:
- '8080:8080'
- '8443:443'
- '2222:22'
volumes:
- /usr/local/docker/gitlab/config:/etc/gitlab
- /usr/local/docker/gitlab/data:/var/opt/gitlab
- /usr/local/docker/gitlab/logs:/var/log/gitlab
EOF
docker-compose -f deploy-gitlab.yaml up -d

小技巧

Docker启动时如何初始化

#初始化脚本
#!/bin/bash
set -e
set -u
if [[ -z "$(ls -A /srv/docs)" ]]; then
echo 'Initializing new docs'
cp -R /srv-init/* /srv/docs
cp -R /srv-init/.vuepress /srv/docs/
fi
cd /srv/docs
yarn docs:$@
cat >Dockerfile<< EOF
FROM node
LABEL maintainer="Thomas de Saint"
COPY package.json yarn.lock /srv/
WORKDIR /srv
RUN yarn
COPY docs /srv-init/
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
CMD ["dev"]
EOF
docker build -t tstex/vuepress .

两个容器加入同一网络

#查看docker中的网络
docker network ls
#查看bridge网络的详细配置
docker network inspect bridge
#创建用户自定义bridge:
docker network create my-net # 创建了一个名为"my-net"的网络
#将Web服务容器和mysql服务容器加入到"my-net"中
docker network connect my-net test_demo # 将Web服务加入my-net网络中
docker network connect my-net mysqld5.7 # 将mysql服务加入my-net网络中
#查看my-net的网络配置
docker network inspect my-net
#断开旧网络
docker network disconnect bridge test_demo
docker network disconnect bridge mysqld5.7

Docker中使用systemctl

#需要启动参数--privileged  init
docker run -itd --name mycentos --privileged centos init

命令行查看docker tag

yum install jq
#apt install jq #jq源码安装
#cd /usr/local/src
#wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-1.5.tar.gz
#tar zxvf jq-1.5.tar.gz
#cd jq-1.5
#./configure
#make && make install
#测试
#echo '{"yjt":"boy","age":20}' |jq cd /bin
vi docker-tag
#!/bin/sh
repo_url=https://registry.hub.docker.com/v1/repositories
image_name=$1
#curl -s ${repo_url}/${image_name}/tags | json_reformat | grep name | awk '{print $2}' | sed -e 's/"//g'
curl -s ${repo_url}/${image_name}/tags | jq . | grep name | awk '{print $2}' | sed -e 's/"//g' #添加执行权限
chmod +x docker-tag #查看mysql所有tag
docker-tag mysql