前言
为什么用容器
- 跨平台性
- 持续部署和测试
- 镜像仓库管理
安装Docker
Windows安装Docker环境Docker Desktop详细步骤-CSDN博客
介绍
- Dockerfile
- image
- container
DockerFile是一段指令,告诉docker我们要构建的image包含哪些东西,会做哪些事情,通过docker build .命令构建成可执行程序,也就是image。然后我们可以运行这个image,也就是docker run命令运行,image 运行起来后就是container
一个简单的Dockerfile
#导入基础image
FROM openjdk:8-alpine
#复制我们需要的程序到image中
COPY springboot-demo-0.0.1-SNAPSHOT.jar /app.jar
#指定工作目录
WORKDIR /
#image运行后会执行的命令
CMD java -jar app.jar
Dockerfile文档 Dockerfile reference | Docker Docs
image的结构
我们可以大致把image分为两类,像mysql和redis这类可以直接使用的image可归类为基础服务image,像java和python这类需要搭配对应编程语言应用使用的image可归类为基础环境image
官方镜像仓库 https://hub.docker.com/
国内加速网站(不一定最新) Docker
image运行成container
docker run的命令格式
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
常用参数
docker run参数文档 docker run | Docker Docs
- -i, --interactive Keep STDIN open even if not attached
即使未连接STDIN(标准输入)也保持打开状态,分配一个交互终端
- -t, --tty Allocate a pseudo-TTY
表示容器启动后会进入其命令行,与it一起使用。
- -d, --detach Run container in background and print container ID
后台运行容器并返回容器ID
- -e, --env Set environment variables
设置镜像中服务运行参数
- --name Assign a name to the container
给容器分配一个名字
- --network Connect a container to a network
给容器分配一个网络
- -p, --publish Publish a container's port(s) to the host
开放容器内端口到宿主机,容器内服务需要端口访问则必须开启(--network host除外)
- -v, --volume Bind mount a volume
绑定一个宿主机目录到容器中,以宿主机中的为准
示例命令
docker run -itd --name hello-world -p 8080:8080 hello-world
其他常用命令
docker pull mysql
从镜像仓库拉去对应镜像,不加版本号默认拉去最新版本
docker images
查看本地所有的镜像
docker ps -a
查看本地所有的容器
其他
- docker stop 停止容器
- docker start 启动容器
- docker rm 删除容器
- docker rmi 删除镜像
简单使用
docker命令文档 Use the Docker command line | Docker Docs
基础服务
mysql,redis镜像
像mysql和redis这种可直接使用的基础服务,可以直接通过指定参数或者配置文件即可得到想要的容器。
- 利用-env参数,指定镜像中服务的运行参数。
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
- 利用-v参数,在宿主机编写好配置文件然后挂载到容器中。
docker run -itd --name redis -p 6379:6379 -v D:\Docker\Redis\conf:/redis/conf redis redis-server /redis/conf/redis.conf
自定义服务
java运行时镜像+Dockerfile
对于运行时环境镜像,一般不会直接运行,会搭配Dockerfile,构建新的镜像然后运行。
- Dockerfile
#导入基础image
FROM openjdk:8-alpine
#复制我们需要的程序到image中
COPY springboot-demo-0.0.1-SNAPSHOT.jar /app.jar
#指定工作目录
WORKDIR /
#image运行后会执行的命令
CMD java -jar app.jar
- 使用上述Dockerfile构建一个自定义镜像
docker build -t springboot-demo:V1.0 .
- 运行我们的自定义容器
docker run -itd --name springboot-demo -p 8080:8080 springboot-demo:V1.0
进阶使用
docker compose
搭建redis cluster集群
version: '1.0'
services:
# redis0配置
redis0:
image: redis
container_name: redis-node0
restart: always
ports:
- 6380:6380
- 16380:16380
volumes:
- ./redis-node0/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis1配置
redis1:
image: redis
container_name: redis-node1
restart: always
ports:
- 6381:6381
- 16381:16381
volumes:
- ./redis-node1/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis2配置
redis2:
image: redis
container_name: redis-node2
restart: always
ports:
- 6382:6382
- 16382:16382
volumes:
- ./redis-node2/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis3配置
redis3:
image: redis
container_name: redis-node3
restart: always
ports:
- 6383:6383
- 16383:16383
volumes:
- ./redis-node3/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis4配置
redis4:
image: redis
container_name: redis-node4
restart: always
ports:
- 6384:6384
- 16384:16384
volumes:
- ./redis-node4/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis5配置
redis5:
image: redis
container_name: redis-node5
restart: always
ports:
- 6385:6385
- 16385:16385
volumes:
- ./redis-node5/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
docker compose -f ./redis-cluster.yml up -d
redis-cli --cluster create 192.168.2.51:6380 192.168.2.51:6381 192.168.2.51:6382 192.168.2.51:6383 192.168.2.51:6384 192.168.2.51:6385 --cluster-replicas 1 -a 123456
一站式服务搭建
kubernetes(k8s)
Kubernetes 是一个可移植、可扩展的开源平台,用于管理容器化的工作负载和服务,方便进行声明式配置和自动化。