docker基础命令快速入门

目录

  1. docker基本命令
  2. docker 网络
  3. docker compose

Docker介绍

Docker是一个虚拟环境容器可以将你的开发环境代码配置文件等一并打包到这个容器中,并发布应用任意平台中。

Docker的三个概念

镜像

Docker镜像一个特殊文件系统,除了提供容器运行时所需的程序、库、资源配置文件外,还包含了一些为运行准备配置参数镜像不包含任何动态数据,其内容构建之后也不会被改变。镜像可以用来创建Docker容器用户可以使用设备上已有的镜像安装多个相同的Docker容器

docker镜像是一个多层的文件系统,最底层是引导文件系统,即bootfs(Boot file system)。第二层是rootfs,是一种或者多种操作系统例如Debian或者Ubuntu)。再上层是只读的其他文件系统(镜像层),最上层是读写文件系统运行程序是在这层执行

docker镜像层级

容器

容器是Docker 镜像的一个运行实例可以理解为一个轻量级虚拟机。容器包含了运行应用程序所需的所有组件,包括操作系统应用程序依赖库等。

仓库

集中存放镜像文件的地方。用户创建完镜像后,可以将其上传公共仓库或者私有仓库,需要在另一台主机上使用该镜像时,只需要从仓库上下载即可

java代码打比方

镜像就是类,容器就是类的实例,仓库就是包。要使用一个容器,就去对应的包(仓库)里找到类(镜像),创建实例(容器)。

运行我们的第一个容器

下载启动一个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命令(不讲解

使用dockerfiledocker build命令推荐

编写dockerfile文件

# Version: 0.0.1
FROM ubuntu
MAINTAINER Ziegler "550360139@qq.com"
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

创建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个容器,一个是名称dbredis容器,另外一个是用redis-cli连接db的容器。

将已存在的容器连接Docker网络

启动一个redisbash容器

> 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:UsersAdministrator>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:UsersAdministrator>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-dockermongo.gittrunk7.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无法与dbredis交互

移除网络

docker network rm app

罗列所有网络

docker network ls

docker compose

Compose用于定义和运行多容器 Docker 应用程序工具

编写docker-compose.yaml启动上面的nginxdockerfile

services:
  myweb:
    environment:
      - TZ=Asia/Shanghai
    build: .
    command: nginx -g "daemon off;"
    ports:
      - "80:80"

TZ=Asia/Shanghai设置容器时区,查看容器时间date -R

目录结构

./
 - 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

清理docker缓存

docker system prune -a --force
# 查看
docker system df

总结

  1. 熟练使用docker基础命令和网络命令
  2. 学习编写dockerfile,可以直接使用dockerfile编译部署项目,就可以不保存上传镜像。
  3. 简单项目,尽量使用docker compose工具,更加方便的部署多个容器以及容器间的互联一键启动。

原文地址:https://blog.csdn.net/weixin_40677588/article/details/134655504

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20682.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注