Dock

Docker 学习

2022-03-24  本文已影响0人  Rinaloving

Docker

一、安装最新版Docker


# 如果已经安装了,就卸载
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# 安装yum-utils                  
yum install -y yum-utils

# 设置稳定的存储库
yum-config-manager --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装
yum install docker-ce docker-ce-cli containerd.io

# docker-compose

curl -L https://github.com/docker/compose/releases/download/1.28.5/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose --version

二、在发布后的文件夹中新建一个 Dockerfile 文件(没有后缀)

# 新建项目勾选docker部署的内容
#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/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

# 我们可以修改下,如下
FROM microsoft/dotnet:2.1-aspnetcore-runtime   //注意和你的版本要匹配
WORKDIR /app
COPY . .  //将当前目录下的所有文件(除了.dockerignore排除的路径),都拷贝进入 image 文件的/app目录。
EXPOSE 5002  //端口号(将容器 5000 端口暴露出来, 允许外部连接这个端口。)
//EXPOSE 443  //Https 端口开启
ENTRYPOINT ["dotnet", "DockerDemo5.dll"]  //运行的程序集 改成你自己的

# 把文件放到项目下(项目拷贝到linux上面)
# 创建 image文件 (-t参数用来指定 image 文件的名字,后面还可以用冒号指定标签 PS:注意最后的 点)
[root@localhost local]# cd XXFB/
[root@localhost XXFB]# docker build -t xxfbdocker2.1 .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost XXFB]# 
# 解决方法
[root@localhost XXFB]# rm -rf /var/lib/docker/
[root@localhost etc]# service docker start
Redirecting to /bin/systemctl start docker.service
[root@localhost etc]# docker build -t xxfbdocker2.1 .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /etc/Dockerfile: no such file or directory
[root@localhost etc]# cd /usr/local/XXFB/
[root@localhost XXFB]# docker build -t xxfbdocker2.1 .
Sending build context to Docker daemon  294.8MB
Error response from daemon: dockerfile parse error line 7: unknown instruction: //EXPOSE
[root@localhost XXFB]# vi Dockerfile 
[root@localhost XXFB]# docker build -t xxfbdocker2.1 .
Sending build context to Docker daemon  294.8MB
Step 1/5 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
2.1-stretch-slim: Pulling from dotnet/core/aspnet
33f99cea3b7d: Downloading  5.045MB/22.53MB
2b463c1ef5a9: Downloading  5.586MB/17.67MB
f66af433fc3e: Download complete 
06e05c6a34c1: Downloading   31
[root@localhost XXFB]# docker image ls
REPOSITORY                             TAG                IMAGE ID       CREATED         SIZE
xxfbdocker2.1                          latest             085e65772e65   3 minutes ago   548MB
mcr.microsoft.com/dotnet/core/aspnet   2.1-stretch-slim   1a48139335f8   7 months ago    253MB
[root@localhost XXFB]# 

# docker 删除镜像(先删除容器 container)
[root@localhost XXFB]# docker ps -a 
CONTAINER ID   IMAGE                  COMMAND               CREATED         STATUS                       PORTS     NAMES
9a510d8b244f   xxfbdocker2.1:latest   "dotnet WebApi.dll"   2 minutes ago   Exited (139) 2 minutes ago             xxfbdocker2.1
[root@localhost XXFB]# docker rm 9a510d8b244f
9a510d8b244f
[root@localhost XXFB]# 

三、创建镜像

# 项目发布后,将Dockerfile 文件拷贝到发布目录中,并将目录cd到部署目录,使用下面的命令构建镜像
# docker build -t <镜像名称> <目录> . 为当前目录
docker build -t dockernjdk .
[root@localhost NJDK]# docker build -t dockernjdk .
Sending build context to Docker daemon  294.8MB
Step 1/6 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
2.1-stretch-slim: Pulling from dotnet/core/aspnet
33f99cea3b7d: Pull complete 
2b463c1ef5a9: Pull complete 
f66af433fc3e: Pull complete 
06e05c6a34c1: Pull complete 
Digest: sha256:80ddf066edeeb7e489b23c3c90d8e5dd741037d4a4875150c1dec777fd24cc76
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim
 ---> 1a48139335f8
Step 2/6 : COPY . /xxfbdocker
 ---> 743f10b7782c
