Docker快速上手
初识Docker
什么是Docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker如何解决项目依赖问题
- Docker允许开发中将应用,依赖,函数库,配置一起打包,形成可移植镜像
- Docker应用运行在容器中,使用沙箱机制,相互隔离
镜像和容器
- 镜像(image):Docker将应用及其所需的依赖,函数库,环境等打包,称为镜像 ----readonly
- 容器(container):镜像中的应用程序运行后形成的进程就是容器,只是Docker会给容器做隔离,对外不可见
Docker架构
Docker配置阿里云镜像
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors":["https://n0dwemtq.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
Docker基本操作
docker操作
systemctl start docker #开启docker
systemctl status docker #查看docker状态
systemctl stop docker #停止docker
systemctl status docker #重启docker
镜像操作
示例
[root@iz2vcaf13jzrddh914393ez ~]# docker pull nginx #拉取操作
....
[root@iz2vcaf13jzrddh914393ez ~]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 10 months ago 141MB
[root@iz2vcaf13jzrddh914393ez ~]# docker save --help #查看docker save指令文档
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
[root@iz2vcaf13jzrddh914393ez ~]# docker save -o nginx.tar nginx # 将nginx镜像保存到nginx.tar
[root@iz2vcaf13jzrddh914393ez ~]# docker rmi nginx #删除镜像
....
[root@iz2vcaf13jzrddh914393ez ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@iz2vcaf13jzrddh914393ez ~]# docker load --help #查看docker load指令文档
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
[root@iz2vcaf13jzrddh914393ez ~]# docker load -i nginx.tar #将镜像加载到docker
...
[root@iz2vcaf13jzrddh914393ez ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 10 months ago 141MB
容器相关命令
示例
[root@localhost ~]# docker run --name nm -p 80:80 -d nginx #运行容器
# --name:指定容器名称
#-p:指定端口映射 -d:后台运行
#-d:后台运行
7d50e60a2c349c377d0d92f7f3c517b8f009176a214e5e5e186cafa0f7396f67
[root@localhost ~]# docker ps #查看容器状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d50e60a2c34 nginx "/docker-entrypoint.…" 26 seconds ago Up 26 seconds 0.0.0.0:80->80/tcp mn
[root@localhost ~]# docker logs mn
Error: No such container: mn
[root@localhost ~]# docker logs nm #查看名为nm的容器的日志信息
...
[root@localhost ~]# docker exec -it nm bash #进入容器内部
# 修改nginx容器的index页面
root@e2a9f52aedd6:/#cd /usr/share/nginx/html #路径在dockerhub nginx官方可以找到
root@e2a9f52aedd6:/usr/share/nginx/html# ls
50x.html index.html
root@e2a9f52aedd6:/usr/share/nginx/html# vi index.html
bash: vi: command not found #在容器中的命令都是系统必须的而vi是在centos中下载的并不是必须的,所以容器中不能执行vi操作
#在容器内的修改是不会保存的,当再次开启容器时之前的操作不会保留,对容器的操作应该使用容器卷技术
root@e2a9f52aedd6:/usr/share/nginx/html# exit #退出容器
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2a9f52aedd6 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nm
[root@localhost ~]# docker stop nm #停止指定容器
nm
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker start nm #启动停止的容器
nm
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2a9f52aedd6 nginx "/docker-entrypoint.…" 22 minutes ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nm
[root@localhost ~]# docker rm nm #删除某个容器
Error response from daemon: You cannot remove a running container e2a9f52aedd6225eac6a592aa3fc8dae72a2902b17ed993c29528d14c47705a3. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm -f nm #强制删除某个容器
nm
[root@localhost conf]# docker update mysql --restart=always #当docker启动时容器自启
nginx的index.html文件路径
数据卷
[root@localhost ~]# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.
[root@localhost ~]# docker volume create html #创建数据卷
html
[root@localhost ~]# docker volume ls #显示数据卷
DRIVER VOLUME NAME
local 3ba00d994ca0b79a3482832ddb870a08dabaafc7fb2115ad140f0b07e619e7d5
local 9efc646d1d18338456272be518609c6e5499bab40c0c230bf706521f3a91aa5e
local 19bae3b01692528be9d4d12d5504f7a045426303435070ed9ffe793fe2a06a3f
local 23a95af39d59a32a72462d6ab917b8ea34845e46abd3ca91c8db3abb4c26da56
local 38ed6ca0bfc41e006d00c2bd66c020ae45363a1e6974b479d0c8d3be3e2f9a44
local 617ed08c7d4f148b9fe696d298d0437d0656c2bedff46ecc31534318c1341c4c
local 909ef9a36586380555f7d0382e0cf8cb7d853a496b289d9d819376a13ce94bf8
local 1690663082c832fbb968ecb00d8e4f2d66bc69d5c7f170c3934b5a092bd1571b
local html
[root@localhost ~]# docker inspect html #显示数据卷详细信息
[
{
"CreatedAt": "2022-11-22T18:26:32+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/html/_data", #挂载点
"Name": "html",
"Options": {},
"Scope": "local"
}
]
[root@localhost ~]# docker volume prune #删除本地所有未使用的卷
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
617ed08c7d4f148b9fe696d298d0437d0656c2bedff46ecc31534318c1341c4c
9efc646d1d18338456272be518609c6e5499bab40c0c230bf706521f3a91aa5e
ad0f495e00d82bac3f9f2a12adf33e2cf4c853371c9215e51331dffa73fb0684
ef17d142de5be03d07da1d89f6ecf0173ea7aaa8e61cf16f4b1f2931bb769e0d
1690663082c832fbb968ecb00d8e4f2d66bc69d5c7f170c3934b5a092bd1571b
html
[root@localhost ~]# docker volume create html
html
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local html
[root@localhost ~]# docker volume rm html
html
[root@localhost ~]# docker volume ls #删除指定的卷
DRIVER VOLUME NAME
数据卷挂载
[root@localhost ~]#docker run --name mn -p 80:80 -v html:/usr/share/nginx/html -d nginx
b33484b837d30762caf81119d664c718ea9a021a9627dfdb5d2c6fff3c39036e
[root@localhost ~]# docker inspect html
[
{
"CreatedAt": "2022-11-22T18:36:49+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/html/_data",
"Name": "html",
"Options": {},
"Scope": "local"
}
]
[root@localhost ~]# cd /var/lib/docker/volumes/html/_data #进入数据卷的目录(宿主机)
[root@localhost _data]# ls
50x.html index.html
[root@localhost _data]# vim index.html #修改之后访问会发现nginx页面被修改
Dockerfile自定义镜像
示例:
将以下命令写到Dockerfile文件
# 指定基础镜像
FROM java:8-alpine
#copy当前目录下的docker-demo.jar文件到指定路径
COPY ./docker-demo.jar /tmp/app.jar
# 暴露端口
EXPOSE 8090
# 入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar
在同级目录下执行
[root@localhost docker-demo]# docker build -t javaweb:2.0 . #最后一定要在加个空格之后加个点表示Dockerfile的
#目录 而.表示当前目录