开发环境中Docker的使用

时间:2022-08-23 06:25:09

一. Ubuntu16.04+Django+Redis+Nginx的Web项目Docker化

1.创建Django项目的image

# 创建项目image 执行 docker build -t ccnu_resource_pub -f docker/app/Dockerfile .
# 把ubuntu:xenial作为基础镜像 如果不存在则pull远程
FROM ubuntu:xenial # 把docker/common/sources.list 拷贝到镜像里的文件系统中
ADD ./docker/common/sources.list /etc/apt/sources.list # 运行linux指令
RUN apt-get -y update # PYTHON环境
WORKDIR /proj
# 把requirements.txt拷贝到/proj文件下
ADD requirements.txt /proj
RUN apt-get install -y python python-dev python3 python3-dev python3-pip
RUN apt-get install -y libmysqlclient-dev
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt
RUN pip3 install uwsgi # ES日志目录
RUN mkdir -p /var/log/ccnu_resource
RUN chmod -R 777 /var/log/ccnu_resource

2.创建Nginx的image

# 创建nginx的image  docker build -t ccnu_resource_pub_nginx -f docker/nginx/Dockerfile .

FROM ubuntu:xenial

ADD ./docker/common/sources.list /etc/apt/sources.list

RUN apt-get -y update && apt-get -y update --fix-missing && apt-get -y update

RUN apt-get install -y nginx

3.创建Redis的image

# 创建Redis的image   docker build -t ccnu_resource_pub_redis -f docker/redis/Dockerfile .

FROM ubuntu:xenial

ADD ./docker/common/sources.list /etc/apt/sources.list
RUN apt-get -y update && apt-get -y update --fix-missing && apt-get -y update RUN apt-get install -y redis-server

4.创建docker-compose.yml

# version表示定义的 docker-compose.yml 文件内容所采用的版本,目前 Docker Compose 的配置文件已经迭代至了第三版
version: "3" # docker-compose.yml 的核心部分,定义了容器的各项细节
services:
redis:
# 指定镜像
image: ccnu_resource_pub_redis:latest
# 端口映射 最好使用引号将端口映射的定义包裹起来,由于 YAML 格式对 xx:yy 这种格式的解析有特殊性,在设置小于 60 的值时,会被当成时间而不是字符串来处理
ports:
- "6379:6379"
command: redis-server --appendonly yes
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10" web:
image: ccnu_resource_pub:latest
ports:
- "8000:8000"
command: python3 manage.py runserver 0.0.0.0:8000
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
# 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器
volumes:
- .:/proj # 依赖 表示启动web的compose前需先启动 redis
depends_on:
- redis celery:
image: ccnu_resource_pub:latest
command: celery worker -A ccnu_resource -l info
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
depends_on:
- web
volumes:
- .:/proj

二. 单机ELK部署Docker化

1. 进入ELK的docker网址(可能要*) https://www.docker.elastic.co/,选择最新稳定版6.54版

开发环境中Docker的使用

使用官方提供的docker-compose.yml创建ES的docker-compose.yml

version: '2.2'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
container_name: elasticsearch
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- esnet
elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
container_name: elasticsearch2
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=elasticsearch"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata2:/usr/share/elasticsearch/data
networks:
- esnet volumes:
esdata1:
driver: local
esdata2:
driver: local networks:
esnet:

2. 然后进入配置页面https://www.elastic.co/guide/en/logstash/6.5/docker-config.html, 查看Logstash的docker配置

开发环境中Docker的使用

 logstash:
image: docker.elastic.co/logstash/logstash:6.5.4
container_name: logstash
networks:
- esnet
depends_on:
- elasticsearch
- elasticsearch2
environment:
- "LS_JAVA_OPTS=-Xms512m -Xmx512m"

3. 然后进入配置页面https://www.elastic.co/guide/en/kibana/6.5/docker.html, 查看Kibana的docker配置

开发环境中Docker的使用

 kibana:
image: docker.elastic.co/kibana/kibana:6.5.4
container_name: kibana
ports:
- "5601:5601"
networks:
- esnet
depends_on:
- elasticsearch
- elasticsearch2

4. 整合docker-compose.ym

version: '2.2'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
container_name: elasticsearch
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- esnet
elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
container_name: elasticsearch2
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=elasticsearch"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata2:/usr/share/elasticsearch/data
networks:
- esnet
logstash:
image: docker.elastic.co/logstash/logstash:6.5.4
container_name: logstash
networks:
- esnet
depends_on:
- elasticsearch
- elasticsearch2
environment:
- "LS_JAVA_OPTS=-Xms512m -Xmx512m"
kibana:
image: docker.elastic.co/kibana/kibana:6.5.4
container_name: kibana
ports:
- "5601:5601"
networks:
- esnet
depends_on:
- elasticsearch
- elasticsearch2 volumes:
esdata1:
driver: local
esdata2:
driver: local networks:
esnet:

配置好后,执行sudo docker-compose up即可成功启动ELK