docker stop `docker ps -aq`---------停止本地电脑上的所有环境容器进程
docker rm `docker ps -aq`---------删除所有容器
docker rmi `docker images`---------删除所有镜像
docker ps -a ---------查看所有容器
docker images ---------查看所有镜像
docker search ---------查找相关镜像
docker pull 镜像:版本 ---------拉取镜像
docker run -d(后台运行) -v(容器卷挂载) -p(端口映射) --name(自定义容器名) --restart=always(开机自启动) ---------运行容器
docker logs 容器名/容器id ---------查看容器运行日志
docker login -u 用户名 -p 密码 ---------登陆到dockerhub
docker commit -m "描述信息" -a "作者" 基础镜像名 你想要的镜像名:版本号(不填默认为latest)---------提交镜像到dockerhub
docker logout---------退出dockerhub
docker exec -it 容器id /bin/bash ---------后台运行进入容器内部
docker rmi `docker images -f dangling=true`-------删除tag为<none>的镜像。这些镜像为在构建过程中产生的中间镜像
docker system prune------清理没有使用的数据,包括镜像数据,已经停止的容器
docker system df-------Docker整体磁盘使用率的概况,包括镜像、容器和(本地)volume
docker inspect --format ‘{{.State.Running}}’ 容器名-------查看容器是否在运行,若是返回true
docker ps -q-------显示所有容器的容器ID
docker ps -f name=容器名-----过滤并展示name=容器名的容器信息
docker login -u 用户名 -p 密码
docker commit -m "test nginx commit" -a "hzhii" nginx hzhnginx:v1.0
docker tag hzhnginx:v1.0 5925docker/hzhnginx:v1.0
docker push 用户名/镜像名:Tag
docker logout
占一个坑,后续补充
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
docker-compose.yml 配置文件编写详解
version: "3.5" #表示使用第几代语法来构建 docker-compose.yaml 文件
services: #compose 需要启动的服务,多个服务之间保留一个空格行
minio: #需要配置minio的服务
image: minio/minio #镜像名称,可通过docker search查找或去docker hub查找
container_name: "minio" #自定义镜像的名称
ports: #端口绑定
- "9000:9000"
- "9010:9010"
volumes: #容器卷挂载目录
- "/mydata/minio/data:/data"
environment: #minio配置文件参数
MINIO_ACCESS_KEY: "minioadmin"
MINIO_SECRET_KEY: "minioadmin"
command: server /data --console-address ":9010" #表示以这个命令来启动容器
deploy: #指定与服务的部署和运行有关的配置。只在 swarm 模式下才会有用。
restart_policy: #重试策略
condition: on-failure
restart: always #是否开机自启动
logging: #日志文件相关信息
driver: json-file #通过json格式记录
options:
max-size: "200k" #单个文件最大值
max-file: "10" #最多多少个文件
version: '3.3'
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
TZ: Asia/Shanghai
MYSQL_DATABASE: pro
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
ports:
- 3306:3306
volumes:
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysql/conf.d
- /mydata/mysql/init:/docker-entrypoint-initdb.d/
redis:
image: redis:5.0.14-alpine3.15
container_name: redis
restart: always
command: ["redis-server","/etc/redis/redis.conf"]
ports:
- 6379:6379
volumes:
- /mydata/redis/data:/data
- /mydata/redis/conf/redis.conf:/etc/redis/redis.conf
rabbitmq:
image: rabbitmq:3.8.3-management
container_name: rabbitmq
restart: always
ports:
- 15672:15672
- 5672:5672
volumes:
- /mydata/rabbitmq/data:/var/lib/rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=root
nginx:
image: nginx:stable-alpine-perl
container_name: nginx
restart: always
ports:
- 80:80
volumes:
- /mydata/nginx/html:/usr/share/nginx/html
- /mydata/nginx/logs:/var/log/nginx
- /mydata/nginx/conf.d:/etc/nginx/conf.d
- /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
tomcat:
restart: always
image: tomcat:9.0
container_name: tomcat
privileged: true
ports:
- 8080:8080
volumes:
- /mydata/tomcat/webapps:/usr/local/tomcat/webapps
- /mydata/tomcat/conf:/usr/local/tomcat/conf
- /mydata/tomcat/logs:/usr/local/tomcat/logs
environment:
TZ: Asia/Shanghai
jenkins:
restart: always
image: jenkins/jenkins:lts-centos7-jdk8
container_name: jenkins
ports:
- 10240:8080
- 10241:50000
volumes:
- /mydata/jenkins/home:/var/jenkins_home
- $(which docker):/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
docker-compose up -d
执行以上的compose文件。具体的相关操作可以看以下的shell脚本,在tomcat部分展示了如何获取配置文件并通过docker cp
命令复制到自己的目录的流程。echo -e "\e[1;31m运行镜像生成临时容器....\e[0m"
docker run --name tomcat -d -p 8080:8080 tomcat:9.0
echo -e "\e[1;31m进行文件复制....\e[0m"
docker cp tomcat:/usr/local/tomcat/conf /mydata/tomcat
docker cp tomcat:/usr/local/tomcat/logs /mydata/tomcat
docker cp tomcat:/usr/local/tomcat/webapps /mydata/tomcat
echo -e "\e[1;31m停止tomcat并删除临时容器....\e[0m"
docker stop tomcat
docker rm tomcat
docker中“-v”与“-mount”的区别是:挂载主机目录使用“-v”时,如果宿主机上没有指定文件不会报错,会自动创建指定文件;当使用“-mount”时,如果宿主机中没有这个文件会报错找不到指定文件,不会自动创建指定文件
需要提前创建/mydata文件夹,并把该shell放置在该目录下。确保该shell生成的yaml文件,能在该目录下被docker-compose up -d
启动
echo -e "\e[1;31m创建mysql相关目录....\e[0m"
mkdir -p /mydata/mysql/data
mkdir -p /mydata/mysql/conf
mkdir -p /mydata/mysql/init
chmod -R 777 /mydata/mysql
rm -rf /mydata/mysql/data/*
echo -e "\e[1;31m复制sql文件到初始化目录....\e[0m"
cp /sql/pro.sql /mydata/mysql/init
echo -e "\e[1;31m写入mysql配置文件....\e[0m"
cat > /mydata/mysql/conf/my.cnf << EOF
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8mb4
collation-server = utf8mb4_general_ci
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
EOF
echo -e "\e[1;31m创建redis相关目录....\e[0m"
mkdir -p /mydata/redis/data
mkdir -p /mydata/redis/conf
chmod -R 777 /mydata/redis
rm -rf /mydata/redis/data/*
rm -rf /mydata/redis/conf/*
echo -e "\e[1;31m写入redis相关配置文件....\e[0m"
cat > /mydata/redis/conf/redis.conf << EOF
appendonly yes
EOF
chmod 777 /mydata/redis/conf/redis.conf
echo -e "\e[1;31m创建rabbitmq相关配置文件\e[0m"
mkdir -p /mydata/rabbitmq/data
chmod -R 777 /mydata/rabbitmq
rm -rf /mydata/rabbitmq/data/*
echo -e "\e[1;31m创建Nginx相关配置文件\e[0m"
mkdir -p /mydata/nginx/html /mydata/nginx/conf /mydata/nginx/conf.d /mydata/nginx/logs
rm -rf /mydata/nginx/html/*
rm -rf /mydata/nginx/conf/*
rm -rf /mydata/nginx/conf.d/*
rm -rf /mydata/nginx/logs/*
chmod -R 777 /mydata/nginx
echo -e "\e[1;31m写入nginx相关配置文件\e[0m"
cat > /mydata/nginx/conf/nginx.conf << EOF
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 300;
server {
listen 80;
server_name 192.168.56.103;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
EOF
echo -e "\e[1;31m写入nginx默认首页\e[0m"
cat > /mydata/nginx/html/index.html << EOF
welcome to Nginx!</h3>
EOF
echo -e "\e[1;31m创建Tomcat文件夹\e[0m"
mkdir -p /mydata/tomcat
chmod -R 777 /mydata/tomcat
echo -e "\e[1;31m清除docker-compose.yaml文件\e[0m"
rm -rf /mydata/docker-compose.yaml
echo -e "\e[1;31m写入docker-compose.yaml文件....\e[0m"
cat > /mydata/docker-compose.yaml << EOF
version: '3.3'
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
TZ: Asia/Shanghai
MYSQL_DATABASE: pro
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
ports:
- 3306:3306
volumes:
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysql/conf.d
- /mydata/mysql/init:/docker-entrypoint-initdb.d/
redis:
image: redis:5.0.14-alpine3.15
container_name: redis
restart: always
command: ["redis-server","/etc/redis/redis.conf"]
ports:
- 6379:6379
volumes:
- /mydata/redis/data:/data
- /mydata/redis/conf/redis.conf:/etc/redis/redis.conf
rabbitmq:
image: rabbitmq:3.8.3-management
container_name: rabbitmq
restart: always
ports:
- 15672:15672
- 5672:5672
volumes:
- /mydata/rabbitmq/data:/var/lib/rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=root
nginx:
image: nginx:stable-alpine-perl
container_name: nginx
restart: always
ports:
- 80:80
volumes:
- /mydata/nginx/html:/usr/share/nginx/html
- /mydata/nginx/logs:/var/log/nginx
- /mydata/nginx/conf.d:/etc/nginx/conf.d
- /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
tomcat:
restart: always
image: tomcat:9.0
container_name: tomcat
privileged: true
ports:
- 8080:8080
volumes:
- /mydata/tomcat/webapps:/usr/local/tomcat/webapps
- /mydata/tomcat/conf:/usr/local/tomcat/conf
- /mydata/tomcat/logs:/usr/local/tomcat/logs
environment:
TZ: Asia/Shanghai
EOF
echo -e "\e[1;31m创建临时tomcat并获取配置文件....\e[0m"
docker run --name tomcat -d -p 8080:8080 tomcat:9.0
echo -e "\e[1;31m进行文件复制....\e[0m"
docker cp tomcat:/usr/local/tomcat/conf /mydata/tomcat
docker cp tomcat:/usr/local/tomcat/logs /mydata/tomcat
docker cp tomcat:/usr/local/tomcat/webapps /mydata/tomcat
echo -e "\e[1;31m停止tomcat并删除临时容器....\e[0m"
docker stop tomcat
docker rm tomcat
echo -e "\e[1;31m启动所有基础环境中....\e[0m"
docker-compose up -d
echo -e "\e[1;31m请核对docker进程的状态\e[0m"
docker ps -a
出现该问题的原因是因为Shell脚本在Windows系统编写时,每行结尾是\r\n,而在Linux系统中行每行结尾是\n,所以在Linux系统中运行脚本时,会认为\r是一个字符,导致运行错误。
解决办法:请参考$‘\r‘: command not found的解决方法
首先先看一段代码:
docker cp tomcat:/usr/local/tomcat/conf /mydata/test/conf
docker cp tomcat:/usr/local/tomcat/logs /mydata/test/logs
docker cp tomcat:/usr/local/tomcat/webapps /mydata/test/webapps
很正常的一段代码吧,看着也没问题。这段代码的意思是将容器内conf,logs,webapps的所有配置文件拷贝到我们自己的相关目录下。那我们运行其中一段试试看。
目录结构如下:
[root@localhost test]# tree -a
.
├── conf
├── logs
└── webapps
3 directories, 0 files
执行命令docker cp tomcat:/usr/local/tomcat/conf /mydata/test/conf
,查看目录复制情况
[root@localhost test]# tree
.
├── conf
│ └── conf
│ ├── Catalina
│ │ └── localhost
│ ├── catalina.policy
│ ├── catalina.properties
│ ├── context.xml
│ ├── jaspic-providers.xml
│ ├── jaspic-providers.xsd
│ ├── logging.properties
│ ├── server.xml
│ ├── tomcat-users.xml
│ ├── tomcat-users.xsd
│ └── web.xml
├── logs
└── webapps
6 directories, 10 files
发现问题了吗,问题就在docker cp命令,他把整个conf目录复制过来了,而我们本意是想要conf下的配置文件。我们进行容器卷挂载的时候,我们挂载的目录为: - /mydata/test/conf:/usr/local/tomcat/conf,而现在我们的挂载目录为 /mydata/test/conf/conf/…配置文件。挂载就对不上了呀,对不上就自然无法启动了
解决办法:docker cp tomcat:/usr/local/tomcat/conf /mydata/tomcat
既然它直接复制整个文件夹,那我们就让它复制到我们的tomcat目录下就可以了,无需再自己创建\conf,\logs,\webapp的文件夹了