Step 3/6 : WORKDIR /xxfbdocker
 ---> Running in 4b218cad2746
Removing intermediate container 4b218cad2746
 ---> 077055218421
Step 4/6 : EXPOSE 80
 ---> Running in 3e59cd02a512
Removing intermediate container 3e59cd02a512
 ---> e98760c5701a
Step 5/6 : EXPOSE 443
 ---> Running in e8c5963d887c
Removing intermediate container e8c5963d887c
 ---> 95a4101aadd1
Step 6/6 : ENTRYPOINT ["dotnet", "WebApi.dll"]
 ---> Running in 34e734e3ed11
Removing intermediate container 34e734e3ed11
 ---> 6650bd7b294a
Successfully built 6650bd7b294a
Successfully tagged dockernjdk:latest
[root@localhost NJDK]# 

# 上述步骤完成后,查看镜像是否创建成功
[root@localhost NJDK]# docker images
REPOSITORY                             TAG                IMAGE ID       CREATED         SIZE
dockernjdk                             latest             6650bd7b294a   4 minutes ago   548MB
dockerdemo                             latest             a42c8ba3496d   23 hours ago    212MB
mcr.microsoft.com/dotnet/aspnet        3.1                f0220ee3f874   5 days ago      208MB
mcr.microsoft.com/dotnet/core/aspnet   2.1-stretch-slim   1a48139335f8   7 months ago    253MB
[root@localhost NJDK]# 

# 运行镜像
镜像构建完成后,使用下面命令来运行镜像容器
# docker run --name=<容器名称> -d<后台运行> -p <外部暴露端口>:<容器端口><关键镜像>
docker run --name=dockernjdk -d -p 5004:80 dockernjdk:latest

[root@localhost NJDK]# docker run --name=dockernjdk -d -p 5004:80 dockernjdk:latest
d5cef486db6c0ab19e8be0d6351cc85b30edbe86b8fcf37618fd0175a2785752
[root@localhost NJDK]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS          PORTS                                            NAMES
d5cef486db6c   dockernjdk:latest   "dotnet WebApi.dll"      8 seconds ago   Up 6 seconds    443/tcp, 0.0.0.0:5004->80/tcp, :::5004->80/tcp   dockernjdk
e91d88e285c7   dockerdemo:latest   "dotnet DockerDemo.d…"   23 hours ago    Up 15 minutes   443/tcp, 0.0.0.0:5003->80/tcp, :::5003->80/tcp   dockerdemo
[root@localhost NJDK]# 

四、启动docker 镜像


