yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum install docker-ce
systemctl start docker
systemctl start docker
systemctl stop docker
systemctl restart docker
systemctl status docker
systemctl enable docker
docker images
docker search 镜像名称
docker pull 镜像名称:版本号
docker rmi 镜像id
docker ps –a
docker run 参数
参数说明:
-i:保持容器运行。通常与 -t 同时使用。加入it这两个参数后,容器创建后自动进入容器中,退出容器后,容器自动关闭。
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用。
-d:以守护(后台)模式运行容器。创建一个容器在后台运行,需要使用docker exec 进入容器。退出后,容器不会关闭。
-it:创建的容器一般称为交互式容器,-id 创建的容器一般称为守护式容器
–name:为创建的容器命名。
示例:
docker run -it --name=容器名字 镜像名称:镜像版本 /bin/bash
docker exec -it 容器名 /bin/bash
docker stop 容器名称
docker start 容器名称
docker rm 容器名称
docker inspect 容器名称
docker logs -f 容器id
docker run -it --name=容器名称 -v /root/data:/root/data_container 镜像名称:镜像版本 /bin/bash
注意事项:
docker run –it --name=c3 –v /volume centos:7 /bin/bash
docker run –it --name=c1 --volumes-from c3 centos:7 /bin/bash
docker run –it --name=c2 --volumes-from c3 centos:7 /bin/bash
docker pull mysql:版本号
mkdir ~/mysql
cd ~/mysql
docker run -id \
-p 宿主机端口:容器端口 \
--name=容器名称 \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=数据库密码 \
mysql:版本号
docker pull tomcat:版本号
mkdir ~/tomcat
cd ~/tomcat
docker run -id --name=容器名称 -p 宿主机端口:8080 -v $PWD:/usr/local/tomcat/webapps tomcat:版本号
docker pull nginx:版本号
mkdir ~/nginx
cd ~/nginx
vim nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
docker run -id --name=容器名称 -p 宿主机端口:容器端口 -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx -v $PWD/html:/usr/share/nginx/html nginx:版本号
mkdir -p nginx/{
conf,conf.d,html,logs}
cd nginx/conf/
vi nginx.conf
复制以下内容
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
cd nginx/conf.d/
vi default.conf
进行自定义配置
server {
listen 80; # 监听端口号
server_name localhost; # 宿主机ip
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.116.130:8080; # 反向代理ip:端口号
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 配置第二个代理
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.116.130:8081;
index index.html index.htm;
}
}
docker run -d -p 80:80 --name nginx -v /root/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/docker/nginx/conf.d:/etc/nginx/conf.d -v /root/docker/nginx/logs:/var/log/nginx nginx:latest
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
# 开始
upstream myserver {
server 192.168.116.130:8080; # 地址1
server 192.168.116.130:8081; # 地址2
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://myserver;
root html;
index index.html index.htm;
}
}
# 结束
}
Nginx配置动静分离
docker pull redis:版本号
mkdir ~/redis
cd ~/redis
docker run -id --name=容器名称 -p 宿主机端口:容器端口 redis:版本号
创建宿主机 redis 容器的数据和配置文件目录
mkdir redis/{
conf,data} -p
进入conf目录下创建 redis.conf 并编写自定义配置
cd redis/conf
vi redis.conf
创建并运行一个名为 redis 的容器
docker run -p 6379:6379 --name redis -v $PWD/conf/redis.conf:/etc/redis/redis.conf:ro -v $PWD/data:/data:rw -d redis:5.0 redis-server /etc/redis/redis.conf --appendonly yes
查看状态
systemctl status firewalld.service
打开防火墙
systemctl start firewalld.service
关闭防火墙
systemctl stop firewalld.service
开启防火墙
systemctl enable firewalld.service
禁用防火墙
systemctl disable firewalld.service
安装并配置redis主从复制
docker commit 容器id 镜像名称:版本号
docker save -o 压缩文件名称 镜像名称:版本号
docker load –i 压缩文件名称
docker bulid –f dockerfile文件路径 –t 镜像名称:版本号
FROM java:8
MAINTAINER itheima <[email protected]>
ADD springboot-hello-0.0.1-SNAPSHOT.jar app.jar
CMD java -jar app.jar
docker build -f ./springboot_dockerfile -t app .
docker run -id -p 9000:8080 app
http://192.168.116.128:9000/hello
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -version
rm /usr/local/bin/docker-compose
mkdir ~/docker-compose
cd ~/docker-compose
version: '3'
services:
nginx:
image: nginx
ports:
- 80:80
links:
- app
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
app:
image: app
expose:
- "8080"
mkdir -p ./nginx/conf.d
server {
listen 80;
access_log off;
location / {
proxy_pass http://app:8080;
}
}
docker-compose up
http://192.168.116.128/hello
若出现502错误,可尝试关闭防火墙测试
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
docker pull registry
docker run -id --name=容器名称 -p 宿主机端口:容器端口 registry
vim /etc/docker/daemon.json
在上述文件中添加一个key,保存退出。此步用于让 docker 信任私有仓库地址;注意将私有仓库服务器ip修改为自己私有仓库服务器真实ip
{“insecure-registries”: [“私有仓库服务器ip:5000”]}
systemctl restart docker
docker start registry
docker tag centos:7 私有仓库服务器IP:5000/centos:7
docker push 私有仓库服务器IP:5000/centos:7
docker pull 私有仓库服务器ip:5000/centos:7