Docker
2021-08-21 本文已影响0人
古飞_数据
4C/8G-Cenos7.9
安装docker和docker-compose
#删除系统自带的Docker
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
#安装官方的docker repo
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#安装docker
yum install -y docker-ce docker-ce-cli containerd.io
#配置docker镜像源和数据目录
#自动创建目录/data/docker
mkdir /etc/docker
cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com"],
"data-root": "/data/docker"
}
EOF
#安装docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker管理
#启动docker
systemctl start docker
#关闭docker
systemctl stop docker
#查看镜像
docker images
#查看运行中的容器
docker ps
#查看所有的容器,包括运行和停止的
docker ps -a
#拉取最新的镜像
docker pull mysql/mysql-server:latest
#查看images的信息
docker inspect mysql/mysql-server:latest
docker run --name=mysql3306 -d mysql/mysql-server:latest
docker ps
docker volume ls
docker logs mysql3306
docker exec -it mysql3306 /bin/bash
docker stop mysql3306
docker rm mysql3306
1.停用全部运行中的容器:
docker stop $(docker ps -q)
2.删除全部容器:
docker rm $(docker ps -aq)
3.删除所有镜像
docker rmi $(docker images -qa)
docker stop $(docker ps -qa)
docker rm $(docker ps -qa)
docker rmi $(docker images -qa)
docker+Redis搭建
cd /data/docker
mkdir redis
cd redis/
[root@VM-0-32-centos redis]# tree ../redis/
../redis/
|-- build
| `-- Dockerfile
|-- data
|-- docker-compose.yml
`-- redis.conf
vi build/Dockerfile
From redis
CMD ["redis-server","/etc/redis/redis.conf"]
vi docker-compose.yml
version: '3.1'
services:
redis:
build: build
container_name: redis
restart: always
ports:
- 6379:6379
volumes:
- ./data:/data
- ./redis.conf:/etc/redis/redis.conf
docker-compose up -d
docker ps
docker exec -it redis /bin/bash
=========================================================
docker+nginx搭建
[root@VM-0-32-centos nginx]# tree ../nginx/
../nginx/
|-- build
| |-- Dockerfile
| `-- nginx-1.17.7.tar.gz
|-- docker-compose.yml
|-- log
|-- nginx.conf
`-- wwwroot
vi build/Dockerfile
-------------
FROM centos:7
RUN yum install unzip make gcc gcc-c++ automake autoconf pcre pcre-devel zlip zlib-devel -y
ADD nginx-1.17.7.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.17.7
RUN ./configure
RUN make && make install
WORKDIR /
CMD ["/usr/local/nginx/sbin/nginx","-c","/usr/local/nginx/conf/nginx.conf","-g","daemon off;"]
nginx.conf
version: '3.1'
services:
nginx:
build: build
container_name: nginx
restart: always
ports:
- 80:80
volumes:
- ./nginx.conf:/usr/local/nginx/conf/nginx.conf
- ./log:/var/log/nginx
- ./wwwroot:/usr/local/nginx/html
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
curl 127.0.0.1
================================
docker+mysql8
[root@VM-0-32-centos mysql8]# tree ../mysql8/
../mysql8/
|-- data
|-- docker-compose.yml
`-- my.cnf
#docker-compose.yml
version: '3.1'
services:
mysql:
image: mysql:8.0.18
restart: always
ports:
- 3306:3306
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
environment:
MYSQL_ROOT_PASSWORD: 123
volumes:
- ./data:/var/lib/mysql
- ./my.cnf:/etc/my.cnf
# 监控
# mysqld_exporter:
# image: prom/mysqld_exporter
# restart: always
# network_mode: host
# environment:
# - DATA_SOURCE_NAME=root:123@(127.0.0.1:3306)/
#my.cnf
[mysqld]
datadir = /var/lib/mysql
port = 3306
socket = /var/run/mysqld/mysqld.sock
pid-file = /var/run/mysqld/mysqld.pid
symbolic-links=0
#timeout
interactive_timeout = 300
wait_timeout = 300
#character set
character-set-server = utf8mb4
open_files_limit = 65535
max_connections = 100
解决cpu 100%问题
解决agetty进程cpu占用率100%,宿主机和容器需要执行
systemctl stop getty@tty1.service
systemctl mask getty@tty1.service