1. docker自动化安装
-
执行命令:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
-
启动docker:
sudo systemctl start docker
-
换源:
vi /etc/docker/deamon.json
写入以下内容:
{ "registry-mirrors": [ "https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ] }
然后重启docker:
service docker restart
-
验证是否安装成功:
# 拉取镜像 docker pull hello-world # 执行hello-world docker run hello-world
-
运行后若出现以下内容,则安装成功:
Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
2. docker命令
-
docker进程
# 启动docker service docker start # 关机docker service docker stop # 重启docker service docker restart
-
镜像操作
# 镜像列表 docker images # 检索镜像, 从镜像仓库中检索 docker search xxx # 下载镜像 docker pull xxx # 删除镜像 docker rmi -f xxx # 创建镜像 docekr commit -m="提交的描述信息"-a="作者" 容器ID 要创建的目标镜像名:[标签名] 例:docker commit -m="vim cmd add ok" -a="tsy" 543161563f tsy/myubuntu:1.0 # 镜像历史查询 docker history [OPTIONS] IMAGE 例:docker history tsy/myubuntu [OPTIONS]: -H:以可读的格式打印镜像大小和日期,默认为true; --no-trunc:显示完整的提交记录; -q:仅列出提交记录ID。
-
容器操作
(1)运行:
# 创建并后台执行 docker run -i -t -d centos:latest -d,指定容器运行与前台或者后台,不加上时前台; -i,打开 STDIN,用于控制台交互; -t,支持终端登录。 # 运行一个带命令在后台不断执行的容器 docker run -d centos:latest ping www.baidu.com # 运行一个在后台不断执行的容器,同时带有命令,程序被终止后还能重启继续跑 docker run -d --restart=always centos:latest ping www.baidu.com # 指定容器名 docker run -d --name=cc_centos centos:latest # 暴露容器端口 80,并与宿主机端口 8080 绑定 docker run -d --name=cc_centos -p 8080:80 centos:latest # 指定容器与宿主机目录(/home/yihui/html/www)共享 docker run -d --name=cc_centos -v /home/cc/html/www:/var/www centos:latest
(2)基本操作:
# 查看容器列表,列出所有的容器 docker ps -a --no-trunc # 启动容器,start后面可以跟上容器名,或者容器id docker start xxx # 关闭容器 docker stop xxx # 重启 docker restart xxx # 删除 docker rm xxx
(3)进阶:
# 查询xxx容器的日志 docker logs -f -t --since="2022-08-10" --tail=10 xxx --since:此参数指定了输出日志开始日期,即只输出指定日期之后的日志; -f:查看实时日志; -t:查看日志产生的日期; --tail=10:查看最后的 10 条日志。 # 将当前目录的test.md文件拷贝到容器的/tmp目录下 docker cp test.md centos:/tmp # 将容器的/tmp/test.md目录拷贝到当前目录下 docker cp centos:/tmp/test.md ./out.md # 进入容器 docker exec -it centos /bin/bash # 获取容器所有信息 docker inspect centos