常用命令
拉取镜像: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-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:/etc/redis/redis.conf -v /usr/local/docker/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yesdocker run -p 6379:6379 --name redis -v /usr/local/redis/etc/redis.conf:/etc/redis/redis.conf -d redis redis-server --appendonly yes
# /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-managementdocker 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.0docker 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
#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 表示从第二行开始读取
来源:
菜鸟教程