[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS                    PORTS     NAMES
e91d88e285c7   dockerdemo:latest   "dotnet DockerDemo.d…"   23 hours ago   Exited (0) 23 hours ago             dockerdemo
[root@localhost ~]# docker start e91d88e285c7
e91d88e285c7
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS         PORTS                                            NAMES
e91d88e285c7   dockerdemo:latest   "dotnet DockerDemo.d…"   23 hours ago   Up 4 seconds   443/tcp, 0.0.0.0:5003->80/tcp, :::5003->80/tcp   dockerdemo
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS          PORTS                                            NAMES
e91d88e285c7   dockerdemo:latest   "dotnet DockerDemo.d…"   23 hours ago   Up 47 seconds   443/tcp, 0.0.0.0:5003->80/tcp, :::5003->80/tcp   dockerdemo
[root@localhost ~]# 




五、导出导入docker镜像

# 说明:
# 镜像打包:
# 1. 镜像打包:
docker save -o tomcat7.tar mytomcat 
# 2. 将打包的镜像伤处啊难道其他服务器
scp tomcat7.tar 其它服务器ip:/root
# 3. 导入镜像
docker load -i /root/tomcat7.tar
# 容器打包:
# 1. 容器打包:
docker export -o /root/t1.tar t1
# 2. 容器导入
docker import t1.tar mytomcat:latest



[root@localhost NJDK]# docker export -o dockernjdk-test.tar dockernjdk
[root@localhost NJDK]# ls
AngleSharp.Css.dll                  dockernjdk-test.tar                                Microsoft.Extensions.Caching.Memory.dll  Spire.License.xml                              System.Data.DataSetExtensions.dll
AngleSharp.dll                      dotnet-aspnet-codegenerator-design.dll             Microsoft.Win32.Registry.dll             Spire.Pdf.dll                                  System.Data.SQLite.dll
appsettings.Development.json        EPPlus.Core.dll                                    Microsoft.Win32.SystemEvents.dll         Spire.Pdf.xml                                  System.Drawing.Common.dll
appsettings.json                    ExcelHelper.dll                                    MySql.Data.dll                           Spire.Presentation.dll                         System.IO.Pipelines.dll
com.uplus.common.dll                ExcelHelper.pdb                                    NodaTime.dll                             Spire.Presentation.xml                         System.Management.dll
com.uplus.common.pdb                Google.Protobuf.dll                                Npgsql.dll                               Spire.XLS.dll                                  System.Security.AccessControl.dll
com.uplus.data.dll                  Hei.Captcha.dll                                    Oracle.ManagedDataAccess.dll             Spire.XLS.xml                                  System.Security.Cryptography.ProtectedData.dll
com.uplus.data.pdb                  HtmlSanitizer.dll                                  qcloudsms_csharp.dll                     SQLitePCLRaw.batteries_green.dll               System.Security.Principal.Windows.dll
com.uplus.log4net.dll               ICSharpCode.SharpZipLib.dll                        qcloudsms.dll                            SQLitePCLRaw.batteries_v2.dll                  System.Xml.XPath.XmlDocument.dll
com.uplus.log4net.pdb               Independentsoft.Office.dll                         RabbitMQ.Client.dll                      SQLitePCLRaw.core.dll                          WebApi.deps.json
com.uplus.rabbitmqhelper.dll        libman.json                                        runtimes                                 SQLitePCLRaw.provider.e_sqlite3.dll            WebApi.dll
com.uplus.rabbitmqhelper.pdb        Log4Net.config                                     SixLabors.Core.dll                       SqlSugar.dll                                   WebApi.pdb
com.uplus.snowleopardframework.dll  log4net.dll                                        SixLabors.Fonts.dll                      SuperSocket.ClientEngine.dll                   WebApi.runtimeconfig.json
com.uplus.snowleopardframework.pdb  Magick.NET.Core.dll                                SixLabors.ImageSharp.dll                 Swashbuckle.AspNetCore.dll                     web.config
com.uplus.spirecore.dll             Magick.NET-Q16-AnyCPU.dll                          SixLabors.ImageSharp.Drawing.dll         Swashbuckle.AspNetCore.Swagger.dll             WebSocket4Net.dll
com.uplus.spirecore.pdb             Microsoft.AspNetCore.Authentication.dll            SixLabors.Shapes.dll                     Swashbuckle.AspNetCore.SwaggerGen.dll          wwwroot
com.uplus.taskschedule.dll          Microsoft.AspNetCore.Authentication.JwtBearer.dll  Spire.Doc.dll                            Swashbuckle.AspNetCore.SwaggerUI.dll
com.uplus.taskschedule.pdb          Microsoft.Data.Sqlite.dll                          Spire.Doc.xml                            System.CodeDom.dll
Dockerfile                          Microsoft.Extensions.Caching.Abstractions.dll      Spire.License.dll                        System.Configuration.ConfigurationManager.dll
[root@localhost NJDK]# rz
-bash: rz: command not found
[root@localhost NJDK]# sz
-bash: sz: command not found
[root@localhost NJDK]# yum install -y lrzsz
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: ftp.sjtu.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
base                                                                                                                                                                                                        | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                                                            | 3.5 kB  00:00:00     
extras                                                                                                                                                                                                      | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                     | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package lrzsz.x86_64 0:0.12.20-36.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================================== Package                                             Arch                                                 Version                                                         Repository                                          Size
===================================================================================================================================================================================================================================Installing:
 lrzsz                                               x86_64                                               0.12.20-36.el7                                                  base                                                78 k

Transaction Summary
===================================================================================================================================================================================================================================Install  1 Package

Total download size: 78 k
Installed size: 181 k
Downloading packages:
lrzsz-0.12.20-36.el7.x86_64.rpm                                                                                                                                                                             |  78 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : lrzsz-0.12.20-36.el7.x86_64                                                                                                                                                                                     1/1 
  Verifying  : lrzsz-0.12.20-36.el7.x86_64                                                                                                                                                                                     1/1 

Installed:
  lrzsz.x86_64 0:0.12.20-36.el7                                                                                                                                                                                                    

Complete!
[root@localhost NJDK]# sz
sz: need at least one file to send
Try `sz --help' for more information.
[root@localhost NJDK]# sz dockernjdk-test.tar 
 ZMODEM  Session started            
------------------------            
 Received  dockernjdk-test.tar            
[root@localhost NJDK]# docker save -o dockernjdk-save.tar dockernjdk:latest
[root@localhost NJDK]# ls
AngleSharp.Css.dll                  dockernjdk-save.tar                                Microsoft.Extensions.Caching.Abstractions.dll  Spire.License.dll                      System.Configuration.ConfigurationManager.dll
AngleSharp.dll                      dockernjdk-test.tar                                Microsoft.Extensions.Caching.Memory.dll        Spire.License.xml                      System.Data.DataSetExtensions.dll
appsettings.Development.json        dotnet-aspnet-codegenerator-design.dll             Microsoft.Win32.Registry.dll                   Spire.Pdf.dll                          System.Data.SQLite.dll
appsettings.json                    EPPlus.Core.dll                                    Microsoft.Win32.SystemEvents.dll               Spire.Pdf.xml                          System.Drawing.Common.dll
com.uplus.common.dll                ExcelHelper.dll                                    MySql.Data.dll                                 Spire.Presentation.dll                 System.IO.Pipelines.dll
com.uplus.common.pdb                ExcelHelper.pdb                                    NodaTime.dll                                   Spire.Presentation.xml                 System.Management.dll
com.uplus.data.dll                  Google.Protobuf.dll                                Npgsql.dll                                     Spire.XLS.dll                          System.Security.AccessControl.dll
com.uplus.data.pdb                  Hei.Captcha.dll                                    Oracle.ManagedDataAccess.dll                   Spire.XLS.xml                          System.Security.Cryptography.ProtectedData.dll
com.uplus.log4net.dll               HtmlSanitizer.dll                                  qcloudsms_csharp.dll                           SQLitePCLRaw.batteries_green.dll       System.Security.Principal.Windows.dll
com.uplus.log4net.pdb               ICSharpCode.SharpZipLib.dll                        qcloudsms.dll                                  SQLitePCLRaw.batteries_v2.dll          System.Xml.XPath.XmlDocument.dll
com.uplus.rabbitmqhelper.dll        Independentsoft.Office.dll                         RabbitMQ.Client.dll                            SQLitePCLRaw.core.dll                  WebApi.deps.json
com.uplus.rabbitmqhelper.pdb        libman.json                                        runtimes                                       SQLitePCLRaw.provider.e_sqlite3.dll    WebApi.dll
com.uplus.snowleopardframework.dll  Log4Net.config                                     SixLabors.Core.dll                             SqlSugar.dll                           WebApi.pdb
com.uplus.snowleopardframework.pdb  log4net.dll                                        SixLabors.Fonts.dll                            SuperSocket.ClientEngine.dll           WebApi.runtimeconfig.json
com.uplus.spirecore.dll             Magick.NET.Core.dll                                SixLabors.ImageSharp.dll                       Swashbuckle.AspNetCore.dll             web.config
com.uplus.spirecore.pdb             Magick.NET-Q16-AnyCPU.dll                          SixLabors.ImageSharp.Drawing.dll               Swashbuckle.AspNetCore.Swagger.dll     WebSocket4Net.dll
com.uplus.taskschedule.dll          Microsoft.AspNetCore.Authentication.dll            SixLabors.Shapes.dll                           Swashbuckle.AspNetCore.SwaggerGen.dll  wwwroot
com.uplus.taskschedule.pdb          Microsoft.AspNetCore.Authentication.JwtBearer.dll  Spire.Doc.dll                                  Swashbuckle.AspNetCore.SwaggerUI.dll
Dockerfile                          Microsoft.Data.Sqlite.dll                          Spire.Doc.xml                                  System.CodeDom.dll
[root@localhost NJDK]# sz dockernjdk-save.tar 
 ZMODEM  Session started            
------------------------            
  57%  dockernjdk-save.tar               
上一篇下一篇

猜你喜欢

热点阅读