docker快速入门
Docker是一个虚拟环境容器,可以将你的开发环境、代码、配置文件等一并打包到这个容器中,并发布和应用到任意平台中。
Docker的三个概念
镜像
Docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的配置参数。 镜像不包含任何动态数据,其内容在构建之后也不会被改变。镜像可以用来创建Docker容器,用户可以使用设备上已有的镜像来安装多个相同的Docker容器。
docker镜像是一个多层的文件系统,最底层是引导文件系统,即bootfs
。第二层是rootfs
,是一种或者多种操作系统(例如:Debian
或者Ubuntu
)。再上层是只读的其他文件系统(镜像层),最上层是读写的文件系统,运行的程序是在这层执行。
容器
Docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的配置参数。 镜像不包含任何动态数据,其内容在构建之后也不会被改变。镜像可以用来创建Docker容器,用户可以使用设备上已有的镜像来安装多个相同的Docker容器。
仓库
集中存放镜像文件的地方。用户创建完镜像后,可以将其上传到公共仓库或者私有仓库,需要在另一台主机上使用该镜像时,只需要从仓库上下载即可。
运行我们的第一个容器
(下载)启动一个ubuntu
系统,并启动shell交互
docker run -i -t ubuntu /bin/bash
# 查看系统版本
uname -a
# 退出,并停止容器
exit
# 退出,不停止容器
CTRL+P+Q
守护方式启动一个支持shell
交互的ubuntu
系统
docker run --name daemon_dave -i -t -d ubuntu /bin/bash
附着到一个容器
# docker attach [docker container name or id]
docker attach daemon_dave
查看容器
# 查看运行中的容器
docker ps
# 查看所有容器
docker ps -a
重启和关闭容器
# docker start [docker container name or id]
docker start daemon_dave
# docker stop [docker container name or id]
docker stop daemon_dave
查看容器日志
# docker logs [docker container name or id]
docker logs daemon_dave
查看容器内进程
# docker top [docker container name or id]
docker top daemon_dave
统计信息
docker stats
# docker stats [docker container name or id list]
docker stats daemon_dave deamon_dave
在容器内部运行进程
docker exec -t -i daemon_dave /bin/bash
获取容器信息
# docker inspect [docker container name or id list]
docker inspect daemon_dave
# 指定标志查看
docker inspect --format='{{ . State.Running }}' daemon_dave
删除容器
# docker rm [docker container name or id]
docker rm daemon_dave
Docker镜像
查看镜像列表
docker images
拉去镜像
docker pull ubuntu
查找镜像
docker search ubuntu
Docker构建镜像
使用docker commit
命令(不讲解)
使用dockerfile
和docker build
命令(推荐)
编写dockerfile
文件
# Version: 0.0.1
FROM ubuntu
MAINTAINER Ziegler "[email protected]"
RUN apt-get update && apt-get install -y nginx
RUN echo 'Hi, I am in your container' > /usr/share/nginx/html/index.html
EXPOSE 80
编译docker镜像
docker build -t="mydocker" .
# 忽略缓存构建
docker build --no-cache -t="mydocker" .
查看指定镜像
docker images mydocker
镜像历史
docker history mydocker
用新镜像启动容器
docker run -d -p 80:80 --name static_web mydocker nginx -g "daemon off;"
# 查看端口
> docker port static_web
80/tcp -> 0.0.0.0:80
查看最近的容器
docker ps -l
删除镜像
docker rmi static_web
# 删除所有镜像
docker rmi 'docker images -a -q'
docker network
- 注意:docker容器是一个虚拟机,它有自己的网络,固不能直接访问。
创建app
网络
docker network create app
查看app
网络
> docker network inspect myweb
[
{
"Name": "app",
"Id": "977f98e232619890bf096d7dc05cafb5e864c2923343d32bfbf2b24807c653b6",
"Created": "2023-09-21T12:27:31.975736849Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
上面的app
网络没有任何容器
app
网络下启动一个redis
容器
docker run -d --net=app --name db redis
再次查看app
网络
> docker network inspect app
[
{
"Name": "app",
"Id": "977f98e232619890bf096d7dc05cafb5e864c2923343d32bfbf2b24807c653b6",
"Created": "2023-09-21T12:27:31.975736849Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0c5cd9ccb641d3e6fd0a36d8afb20892186d4a24024db5dd6e73ad1bc758ef4c": {
"Name": "db",
"EndpointID": "90b79286dd7a415bfc4f7d957fcf98bbd826667d019c6ab5192c731bcb42b5a3",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
包含一个名字为db
的容器,容器的IP:172.27.0.2
创建另外一个容器,通过app
网路连接redis
docker run -it --net=app --rm redis redis-cli -h db
> docker run -it --net=app --rm redis redis-cli -h db
db:6379> keys *
(empty array)
db:6379> set greet "hello world"
OK
db:6379> keys *
1) "greet"
db:6379>
再次检测app
网络
> docker network inspect app
[
{
"Name": "app",
"Id": "977f98e232619890bf096d7dc05cafb5e864c2923343d32bfbf2b24807c653b6",
"Created": "2023-09-21T12:27:31.975736849Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0c5cd9ccb641d3e6fd0a36d8afb20892186d4a24024db5dd6e73ad1bc758ef4c": {
"Name": "db",
"EndpointID": "90b79286dd7a415bfc4f7d957fcf98bbd826667d019c6ab5192c731bcb42b5a3",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
},
"9da11d04a018d7bbdba840c14c13e03c2d4fe528e228b040cf1c9c6112be0af8": {
"Name": "peaceful_knuth",
"EndpointID": "02ffb2e2cfe5c4af104583e5bbb46b45e064683a2a36dfd26f5f97db923494e5",
"MacAddress": "02:42:ac:1b:00:03",
"IPv4Address": "172.27.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
上面app
网络包含2个容器,一个是名称为db
的redis
容器,另外一个是用redis-cli
连接db
的容器。
将已存在的容器连接Docker网络
在启动一个redis
的bash
容器
> docker run -it --rm --name rc redis bash
root@62b018e3b79e:/data# redis-cli -h db
Could not connect to Redis at db:6379: Name or service not known
not connected>
连接失败
检查app
网络
C:\Users\Administrator>docker network inspect app
[
{
"Name": "app",
"Id": "977f98e232619890bf096d7dc05cafb5e864c2923343d32bfbf2b24807c653b6",
"Created": "2023-09-21T12:27:31.975736849Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0c5cd9ccb641d3e6fd0a36d8afb20892186d4a24024db5dd6e73ad1bc758ef4c": {
"Name": "db",
"EndpointID": "90b79286dd7a415bfc4f7d957fcf98bbd826667d019c6ab5192c731bcb42b5a3",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
将rc
容器添加到app
网络
docker network connect app rc
再看app
网络
C:\Users\Administrator>docker network inspect app
[
{
"Name": "app",
"Id": "977f98e232619890bf096d7dc05cafb5e864c2923343d32bfbf2b24807c653b6",
"Created": "2023-09-21T12:27:31.975736849Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0c5cd9ccb641d3e6fd0a36d8afb20892186d4a24024db5dd6e73ad1bc758ef4c": {
"Name": "db",
"EndpointID": "90b79286dd7a415bfc4f7d957fcf98bbd826667d019c6ab5192c731bcb42b5a3",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
},
"6a10947555ddeba0415334b031f18bceac0d00877ea300a6ae35dc1e1a9f6445": {
"Name": "rc",
"EndpointID": "edb6cdd445160b1f1bacd8878bb1c3f02b1b6247a47fa002843fc2f03591d167",
"MacAddress": "02:42:ac:1b:00:03",
"IPv4Address": "172.27.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
再rc容器再次连接db的redis,成功
I:\my-docker\mongo.git\trunk\7.0>docker run -it --rm --name rc redis bash
root@6a10947555dd:/data# redis-cli -h db
Could not connect to Redis at db:6379: Name or service not known
not connected> redis-cli -h db
(error) ERR unknown command 'redis-cli', with args beginning with: '-h' 'db'
db:6379> exit
root@6a10947555dd:/data# redis-cli -h db
db:6379>
断开rc
容器与app
网络的连接
docker network disconnect app rc
rc
容器redis-cli
无法与db
的redis
交互
移除网络
docker network rm app
罗列所有网络
docker network ls
docker compose
编写docker-compose.yaml
,启动上面的nginx
的dockerfile
services:
myweb:
build: .
command: nginx -g "daemon off;"
ports:
- "80:80"
目录结构
./
- Dockerfile
- docker-compose.yaml
docker compose命令
# 在存在docker-compose.yaml的目录执行下面指令
# 启动
docker compose up
# 后台启动
docker compose up -d
# 查看
docker compose ps
# 查看日志
docker compose logs
# 停止服务
docker compose stop
# 删除服务
docker compose rm
# 停止并删除服务
docker compose down
# 查看进程
docker compose top
# 查看镜像
docker compose images
# 重启
docker compose restart
# 编译
docker compose build
# 执行命令
# docker compose exec [OPTIONS] SERVICE COMMAND [ARGS...]
docker compose exec myweb bash
# 一次性执行
docker compose run myweb bash