docker 安装镜像及使用命令

目录

  • [1. Mysql](#1. Mysql)
  • [2. Redis](#2. Redis)
  • [3. Nginx](#3. Nginx)
  • [4. Elasticsearch](#4. Elasticsearch)

docker pull 容器名:版本号 拉取容器, 不指定版本号默认最新的

run 运行

-d 后台运行

-p 3306:3306 -p是port 对外端口:对内端口

--name xyy_mysql 容器名称

-e MYSQL_ROOT_PASSWORD=123456 环境变量

-v 系统地址:docker地址 docker 地址映射系统地址

docker exec -it 容器名称 bash 可以进入该容器,进行操作

最后指定启动那个镜像:版本

1. Mysql

shell 复制代码
# 拉去mysql
docker pull mysql:version
# 启动mysql
docker run -p 3306:3306 --name xyy_mysql -e MYSQL_ROOT_PASSWORD=123456 -v D:/docker/mysql/data:/var/lib/mysql -d mysql:版本 --character-set-server=utf8mb4

后续不需要重复这么启动, 只需要以下方式即可启动, 其他同理

shell 复制代码
docker start xyy_mysql

2. Redis

shell 复制代码
docker pull redis:version
  1. 创建网络 里面有个IPAM.Config.Gateway 的值记下来, 是网络ip

    shell 复制代码
    # 删除网络信息
    docker network rm redis_network
    # 重新创建子网指定subnet, 172.21这个很重要
    docker network create --subnet 172.21.0.0/16 redis_network
    # 查看网络信息 `IPAM.Config.Subnet`的值
    docker network inspect redis_network
  2. 创建本地磁盘和容器映射

    shell 复制代码
    vim conf\node-1\redis.conf
    vim conf\node-2\redis.conf
    vim conf\node-3\redis.conf
    
    `vim redis.conf`
    # 分别写入以下配置
    port 6379 #端口
    bind 0.0.0.0 #开其他设备访问权限
    cluster-enabled yes #集群支持
    cluster-config-file nodes.conf # 节点配置文件
    cluster-node-timeout 5000 # 节点超时
    cluster-announce-ip 172.21.0.11 ##node2 node3 分别+1
    cluster-announce-port 6379
    cluster-announce-bus-port 16379
    appendonly yes #持久化
    requirepass 123456 # 密码
  3. 启动redis

    shell 复制代码
    docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v D:\docker\redis\data\node-1:/data -v D:\docker\redis\conf\node-1\redis.conf:/etc/redis/redis.conf --net redis_network --ip 172.21.0.11 -d redis redis-server /etc/redis/redis.conf
    docker run -p 6372:6379 -p 16372:16379 --name redis-2 -v D:\docker\redis\data\node-2:/data -v D:\docker\redis\conf\node-2\redis.conf:/etc/redis/redis.conf --net redis_network --ip 172.21.0.12 -d redis redis-server /etc/redis/redis.conf
    docker run -p 6373:6379 -p 16373:16379 --name redis-3 -v D:\docker\redis\data\node-3:/data -v D:\docker\redis\conf\node-3\redis.conf:/etc/redis/redis.conf --net redis_network --ip 172.21.0.13 -d redis redis-server /etc/redis/redis.conf
  4. 集群配置
    --cluster-replicas 1 表示每台主节点至少需要一个从节点

    shell 复制代码
    	# 进入redis-1 控制台
    	docker exec -it redis-1 bash
    	# 集群配置
    	redis-cli --cluster [-replicas 1] create 172.21.0.11:6379 172.21.0.12:6379 172.21.0.13:6379
  5. 检查集群信息

    shell 复制代码
    # 登录
    redis-cli -c 
    # 认证密码
    auth "123456"
    # 集群信息
    cluster info
    # 节点信息
    cluster nodes

3. Nginx

  1. 拉取nginx

    shell 复制代码
    docker pull nginx:version
  2. 启动 nginx 并查看容器id

    shell 复制代码
    docker run -p 9998:80 --name nginx -d nginx:版本
    docker ps # 查看容器id
  3. 将nginx中的conf文件复制出来

    docker下nginx的目录就是这里

    shell 复制代码
    	docker cp 容器ID:/etc/nginx/nginx.conf /server/nginx/conf
  4. 创建几个文件夹

    shell 复制代码
    mkdir -p /server/nginx/logs #日志
    mkdir -p /server/nginx/conf #配置
  5. 停止之前的镜像并删除

    shell 复制代码
    docker stop nginx
    docker rm nginx
  6. 重新启动nginx镜像配置

    shell 复制代码
    docker run -d -p 9999:80 --name nginx -v /server/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /server/nginx/logs:/var/log/nginx --privileged=true nginx:1.17.8

4. Elasticsearch

  1. 拉取es

    shell 复制代码
    docker pull docker.elastic.co/elasticsearch/elasticsearch:8.13.3
  2. 创建网络

    shell 复制代码
    docker network create es_network
  3. 创建映射文件夹

    shell 复制代码
    mkdir -p D:/docker/es/conf
    mkdir -p D:/docker/es/data
    mkdir -p D:/docker/es/plugins
    echo "http.host : 0.0.0.0" >> D:/docker/es/conf/elasticsearch.yml
  4. 启动
    discovery.type=single-node 单节点启动

    shell 复制代码
    docker run --name es -p 9200:9200 -p 9300:9300 \
    -e "discovery.type=single-node"\
    -e ES_JAVA_OPTS="-Xms64m -Xmx128m"
    -v D:/docker/es/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml\
    -v D:/docker/es/data:/usr/share/elasticsearch/data\
    -v D:/docker/es/plugins:/usr/share/elasticsearch/plugins\
    -d elasticsearch:8.13.3
    shell 复制代码
    docker run --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx128m" -v D:/docker/es/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v D:/docker/es/data:/usr/share/elasticsearch/data -v D:/docker/es/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:8.13.3
  5. 下载ik分词器

    下载地址: https://github.com/infinilabs/analysis-ik/releases/tag/v8.13.3

    将对应下载好的zip安装包解压到 D:/docker/es/plugins 因为之前做了映射. (ps: 不要将压缩包放到目录下, 这个目录只放插件)

  6. 将ik分词器作为默认分词器

    ES的默认分词是standard, 会在分割中文时将每个汉字都拆分开,

    IK分词器有两种设置, 一种是 ik_smart 最少分片, 会将 "清华大学" 分为 "清华大学" , 另一种是 ik_max_word 最细分片 会将"清华大学" 分为 "清华","大学"

    json 复制代码
    PUT /school_index
    {
    	"setting":{
    		"index":{
    			"analysis.analyzer.default.type":"ik_max_word"
    		}	
    	}
    }

官网指导

官方指导链接: https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

集群模式下其中两个文件进行了修改:

  1. .env 文件

    c 复制代码
    # es 密码, 6位数起
    ELASTIC_PASSWORD=123456
    
    # kabana 密码, 6位数起
    KIBANA_PASSWORD=123456
    
    # es安装版本
    STACK_VERSION=8.13.3
    
    # 集群名称
    CLUSTER_NAME=docker-es-cluster
    
    # 简单模式或者试用模式(30天)
    LICENSE=basic
    #LICENSE=trial
    
    # 映射ip, 如果指定端口则只有指定机器可以访问
    ES_PORT=9200
    #ES_PORT=127.0.0.1:9200
    
    # kabana端口
    KIBANA_PORT=5601
    #KIBANA_PORT=80
    
    # 内存大小(in bytes)
    MEM_LIMIT=1073741824
    
    # 名称前缀
    #COMPOSE_PROJECT_NAME=es
  2. docker-compose.yml 文件

    yml 复制代码
    version: "2.2"
    
    services:
      setup:
        image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
        volumes:
          - D:/docker/es/config/certs:/usr/share/elasticsearch/config/certs
        user: "0"
        command: >
          bash -c '
            if [ x${ELASTIC_PASSWORD} == x ]; then
              echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
              exit 1;
            elif [ x${KIBANA_PASSWORD} == x ]; then
              echo "Set the KIBANA_PASSWORD environment variable in the .env file";
              exit 1;
            fi;
            if [ ! -f config/certs/ca.zip ]; then
              echo "Creating CA";
              bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
              unzip config/certs/ca.zip -d config/certs;
            fi;
            if [ ! -f config/certs/certs.zip ]; then
              echo "Creating certs";
              echo -ne \
              "instances:\n"\
              "  - name: es01\n"\
              "    dns:\n"\
              "      - es01\n"\
              "      - localhost\n"\
              "    ip:\n"\
              "      - 127.0.0.1\n"\
              "  - name: es02\n"\
              "    dns:\n"\
              "      - es02\n"\
              "      - localhost\n"\
              "    ip:\n"\
              "      - 127.0.0.1\n"\
              "  - name: es03\n"\
              "    dns:\n"\
              "      - es03\n"\
              "      - localhost\n"\
              "    ip:\n"\
              "      - 127.0.0.1\n"\
              > config/certs/instances.yml;
              bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key;
              unzip config/certs/certs.zip -d config/certs;
            fi;
            echo "Setting file permissions"
            chown -R root:root config/certs;
            find . -type d -exec chmod 750 \{\} \;;
            find . -type f -exec chmod 640 \{\} \;;
            echo "Waiting for Elasticsearch availability";
            until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
            echo "Setting kibana_system password";
            until curl -s -X POST --cacert config/certs/ca/ca.crt -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" https://es01:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
            echo "All done!";
          '
        healthcheck:
          test: ["CMD-SHELL", "[ -f config/certs/es01/es01.crt ]"]
          interval: 1s
          timeout: 5s
          retries: 120
    
      es01:
        depends_on:
          setup:
            condition: service_healthy
        image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
        volumes:
          - D:/docker/es/config/certs:/usr/share/elasticsearch/config/certs
          - D:/docker/es/data/note01:/usr/share/elasticsearch/data
        ports:
          - ${ES_PORT}:9200
        environment:
          - node.name=es01
          - cluster.name=${CLUSTER_NAME}
          - cluster.initial_master_nodes=es01,es02,es03
          - discovery.seed_hosts=es02,es03
          - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
          - bootstrap.memory_lock=true
          - xpack.security.enabled=true
          - xpack.security.http.ssl.enabled=true
          - xpack.security.http.ssl.key=certs/es01/es01.key
          - xpack.security.http.ssl.certificate=certs/es01/es01.crt
          - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
          - xpack.security.transport.ssl.enabled=true
          - xpack.security.transport.ssl.key=certs/es01/es01.key
          - xpack.security.transport.ssl.certificate=certs/es01/es01.crt
          - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
          - xpack.security.transport.ssl.verification_mode=certificate
          - xpack.license.self_generated.type=${LICENSE}
        mem_limit: ${MEM_LIMIT}
        ulimits:
          memlock:
            soft: -1
            hard: -1
        healthcheck:
          test:
            [
              "CMD-SHELL",
              "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
            ]
          interval: 10s
          timeout: 10s
          retries: 120
    
      es02:
        depends_on:
          - es01
        image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
        volumes:
          - D:/docker/es/config/certs:/usr/share/elasticsearch/config/certs
          - D:/docker/es/data/note02:/usr/share/elasticsearch/data
        environment:
          - node.name=es02
          - cluster.name=${CLUSTER_NAME}
          - cluster.initial_master_nodes=es01,es02,es03
          - discovery.seed_hosts=es01,es03
          - bootstrap.memory_lock=true
          - xpack.security.enabled=true
          - xpack.security.http.ssl.enabled=true
          - xpack.security.http.ssl.key=certs/es02/es02.key
          - xpack.security.http.ssl.certificate=certs/es02/es02.crt
          - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
          - xpack.security.transport.ssl.enabled=true
          - xpack.security.transport.ssl.key=certs/es02/es02.key
          - xpack.security.transport.ssl.certificate=certs/es02/es02.crt
          - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
          - xpack.security.transport.ssl.verification_mode=certificate
          - xpack.license.self_generated.type=${LICENSE}
        mem_limit: ${MEM_LIMIT}
        ulimits:
          memlock:
            soft: -1
            hard: -1
        healthcheck:
          test:
            [
              "CMD-SHELL",
              "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
            ]
          interval: 10s
          timeout: 10s
          retries: 120
    
      es03:
        depends_on:
          - es02
        image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
        volumes:
          - D:/docker/es/config/certs:/usr/share/elasticsearch/config/certs
          - D:/docker/es/data/note03:/usr/share/elasticsearch/data
        environment:
          - node.name=es03
          - cluster.name=${CLUSTER_NAME}
          - cluster.initial_master_nodes=es01,es02,es03
          - discovery.seed_hosts=es01,es02
          - bootstrap.memory_lock=true
          - xpack.security.enabled=true
          - xpack.security.http.ssl.enabled=true
          - xpack.security.http.ssl.key=certs/es03/es03.key
          - xpack.security.http.ssl.certificate=certs/es03/es03.crt
          - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
          - xpack.security.transport.ssl.enabled=true
          - xpack.security.transport.ssl.key=certs/es03/es03.key
          - xpack.security.transport.ssl.certificate=certs/es03/es03.crt
          - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
          - xpack.security.transport.ssl.verification_mode=certificate
          - xpack.license.self_generated.type=${LICENSE}
        mem_limit: ${MEM_LIMIT}
        ulimits:
          memlock:
            soft: -1
            hard: -1
        healthcheck:
          test:
            [
              "CMD-SHELL",
              "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
            ]
          interval: 10s
          timeout: 10s
          retries: 120
    
      kibana:
        depends_on:
          es01:
            condition: service_healthy
          es02:
            condition: service_healthy
          es03:
            condition: service_healthy
        image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
        volumes:
          - D:/docker/es/config/certs:/usr/share/kibana/config/certs
          - D:/docker/es/data/kabana:/usr/share/kibana/data
        ports:
          - ${KIBANA_PORT}:5601
        environment:
          - SERVERNAME=kibana
          - ELASTICSEARCH_HOSTS=https://es01:9200
          - ELASTICSEARCH_USERNAME=kibana_system
          - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
          - ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
        mem_limit: ${MEM_LIMIT}
        healthcheck:
          test:
            [
              "CMD-SHELL",
              "curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'",
            ]
          interval: 10s
          timeout: 10s
          retries: 120
    
    volumes:
      certs:
        driver: local
      esdata01:
        driver: local
      esdata02:
        driver: local
      esdata03:
        driver: local
      kibanadata:
        driver: local
相关推荐
_.Switch3 小时前
高级Python自动化运维:容器安全与网络策略的深度解析
运维·网络·python·安全·自动化·devops
2401_850410833 小时前
文件系统和日志管理
linux·运维·服务器
JokerSZ.3 小时前
【基于LSM的ELF文件安全模块设计】参考
运维·网络·安全
芯盾时代4 小时前
数字身份发展趋势前瞻:身份韧性与安全
运维·安全·网络安全·密码学·信息与通信
心灵彼岸-诗和远方5 小时前
DevOps业务价值流:架构设计最佳实践
运维·产品经理·devops
一只哒布刘5 小时前
NFS服务器
运维·服务器
南猿北者5 小时前
docker容器
docker·容器
苹果醋36 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
二十雨辰6 小时前
[linux]docker基础
linux·运维·docker
time never ceases6 小时前
使用docker方式进行Oracle数据库的物理迁移(helowin/oracle_11g)
数据库·docker·oracle