concept
- docker is an open source application container engine
- Implementation based on GO
Installation (based on centos7)
- Download and install docker
# yum 包更新到最新
yum update
# 安装需要的软件包, yum-util 提供 yum-config-manager 功能,另外两个是 devicemapper 驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker,出现输入的界面都按 y
yum install -y docker-ce
# 查看docker版本,验证是否验证成
docker -v
- Configure aliyun mirror acceleration
1. Enter the Alibaba Cloud container mirror service:
https://cr.console.aliyun.com/cn-beijing/instances/mirrors
2. Configure your own Docker accelerator
and find daemon.json in the /etc/docker directory file, if not, directly vi daemon.json
docker command
Process related commands
- Start the docker service
systemctl start docker
- stop docker service
systemctl stop docker
- Restart the docker service
systemctl restart docker
- View docker status
systemctl status docker
- Set docker to start automatically at boot
systemctl enable docker
Image related commands
- view mirror
docker images
docker images -q # 查看所有镜像id
- Search mirrors (find the required mirrors from the network)
docker search 镜像名称
- Pull the image (download the image from the docker warehouse to the local if the version is not specified, the default is latest)
docker pull 镜像名称 # 名称:版本号
# 可以去hub.docker.com 查看对应镜像的版本号
- delete mirror
docker rmi 镜像id # 删除指定id的镜像
- generate mirror
docker build -t 镜像名称:镜像版本 .
Container (container) related commands
- view container list
docker ps # 查看正在运行的容器
docker ps -a # 查看所有历史容器
- Create and run the container
docker run -it --name=n1 镜像名称:版本 /bin/bash # 创建一个容器名为 n1 ,并分配一个终端,进入 /bin/bash, 退出容器后容器自动关闭
docker run -id --name=n2 镜像名称:版本 # 创建一个容器命名为n2,后台运行,并且进入容器后退出也不会关闭容器
-i: Keep the container running, usually used together with -t, use the it parameter to create the container, automatically enter the container after creation, and automatically close after exiting
-t: reassign a pseudo-input terminal for the container
-d: to guard (background) Mode to run the container, create a container to run in the background, you need to manually enter the container, the container will not close after exiting-it: the
created container is generally called an interactive container
-id: the created container is generally called a guardian container
–name: named
- into the container
docker exec -it 容器名称 /bin/bash # 进入容器,并且退出容器不会关闭
- Start the container
docker start 容器名称
- stop container
docker stop 容器名称
- delete container
docker rm 容器名称
- View container details
docker inspect 容器名称
data volume
concept
- Is a directory or file in the host machine
- After the container directory and the host directory are bound, the modifications of both parties are synchronized
- Containers and data volumes have a many-to-many relationship, that is, one container can mount multiple data volumes, and one data volume can also be mounted by multiple containers at the same time
effect
- Container data persistence (the data in it will not be lost after the container is deleted due to failure)
- Communication between external machines and containers
- Data exchange between containers
configuration
- When creating a startup container, use the -v parameter to configure the data volume
docker run -it --name=xx \
-v 宿主机目录:容器内目录
-v 宿主机目录:容器内目录 # 第二个挂载的目录
...
centos:7 # 要创建的容器的镜像名
- Note: 1. The directory must be an absolute path; 2. If the directory does not exist, it will be created automatically; 3. Multiple data volumes can be mounted
data volume container
- Definition: Create a container, mount a directory, and let other containers inherit from this container
- Function: It is convenient for multiple containers to mount the same data volume
- Create a data volume container:
docker run -it --name=n3 -v /volume centos:7
- Bind other containers to the data volume:
docker run -it --name=n1 --volumes-from n3 centos:7
docker application deployment
- Port mapping: the way external machines connect to internal applications
deploy mysql
- Search mysql mirror
docker search mysql
- Pull mysql image
docker pull mysql:5.7
- Create a mysql container based on the image, set port mapping, directory mapping
# 在宿主机/root目录下创建mysql目录,用于存储mysql数据信息
mkdir ~/mysql
cd ~/mysql
docker run -id \
-p 3306:3306 \
--name=c_mysql \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
# -id 守护进程方式创建 mysql 容器
# -p 设置端口映射 容器端口:宿主机端口
# -v 配置文件,日志文件,数据文件映射
# -e MYSQL_ROOT_PASSWORD 设置 mysql 账户密码
docker-compose method
version: '3'
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
ports:
- 3306:3306
network_mode: host
volumes:
- /data/mysql/log:/var/log/mysql
- /data/mysql/data:/var/lib/mysql
- /data/mysql/conf/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
environment:
MYSQL_ROOT_PASSWORD: "123456"
TZ: Asia/Shanghai
- Enter the container and operate mysql
docker exec -it c_mysql /bin/bash
- Use an external machine to connect to mysql in the container
Connect to visual tools such as navicat or SQLyog, and enter the host ip and mapped port number
deploy tomcat
- search mirror
- pull image
docker pull tomcat
- Create container, set port mapping, directory mapping
# 在宿主机/root 目录下创建tomcat目录,用户存放tomcat数据信息
mkdir ~/tomcat
cd ~/tomcat
docker run -id \
--name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat
# -v 宿主机当前目录映射到tomcat存放目录
- Create a project under the directory Enter
/text/index.html
in the browser toip:8080/text/index.html
access successfully
deploy nginx
- Search nginx mirror
- Pull the nginx mirror
- Create container, set port mapping, directory mapping
# 在宿主机/root 目录下创建nginx目录,用于存放nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf 目录下创建nginx.conf文件
touch nginx.conf
vim nginx.conf
- and paste the following
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
server {
listen 80;
server_name 10.2.128.46;
#前端页面转发
location / {
root /usr/share/nginx/html/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
docker run
docker run -id --name=c_nginx \
-p 80:80
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
docker-compose
version: '3'
services:
nginx:
image: nginx:1.20
container_name: nginx
environment:
TZ: Asia/Shanghai
restart: always
ports:
- 80:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./html:/usr/share/nginx/html
- /data/nginx/log:/var/log/nginx
- test nginx
deploy redis
- Search image
docker search redis - Pull image
docker pull redis:5.0 - Create a container, port mapping
docker run
docker run -id --name=c_redis -p 6379:6379 redis:5.0
docker-compose method
version: '3'
services:
master:
image: redis:5.0
container_name: redis
network_mode: host
restart: always
command: redis-server /etc/redis/redis.conf --appendonly yes
ports:
- 6379:6379
privileged: true
volumes:
- /data/redis/data:/data
- /data/redis/conf/redis.conf:/etc/redis/redis.conf
- /data/redis/log:/var/log/redis
- Use an external machine to connect to redis (redis connection on win)
./redis-cli.exe -h 192.xxx.xxx.xxx -p 6379
Build images using Dockerfile
build steps
- Write Dockerfile
- build image
docker build -f ./dockerfile_centos -t 镜像名称:版本 .
# -f 指定 dockerfile 文件路径
# -t 设置新镜像的名称和版本
# . 代表 dockerfile 寻址路径
Deploy the spring-boot project
- Write Dockerfile
# 文件名 dockerfile_springboot
FROM java:8
MAINTAINER Powerstot <598227099@qq.com>
ADD SpringBootDemo-1.0-SNAPSHOT.jar app.jar
CMD java -jar app.jar
# ADD 重命名 jar 包
# CMD 创建容器后默认执行的命令
- build image
docker build \
-f ~/dockerfile/dockerfile_springboot \
-t springboot \
.
# -f 指定 dockerfile 文件路径
# -t 名称默认 latest 版本
- Create containers based on images
docker run -id \
--name=springboordemo \
-p 9000:8080 \
springboot
# -p 指定端口,方便外部访问内部的 tomcat 端口
- Enter IP:port to access the project
http://服务器IP:9000/hello
docker-compose service orchestration
introduce
- Docker Compose is a tool for orchestrating distributed deployment of multiple containers, providing a command set to manage the complete development cycle of containerized applications, including service construction, starting and stopping.
- Simply put, it can manage multiple services at the same time, which is convenient for the development of microservice projects
installation steps
- install binaries
curl -L https://get.daocloud.io/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 这是国内 daocloud 的镜像,国外 github 比较慢,要使用 github 的话,直接修改域名为 github.com 即可
- Set file executable permissions
chmod +x /usr/local/bin/docker-compose
- Check whether the version detection is installed successfully
docker-compose -version
uninstall
rm /usr/local/bin/docker-compose
# 二进制包下载 直接删除文件即可
Use docker-compose to orchestrate nginx+springboot projects
- Create docker-compose directory
mkdir ~/docker-compose
cd ~/docker-compose
- Edit the docker-compose.yml file
version: '3'
services:
nginx:
container_name: gotion-data
restart: always
image: java:8
network_mode: gdmo-sp #设置容器的网络模式
privileged: true #用来给容器root权限,不安全的
volumes:
- /home/gdmo-sp/gotion-data/gotion-data-1.0.0.jar:/gotion-data-1.0.0.jar
- /home/gdmo-sp/logs:/logs
- /etc/localtime:/etc/localtime
ports:
- "8124:8124"
enviroment:
-TZ="Asia/Shanghai"
entrypoint: java -server -Xms256m -Xmx256m -jar -Dserver.port=8124 gotion-data-1.0.0.jar
– Create a bridged network
docker network create -d bridge --subnet 162.28.0.0/16 gdmo-sp
-View network
docker network ls
- View network routing
route
- Create ./nginx/conf.d directory
mkdir -p ./nginx/conf.d
# -p 表示没有父目录就连父目录一起创建
- Write the springbootdemo.conf file in the ./nginx/conf.d directory
server {
listen 80;
access_log off;
location / {
proxy_pass http://springboot:8080;
}
}
# 反向代理将 springboot 的 8080 端口代理到 80 端口,外部访问 80 即访问到了项目的 8080 端口
- In the ~/docker-compose directory, use docker-compose to start the container
docker-compose up
# -d 后台启动,不加就是前台启动,打印日志
# docker-compose 会进行 nginx 和 springboot 两个容器的创建,然后按照配置文件设置反向代理,再 up 两个容器
- test access
http://服务器IP/hello
docker private warehouse
- It is a container, first download the registry image, and then create a container, which is a private warehouse
- Build a private warehouse
# 拉取私有仓库镜像
docker pull registry
# 启动私有仓库容器
docker run -id --name=registry -p 5000:5000 registry
# 浏览器输入地址 http://私有仓库服务器ip:5000/v2/_catalog
# 显示 {
"repositories":[]} 表示私有仓库 搭建成功
# 修改daemon.json
vim /etc/docker/daemon.json
# 添加一个key,用于让 docker 信任私有仓库地址
{
"insecure-registries": ["私有仓库服务器ip:5000"]}
# 重启docker 服务
systemctl restart docker
# 开启 registry 容器
docker start registry
- Upload the image to the warehouse
# 标记镜像为私有仓库的镜像
docker tag centos:7 私有仓库服务器IP:5000/centos:7
# 上传标记的镜像
docker push 私有仓库服务器IP:5000/centos:7
- Pull the image from the warehouse
# 拉取镜像
docker pull 私有仓库服务器ip:5000/centos:7