• Docker常用命令,Docker安装Nginx、Redis、Jenkins、tomcat、MySQL


    常用命令

    拉取镜像:docker pull xxx
    启动镜像:docker run --name xxx 8080:8080 -d xxx
    查看容器:docker ps xxx 停止容器:docker stop xxx 启动容器:docker start xxx 删除容器:docker rm xxx
    删除镜像:docker rmi xxx
    进入容器控制台:
    docker exec -it xxx bash
    拷贝容器中文件到宿主机:docker cp xxx:/usr/local/tomcat/conf/server.xml /usr/local/docker/tomcat/conf
    拷贝宿主机文件到容器:docker cp /usr/local/docker/tomcat/conf/server.xml xxx:/usr/local/tomcat/conf
    更改docker源:https://cr.console.aliyun.com/undefined/instances/mirrors
    查看docker容器信息:docker inspect xxx(容器名称或id)
    docker网络:查看与创建
      docker network ls
      docker network create -d bridge --subnet=xx.xx.xx.xx/16 --gateway=xx.xx.xx.xx mybridge
      docker run --net=host # --net=host代表直接将docker容器的端口暴露到和宿主机通网段下,就不做端口映射了
    指定ip:docker run --network mybridge --ip xx.xx.xx.xx
    查看日志:docker logs -t -f --tail 100 xxx

    一、docker-nginx:

    docker pull nginx 
    docker run --name nginx -p 80:80 -d nginx
    mkdir -p /usr/local/docker/nginx/www /usr/local/docker/nginx/conf /usr/local/docker/nginx/logs
    docker cp ${container-id}:/etc/nginx/nginx.conf /usr/local/docker/nginx/conf
    docker stop nginx
    docker rm nginx
    docker run -d -p 80:80 --net host --name nginx -v /usr/local/docker/nginx/www:/usr/share/nginx/html -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/logs:/var/log/nginx nginx

    二、docker-redis:

    docker pull redis
    mkdir -p /usr/local/docker/redis/conf /usr/local/docker/redis/data
    docker run -p 6379:6379 --name redis -v /usr/local/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /usr/local/docker/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
    docker run -p 6379:6379 --name redis -v /usr/local/redis/etc/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --appendonly yes

    firewall-cmd --zone=public --add-port=6380/tcp --permanent
    firewall-cmd --reload
    chmod 777 /xxx/docker/redis
    docker run --net=host --name redis -v /xxx/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /xxx/docker/redis:/etc/redis --privileged=true -d redis:5.0 redis-server /etc/redis/redis.conf

    docker run -it --name sentinel -p 26379:26379 --net=host -v /usr/local/docker/redis/conf/sentinel.conf:/etc/redis/sentinel.conf -d redis redis-sentinel /etc/redis/sentinel.conf #哨兵 #
    /usr/local/docker/redis/conf挂载到/etc/redis/redis.conf # /usr/local/docker/redis/data挂载到/data # --appendonly yes 开启持久化

    三、docker-jenkins

    1.新建Dockerfile
    在/usr/local/docker/jenkins下

    touch Dockerfile
    vi Dockerfile

    2.加入以下内容:

    FROM jenkins
    
    USER root
    #清除了基础镜像设置的源,切换成腾讯云的jessie源
    #使用非腾讯云环境的需要将 tencentyun 改为 aliyun
    #RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list 
    # && echo "deb http://mirrors.aliyun.com/debian jessie main contrib non-free" > /etc/apt/sources.list 
    # && echo "deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list 
    # && echo "deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list
    #更新源并安装缺少的包
    RUN apt-get update && apt-get install -y libltdl7 && apt-get update
    
    ARG dockerGid=999
    
    RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
    
    # 安装 docker-compose 因为等下构建环境的需要
    RUN curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    
    RUN chmod +x /usr/local/bin/docker-compose

    3.构建

    docker build . -t jenkins

    4.在启动Jenkins时,需要先创建一个Jenkins的配置目录,并且挂载到docker 里的Jenkins目录下

    mkdir -p /var/jenkins_home

    5.修改目录权限(很重要!)

    chown -R 1000 /var/jenkins_home

    6.运行 Jenkins

    docker run --name jenkins -p 8080:8080 -p 50000:50000 
    -v /var/run/docker.sock:/var/run/docker.sock 
    -v $(which docker):/bin/docker 
    -v /var/jenkins_home:/var/jenkins_home 
    -d jenkins

    7.进入容器

    docker exec -it jenkins /bin/bash

    8.查看密码

    cat /var/jenkins_home/secrets/initialAdminPassword

    四、安装tomcat

    docker run --name tomcat -p 8079:8079 -v /usr/local/docker/tomcat/webapps:/usr/local/tomcat/webapps -v /usr/local/docker/tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml -d tomcat
    docker exec -it tomcat bash //进入控制台

    五、docker-rabbitmq

    docker pull rabbitmq:3.7-management
    
    docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -v `pwd`/data:/var/lib/rabbitmq --hostname myRabbit  -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:3.7-management
    docker run -d --name rabbitmq  -p 5672:5672 -p 15672:15672 --hostname rabbit  -e RABBITMQ_DEFAULT_USER=root -e RABBITMQ_DEFAULT_PASS=@1234 rabbitmq:3.7-management
    #rabbitmq命令 rabbitmqctl list_users rabbitmqctl add_user username password rabbitmqctl set_user_tags username administrator(角色分为:none、management、policymaker、monitoring、administrator) rabbitmqctl change_password userName newPassword rabbitmqctl delete_user username

     六、docker-elasticsearch

    docker pull elasticsearch:6.8.0
    
    docker run -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -p 9200:9200 -p 9300:9300 --name elasticsearch elasticsearch:6.8.0
    #安装插件
    docker exec -it xxxxx /bin/bash
    cd /usr/share/elasticsearch/bin
    plugin install mobz/elasticsearch-head

     七、docker-mysql

    docker pull mysql:5.7
    
    docker run -itd -p 3306:3306 --name mysql -v /e/xxx/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --privileged=true mysql:5.7
    # mysql 8.0
    docker run -itd -p 3306:3306 --name mysql -v /e/lcmjava/mysql:/etc/mysql -v /e/lcmjava/mysql/data:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123456 --privileged=true mysql:8.0
    ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY '123456';

     八、docker-mongo

    #查看mongo版本
    https://hub.docker.com/_/mongo?tab=tags&page=1
    #版本可以是latest
    docker pull mongo:4.2
    #--auth表示需要密码登录
    ocker run -itd --name mongo -p 27017:27017 mongo --auth
    #进入容器并设置账号密码
    docker exec -it mongo mongo admin
    db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]});
    db.auth('admin', '123456')

    八、docker-kafak

    https://www.cnblogs.com/linjiqin/p/11891776.html

    操作容器:

    #docker中 启动所有的容器命令
    docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
    
    #docker中 关闭所有的容器命令
    docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
    
    docker中 删除所有的容器命令
    #docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
    
    docker中 删除所有的镜像
    docker rmi $(docker images | awk '{print $3}' |tail -n +2)
    
    #tail -n +2 表示从第二行开始读取

    来源:

    菜鸟教程

    https://www.cnblogs.com/stulzq/p/8627360.html

  • 相关阅读:
    Spring通过springframework.data的@PageableDefault注解分页
    Specifications动态查询
    IDEA生成序列号serialVersionUID快捷键
    JS组件系列——表格组件神器:bootstrap table(还没细看)
    南昌网络赛 Distance on the tree(可持久化线段树)
    南昌网络赛 Max answer(单调栈)
    HDU 6148 Valley Numer(数位DP)
    python 二位dict
    Codeforces Round #230 (Div. 2) 题解
    Codeforces Round #228 (Div. 2) 题解
  • 原文地址:https://www.cnblogs.com/lcmlyj/p/11207821.html
Copyright © 2020-2023  润新知