Docker 常用操作
Docker 常用命令
docker --version
查看当前安装版本
docker ps
查看正在运行的容器
docker images
查看本地运行的镜像
docker run
运行本地容器
docker pull
拉取远程镜像到本地
docker run -p 8123:80 -d --name myapi myapi
本地运行镜像
docker run -p 8123:80 -e AllowedHosts=192.168.1.1 -d --name myapi myapi
本地运行镜像 (设置环境变量)
docker build -t aspnetcoredocker .
生成本地镜像
docker stop 758f9c904bb8
停止正在运行的镜像
dcoker start 758f9c904bb8
开启已经启动过得镜像
docker researt 758f9c904bb8
重新启动正在运行的镜像
docker stop $(docker ps -a -q)
停止所有的container
docker rm $(docker ps -a -q)
删除全部container
docker rmi <image id>
删除单个镜像
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
删除none的镜像
docker rmi $(docker images -q)
删除所有的镜像
docker exec -it mysql bash
docker exec -it {cid} /bin/sh
进入到控制台模式
docker logs mysql
查看镜像日志
docker tag dockerweb springleee/dockerweb
TAG
docker volume create todo-db
创建 volume
docker volume inspect todo-db
查看 volume
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
映射 volume
.Net Core 镜像
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
COPY . .
EXPOSE 80
ENTRYPOINT ["dotnet", "dockerWeb.dll"]
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS publish
WORKDIR /src
COPY ./ DockerWeb2/
WORKDIR "/src/DockerWeb2"
RUN dotnet restore "DockerWeb2.csproj"
RUN dotnet publish "DockerWeb2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerWeb2.dll"]
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["DockerWebAPI/DockerWebAPI.csproj", "DockerWebAPI/"]
RUN dotnet restore "DockerWebAPI/DockerWebAPI.csproj"
COPY . .
WORKDIR "/src/DockerWebAPI"
RUN dotnet build "DockerWebAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerWebAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerWebAPI.dll"]
Docker Compose
version: '3.7'
services:
docker_webapi:
image: docker_webapi_1.0.0
build:
context: .
dockerfile: Dockerfile
environment:
- ASPNETCORE_DBCONN=mongodb://192.168.183.230:27089
- ASPNETCORE_DBNAME=dockerdb
ports:
- "8001:80"
docker-compose up -d
docker-compose down
Docker 运行Postgres
docker pull postgres
docker run --name=postgres -p 15432:5432 -e POSTGRES_PASSWORD=postgres -d postgres
-v /var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres -e TZ=Asia/Shanghai