常用命令
docker info
查看docker信息
#客户端信息
Client: Docker Engine - Community
Version: 26.1.4
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.14.1
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.27.1
Path: /usr/libexec/docker/cli-plugins/docker-compose
#服务端信息
Server:
#容器的个数
Containers: 39
#运行的个数
Running: 36
#暂停的个数
Paused: 0
#停止的个数
Stopped: 3
#镜像的数量
Images: 15
#服务版本
Server Version: 26.1.4
#存储驱动
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
#日志格式
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
#swarm信息
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d2d58213f83a351ca8f528a95fbd145f5654e957
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.84GiB
Name: k8s-01
ID: 17f347ce-624d-4f51-9ad6-dd8f9706f92d
#日志存放目录,建议ssd应用挂着在此处,避免文件占用量过高
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
#镜像仓库
Registry Mirrors:
https://11777p0c.mirror.aliyuncs.com/
https://hub-mirror.c.163.com/
https://mirror.baidubce.com/
https://docker.m.daocloud.io/
https://docker.1panel.live/
Live Restore Enabled: false
docker version
查看版本信息
#客户信息
Client: Docker Engine - Community
Version: 26.1.4
API version: 1.45
Go version: go1.21.11
Git commit: 5650f9b
Built: Wed Jun 5 11:32:04 2024
OS/Arch: linux/amd64
Context: default
#服务端信息
Server: Docker Engine - Community
Engine:
Version: 26.1.4
API version: 1.45 (minimum version 1.24)
Go version: go1.21.11
Git commit: de5c9cf
Built: Wed Jun 5 11:31:02 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.33
GitCommit: d2d58213f83a351ca8f528a95fbd145f5654e957
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker search
- 格式
#docker search 镜像名称
docker search nignx
- 搜索nginx镜像
#镜像名称 镜像描述 星数 是否是官方
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 19996 [OK]
unit Official build of NGINX Unit: Universal Web … 32 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 92
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 154
docker pull
- 格式
#docker pull 镜像名称
docker pull nginx
- 拉取nginx镜像到本地
Using default tag: latest #镜像版本
latest: Pulling from library/nginx #镜像来源
a2abf6c4d29d: Pull complete #镜像分层
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest #镜像仓库
docker tag
- 格式
docker tag 老镜像名称:版本 新镜像名称:版本
- 为镜像重新设置标签
docker tag nginx:latest registry.cn-beijing.aliyuncs.com/publicspaces/nginx:latest
docker login
- 格式
#docker login --username=用户名称 仓库地址
docker login --username=ai***ai registry.cn-beijing.aliyuncs.com
- 登录阿里云仓库
Password: #输入密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
docker push
- 格式
#docker push 镜像名称
docker push registry.cn-beijing.aliyuncs.com/publicspaces/nginx:latest
- 拉取nginx镜像到本地
The push refers to repository [registry.cn-beijing.aliyuncs.com/publicspaces/nginx] #仓库地址
d874fd2bc83b: Layer already exists #分层推送
32ce5f6a5106: Layer already exists
f1db227348d0: Layer already exists
b8d6e692a25e: Layer already exists
e379e8aedd4d: Layer already exists
2edcec3590a4: Layer already exists
latest: digest: sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3 size: 1570
docker run
- 格式
#docker run -d --name 容器名称 镜像名称:版本号
docker run -d -p 80:80 --name nginx-test nginx:latest
- 启动nginx镜像
#nginx容器的ID号
9c264affcd9f3c8b7ecc0c1f6b7e900835ef2f9bdb1e35aead90ab3d0255d684
docker logs
- 格式
#docker logs 容器名称或容器ID
docker logs 9c264affcd9f3c8b7
docker logs nginx-test
- 查看nginx容器的日志
#容器内日志信息
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/07/10 12:21:29 [notice] 1#1: using the "epoll" event method
2024/07/10 12:21:29 [notice] 1#1: nginx/1.21.5
2024/07/10 12:21:29 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2024/07/10 12:21:29 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2024/07/10 12:21:29 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/07/10 12:21:29 [notice] 1#1: start worker processes
2024/07/10 12:21:29 [notice] 1#1: start worker process 31
2024/07/10 12:21:29 [notice] 1#1: start worker process 32
docker ps
- 查看所有正在运行的容器
docker ps
- 查看所有容器
docker ps -a
- 输出内容
#容器ID号 容器使用的镜像 容器运行的命令 容器创建时长 容器的状态 容器端口映射关系 容器的名称
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c264affcd9f nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp nginx-test
docker exec
- 格式
#docker exec -it 容器名称或容器ID /bin/bash
docker exec -it nginx-test /bin/bash
- 进入nginx容器
#容器的用户@容器的ID
root@9c264affcd9f:/#
docker stop
- 格式
docker stop 容器名称或容器ID
- 停止nginx容器
docker stop nginx-test
docker cp
- 格式
#从容器内拷贝到本地
#docker cp 容器名称:容器内路径或文件 本地路径
#从本地拷贝到容器内
#docker cp 本地路径或文件 容器名称:容器内路径
- 拷贝nginx容器内数据到本地
docker cp nginx-test:/etc/nginx/conf.d/default.conf /tmp/
- 拷贝本地数据到nginx容器
docker cp /root/calico-v3_20.yaml nginx-test:/tmp/
docker rmi
- 格式
#docker rmi -f 镜像名称或镜像ID
docker rmi -f nginx:latest
- 删除nginx镜像
Untagged: nginx:latest #被删除镜像的名称和版本号
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 #被删除镜像的ID号
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85 #分层删除
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
docker rm
- 格式
#docker rm -f 容器名称或容器ID
docker rm -f nginx-test
- 删除nginx容器
nginx-test #被删除容器的名称
docker images
- 格式
docker images
docker images 镜像名称或镜像ID
- 查看所有镜像
#镜像仓库 镜像标签 镜像ID 镜像创建时长 镜像大小
REPOSITORY TAG IMAGE ID CREATED SIZE
calico/node v3.20.6 daeec7e26e1f 23 months ago 156MB
calico/pod2daemon-flexvol v3.20.6 39b166f3f936 23 months ago 18.6MB
calico/cni v3.20.6 13b6f63a50d6 23 months ago 138MB
calico/kube-controllers v3.20.6 4dc6e7685020 23 months ago 60.2MB
registry.cn-beijing.aliyuncs.com/publicspaces/nginx latest 605c77e624dd 2 years ago 141MB
registry.aliyuncs.com/google_containers/kube-proxy v1.20.9 8dbf9a6aa186 2 years ago 99.7MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.20.9 295014c114b3 2 years ago 47.3MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.20.9 eb07fd4ad3b4 2 years ago 116MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.20.9 0d0d57e4f64c 2 years ago 122MB
registry.aliyuncs.com/google_containers/kube-proxy v1.20.0 10cc881966cf 3 years ago 118MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.20.0 ca9843d3b545 3 years ago 122MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.20.0 3138b6e3d471 3 years ago 46.4MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.20.0 b9fa1895dcaa 3 years ago 116MB
registry.aliyuncs.com/google_containers/etcd 3.4.13-0 0369cf4303ff 3 years ago 253MB
registry.aliyuncs.com/google_containers/coredns 1.7.0 bfe3a36ebd25 4 years ago 45.2MB
registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 4 years ago 683kB
- 查看nginx镜像
#镜像仓库 镜像标签 镜像ID 镜像创建时长 镜像大小
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-beijing.aliyuncs.com/publicspaces/nginx latest 605c77e624dd 2 years ago 141MB
docker build
docker history
- 格式
docker history 镜像名称或镜像ID
- 查看nginx镜像历史创建记录
#镜像名称 创建时长 基于什么创建的 镜像的大小 镜像提交记录
IMAGE CREATED CREATED BY SIZE COMMENT
605c77e624dd 2 years ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 2 years ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 2 years ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 2 years ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 2 years ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 2 years ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 2 years ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 2 years ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 2 years ago /bin/sh -c set -x && addgroup --system -… 61.1MB
<missing> 2 years ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 2 years ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.1 0B
<missing> 2 years ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.5 0B
<missing> 2 years ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 2 years ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:09675d11695f65c55… 80.4MB
docker commit
- 格式
#docker commit 容器名称或容器ID 镜像名称:版本号
docker commit nginx-test nginx:latest
- 根据nginx容器创建一个新镜像
sha256:d3389fafa10f6d4eef696ef0b76276b1e39a22574b157f579e726791fcf8d486 #镜像ID
Dockerfile
常用命令
FROM
- 镜像继承哪个基础镜像
- 如:FROM centos:7
MAINTAINER
- 标明镜像作者信息,可以使用LABEL替换
- 如: MAINTAINER <“wangmingqu;[email protected]”>
LABEL
- 为镜像设置标签,可以描述镜像用途等
- 如:LABEL maintainer=“wangmingqu”
- 如:LABEL email=“[email protected]”
- 如:LABEL describe=“第一个dockerfile镜像”
RUN
- 用来执行shell命令
- 如:RUN yum -y install vim
EXPOSE
- 用于暴露容器的端口号
- 如:EXPOSE 80
CMD
- 用于容器启动时默认执行的命令,但此处设置的命令可以被覆盖
- 如:CMD systemctl start nginx
ENTRYPOINT
- 用于容器启动时执行的命令,此处设置的命令不会被覆盖
VOLUME
- 用于暴露容器的挂载点,将宿主机目录挂载后可以永久暴露容器内的数据
- 如:VOLUME /usr/local/nginx/logs/
ENV
- 用于设置容器的环境变量,一般不写入容器,而是通过-e参数在生成容器是写入变量
- 如:ENV HOST_NAME docker-01
ADD
- 用于复制本地文件到容器,如果复制的是压缩包,可以自动解压;注意宿主机上的目录为相对目录(相对dockerfile的目录)
- 如:ADD ./nginx.conf /etc/nginx/conf.d/nginx.conf
COPY
- 用于复制本地文件到容器,拷贝的资源不会解压;注意宿主机上的目录为相对目录(相对dockerfile的目录)
- 如:COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf
WORKDIR
- 用于指定容器的工作目录
- 如:WORKDIR /usr/local/nginx
USER
- 用于指定容器使用的用户
- 如:USER nginx
应用举例
编辑配置文件
#指定镜像基于centos7构建(此处使用的是本地镜像)
FROM my-centos:7.9.2009
#描述镜像的基本信息
LABEL maintainer="wangmingqu"
LABEL email="[email protected]"
#创建一个目录
RUN mkdir -p /data/logs
#创建一个用户
RUN useradd nginx
#目录修改用户
RUN chown -R nginx:nginx /data/logs
#指定使用的用户启动容器
USER nginx
#指定的工作目录
WORKDIR /data/
#暴露端口
EXPOSE 80
#设置变量
ENV myhostname="docker01"
#应用变量
RUN echo $myhostname > /tmp/linshi.txt
#添加一个文件
ADD /root/kubeadm-config.yaml /tmp/
#添加一个文件
COPY /root/images_kube.sh /tmp/
#添加一个挂载点
VOLUME /data/logs/
创建构建镜像
- 格式
docker build -f dockerfile的文件目录 -t 镜像名称:镜像版本号 .
#注意:特别注意命令的最后一定要加上"空格和."
- 举例
docker build -f /root/dockerfile-test -t test:v1.0 .
制作镜像
制作思路
- 选择合适的镜像作为基础镜像,如:alpine、busybox、scratch等;
- 使用多阶段构建镜像,将编译操作和生成镜像操作分开;
- 合理使用挂载操作,将数据、配置、日志等大文件挂载到宿主机;
制作镜像
- 编辑Dockerfile
FROM centos
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 按照 vim编辑器
#RUN yum -y install vim
# 安装ifconfig命令查看网络
#RUN yum -y install net-tools
# 安装java8以及lib库,必须先安装glibc
#RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
# ADD是相对路径jar,把jdk-8u40-linux-x64.gz添加到容器中,按照包必须是Dockerfile文件在同一位置
ADD jdk-8u40-linux-x64.gz /usr/local/java/
#配置java环境
ENV JAVA_HOME /usr/local/java/jdk1.8.0_40
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "success................ok"
CMD /bin/bash
- 制作镜像
docker build -f dockerfile-java -t java-wmq:v1.0
多阶段制作镜像
- 编辑Dockerfile
# 第一个阶段:编译应用程序
FROM golang:1.16 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# 第二个阶段:运行应用程序
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]
- 制作镜像
docker build -f dockerfile-golang -t golang-wmq:v1.0
scratch镜像
是一个全空的镜像,没有任何资源与依赖,一般不适用。
- 编辑Dockerfile
# 第一个阶段:编译应用程序
FROM golang:1.16 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# 第二个阶段:运行应用程序
FROM scratch
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]
- 制作镜像
docker build -f dockerfile-golang -t golang-scratch-wmq:v1.0
标签:ago,容器,Kubernetes,05,years,nginx,镜像,Docker,docker
From: https://blog.csdn.net/weixin_40901913/article/details/140569892