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
查看容器ip:docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
查看日志: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
GRANT ALL ON *.* TO ‘root’@’localhost’;
update mysql.user set password=password('root') where user='root';
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

 八、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 表示从第二行开始读取

 九、docker-springboot

1.准备好springboot jar包

2.编写Dockerfile:

# 镜像来源的基础
FROM java:8
# 创建镜像容器时,会在Docker容器中创建对应的文件夹,因为SpringBoot内嵌的Tomcat工作路径需要这个文件夹
VOLUME /tmp
# 构建Docker镜像时,传入参数
# 比如:docker build --build-arg JAR_NAME=manage-0.0.1-SNAPSHOT.jar
ARG JAR_NAME
# 复制jar包名称,简化名称
ADD ${JAR_NAME} app.jar
# docker容器中暴露出的端口,用于docker容器启动时,映射端口
# 比如:docker run -di --name 容器名称 -p 8080:8080 镜像名称,这里前面的8080对应宿主机的端口,后面的8080对应docker容器中的端口,意思就是将宿主机的8080端口和docker容器的8080端口映射上去
EXPOSE 8080
# 最后一步 执行命令
# 格式一:ENTRYPOINT exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar
# 格式二:ENTRYPOINT ["java", "${JAVA_OPTS}", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
ENTRYPOINT exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar

3.打镜像并启动

# 构建命令:docker build --build-arg JAR_NAME=manage-0.0.1-SNAPSHOT.jar -t app .

# 启动命令:docker run -di --name app01 -p 8080:8080 app

 十、docker-gitlab-runner

# docker安装gitlab-runner
docker pull gitlab/gitlab-runner
# 查看版本
docker run --rm -t -i gitlab/gitlab-runner --help
# 启动并挂载
docker run --name gitlab-runner --restart always -v /usr/local/var/docker/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner

# 进入容器,注册runner
docker exec -it gitlab-runner /bin/bash
gitlab-runner register

# 输入gitlab服务器地址:Enter the GitLab instance URL (for example, https://gitlab.com/):
http://172.17.0.1
# Enter the registration token: 输入token,从gitlab获得
iZj2bKKef****
# Enter a description for the runner:描述,根据情况自定义
my project test
# Enter tags for the runner (comma-separated):输入标签,重要,和.gitlab-ci.yml的tag会进行关联,根据逗号隔开
dev,test
# Enter optional maintenance note for the runner: 主要说明
lcm_test
# Enter an executor: parallels, shell, ssh, virtualbox, docker+machine, docker-ssh, docker, docker-ssh+machine, kubernetes, custom: 执行器,docker最优
docker
# Enter the default Docker image (for example, ruby:2.7):设置默认的docker镜像 tico/docker
tico/docker

 注意,最后一步的镜像设置很重要,意味着每次的gitlab-runner会新生成一个docker镜像容器,是在这个镜像的基础上,所以,一个有基本命令的镜像是很必要的,可以用 tico/docker

来源:

菜鸟教程

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

十二、Docker容器安装vim

apt-get update
apt-get install -y vim

 

posted on 2019-07-18 15:48  lyjlyjlyj  阅读(419)  评论(0编辑  收藏  举报

导航