Docker 常用命令
Docker 常用命令
一. docker 命令
查看所有容器
docker ps -a
停止所有容器
docker stop `docker ps -a -q`
删除所有容器
docker rm `docker ps -a -q`
docker查看错误日志
docker logs 容器id
docker 容器设置开机启动
docker update --restart=always xxx
docker 进入容器
docker exec -it 容器名或ID /bin/bash
docker 容器内安装 vim
进入容器,执行
apt-get update
apt-get install vim
docker 停止所有容器
docker stop $(docker ps -a -q)
docker 查看日志并搜索
docker logs container-id 2>&1 | grep "Password" #示例搜索Password
批量删除镜像,批量删除 repository name中包含dirtest中的镜像
docker images | grep 'dirtest'| awk '{print $3}'|xargs docker rmi
一. 镜像命令
删除所有镜像
docker rmi `docker images -q`
三. dockerfile 配置
Dockerfile - vue项目
# 设置基础镜像 FROM nginx # 定义作者 MAINTAINER lihongyuan # 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面 COPY . /usr/share/nginx/html/
Dockerfile - net6项目
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base LABEL description="this is a test website" LABEL version="1.0" ENV TimeZone=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone WORKDIR /app COPY . . EXPOSE 80 ENTRYPOINT ["dotnet", "Rail.Frame.TestController.dll"]
Dockerfile - net6项目(带编译、发布)
方式1. 参考:https://baijiahao.baidu.com/s?id=1722457351925102595&wfr=spider&for=pc
方式2. 在项目上右键 - 添加 - docker支持,生成Dockerfile 文件
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
ENV TimeZone=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY ["Rail.Frame.TestController/Rail.Frame.TestController.csproj", "Rail.Frame.TestController/"] COPY ["Rail.Model.RailCDE/Rail.Model.RailCDE.csproj", "Rail.Model.RailCDE/"] COPY ["Rail.Model.Mate/Rail.Model.Mate.csproj", "Rail.Model.Mate/"] COPY ["Rail.Service.Assist/Rail.Service.Assist.csproj", "Rail.Service.Assist/"] COPY ["Rail.Model.DataBase/Rail.Model.DataBase.csproj", "Rail.Model.DataBase/"] COPY ["Rail.Model.Assist/Rail.Model.Assist.csproj", "Rail.Model.Assist/"] COPY ["Rail.Medium/Rail.Medium.csproj", "Rail.Medium/"] COPY ["Rail.Model.TestPlatform/Rail.Model.TestPlatform.csproj", "Rail.Model.TestPlatform/"] COPY ["Rail.Service.RailCDE/Rail.Service.RailCDE.csproj", "Rail.Service.RailCDE/"] COPY ["Rail.Service.TestPlatform/Rail.Service.TestPlatform.csproj", "Rail.Service.TestPlatform/"] COPY ["Rail.Service.Mate/Rail.Service.Mate.csproj", "Rail.Service.Mate/"] RUN dotnet restore "Rail.Frame.TestController/Rail.Frame.TestController.csproj" COPY . . WORKDIR "/src/Rail.Frame.TestController" RUN dotnet build "Rail.Frame.TestController.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "Rail.Frame.TestController.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Rail.Frame.TestController.dll"]
注意增加时区设置
ENV TimeZone=Asia/Shanghai
ENV /etc/localtime=/etc/localtime # 使用宿主机时间
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
导出Docker 镜像
docker save -o image.tar image:v11
批量强制删除容器
docker ps -a |grep 'Exited' |awk '{print $1}' |xargs docker rm -f
同理,删除none的镜像
docker images | grep none| awk '{print $3}'|xargs docker rmi