Samouczek: Konteneryzowanie aplikacji .NET CoreTutorial: Containerize a .NET Core app
W tym samouczku dowiesz się, jak konteneryzowanie aplikację .NET Core z platformą Docker.In this tutorial, you'll learn how to containerize a .NET Core application with Docker. Kontenery mają wiele funkcji i korzyści, takie jak niezmienne infrastruktury, zapewniające przenośną architekturę i umożliwiające skalowalność.Containers have many features and benefits, such as being an immutable infrastructure, providing a portable architecture, and enabling scalability. Obraz może służyć do tworzenia kontenerów dla lokalnego środowiska projektowego, chmury prywatnej lub chmury publicznej.The image can be used to create containers for your local development environment, private cloud, or public cloud.
W tym samouczku zostały wykonane następujące czynności:In this tutorial, you:
- Tworzenie i publikowanie prostej aplikacji .NET CoreCreate and publish a simple .NET Core app
- Tworzenie i konfigurowanie pliku dockerfile dla platformy .NET CoreCreate and configure a Dockerfile for .NET Core
- Kompilowanie obrazu platformy DockerBuild a Docker image
- Tworzenie i uruchamianie kontenera platformy DockerCreate and run a Docker container
Zapoznaj się z tematem tworzenie i wdrażanie kontenerów platformy Docker dla aplikacji platformy .NET Core.You'll understand the Docker container build and deploy tasks for a .NET Core application. Platforma Docker korzysta z aparatu platformy Docker , aby szybko kompilować i spakować aplikacje jako obrazy platformy Docker.The Docker platform uses the Docker engine to quickly build and package apps as Docker images. Te obrazy są zapisywane w formacie pliku dockerfile , który ma zostać wdrożony i uruchomiony w kontenerze warstwowym.These images are written in the Dockerfile format to be deployed and run in a layered container.
Uwaga
Ten samouczek nie dotyczy aplikacji ASP.NET Core.This tutorial is not for ASP.NET Core apps. Jeśli używasz ASP.NET Core, zapoznaj się z samouczkiem uczenie się, jak konteneryzowanie aplikacji ASP.NET Core .If you're using ASP.NET Core, see the Learn how to containerize an ASP.NET Core application tutorial.
Wymagania wstępnePrerequisites
Zainstaluj następujące wymagania wstępne:Install the following prerequisites:
- Zestaw SDK platformy .NET Core 5,0.NET Core 5.0 SDK
Jeśli masz zainstalowany program .NET Core, użyjdotnet --info
polecenia, aby określić, którego zestawu SDK używasz.If you have .NET Core installed, use thedotnet --info
command to determine which SDK you're using. - Platforma Docker Community EditionDocker Community Edition
- Tymczasowy folder roboczy dla przykładowej aplikacji pliku dockerfile i .NET Core.A temporary working folder for the Dockerfile and .NET Core example app. W tym samouczku Nazwa programu Docker Work jest używana jako folder roboczy.In this tutorial, the name docker-working is used as the working folder.
Tworzenie aplikacji .NET CoreCreate .NET Core app
Potrzebna jest aplikacja .NET Core, którą zostanie uruchomiony kontener platformy Docker.You need a .NET Core app that the Docker container will run. Otwórz Terminal, Utwórz folder roboczy, jeśli jeszcze tego nie zrobiono, a następnie wprowadź go.Open your terminal, create a working folder if you haven't already, and enter it. W folderze roboczym Uruchom następujące polecenie, aby utworzyć nowy projekt w podkatalogu o nazwie App:In the working folder, run the following command to create a new project in a subdirectory named app:
dotnet new console -o App -n NetCore.Docker
Drzewo folderów będzie wyglądać następująco:Your folder tree will look like the following:
docker-working
└──App
├──NetCore.Docker.csproj
├──Program.cs
└──obj
├──NetCore.Docker.csproj.nuget.dgspec.json
├──NetCore.Docker.csproj.nuget.g.props
├──NetCore.Docker.csproj.nuget.g.targets
├──project.assets.json
└──project.nuget.cache
dotnet new
Polecenie tworzy nowy folder o nazwie App i generuje aplikację konsolową "Hello World".The dotnet new
command creates a new folder named App and generates a "Hello World" console application. Zmień katalogi i przejdź do folderu aplikacji z sesji terminala.Change directories and navigate into the App folder, from your terminal session. Użyj dotnet run
polecenia, aby uruchomić aplikację.Use the dotnet run
command to start the app. Aplikacja zostanie uruchomiona i wydrukowana Hello World!
poniżej polecenia:The application will run, and print Hello World!
below the command:
dotnet run
Hello World!
Szablon domyślny tworzy aplikację, która drukuje do terminalu, a następnie natychmiast kończy pracę.The default template creates an app that prints to the terminal and then immediately terminates. Na potrzeby tego samouczka będziesz używać aplikacji, która nieskończonie pętli.For this tutorial, you'll use an app that loops indefinitely. Otwórz plik program.cs w edytorze tekstu.Open the Program.cs file in a text editor.
Porada
Jeśli używasz Visual Studio Code, w poprzedniej sesji terminala wpisz następujące polecenie:If you're using Visual Studio Code, from the previous terminal session type the following command:
code .
Spowoduje to otwarcie folderu aplikacji zawierającego projekt w Visual Studio Code.This will open the App folder that contains the project in Visual Studio Code.
Program.cs powinien wyglądać podobnie do następującego kodu w języku C#:The Program.cs should look like the following C# code:
using System;
namespace NetCore.Docker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Zastąp plik następującym kodem, który zlicza liczby w każdej sekundzie:Replace the file with the following code that counts numbers every second:
using System;
using System.Threading.Tasks;
namespace NetCore.Docker
{
class Program
{
static async Task Main(string[] args)
{
var counter = 0;
var max = args.Length != 0 ? Convert.ToInt32(args[0]) : -1;
while (max == -1 || counter < max)
{
Console.WriteLine($"Counter: {++counter}");
await Task.Delay(1000);
}
}
}
}
Zapisz plik i ponownie przetestuj program przy użyciu programu dotnet run
.Save the file and test the program again with dotnet run
. Należy pamiętać, że ta aplikacja jest uruchamiana w nieskończoność.Remember that this app runs indefinitely. Aby zatrzymać, użyj polecenia Cancel Ctrl + C .Use the cancel command Ctrl+C to stop it. Oto przykładowe dane wyjściowe:The following is an example output:
dotnet run
Counter: 1
Counter: 2
Counter: 3
Counter: 4
^C
Jeśli przekażesz liczbę w wierszu polecenia do aplikacji, będzie ona liczyć tylko do tej wartości, a następnie zostanie zakończona.If you pass a number on the command line to the app, it will only count up to that amount and then exit. Wypróbuj go, dotnet run -- 5
Aby liczyć do pięciu.Try it with dotnet run -- 5
to count to five.
Ważne
Wszystkie parametry po --
nie są przenoszone do dotnet run
polecenia i zamiast tego są przesyłane do aplikacji.Any parameters after --
are not passed to the dotnet run
command and instead are passed to your application.
Publikowanie aplikacji platformy .NET CorePublish .NET Core app
Przed dodaniem aplikacji .NET Core do obrazu platformy Docker należy najpierw ją opublikować.Before adding the .NET Core app to the Docker image, first it must be published. Najlepszym rozwiązaniem jest uruchomienie przez kontener opublikowanej wersji aplikacji.It is best to have the container run the published version of the app. Aby opublikować aplikację, uruchom następujące polecenie:To publish the app, run the following command:
dotnet publish -c Release
To polecenie kompiluje aplikację do folderu Publikowanie .This command compiles your app to the publish folder. Ścieżka do folderu publikowania z folderu roboczego powinna być .\App\bin\Release\net5.0\publish\
The path to the publish folder from the working folder should be .\App\bin\Release\net5.0\publish\
W folderze App (aplikacja ) Pobierz listę katalogów folderu publikowania, aby sprawdzić, czy plik NetCore.Docker.dll został utworzony.From the App folder, get a directory listing of the publish folder to verify that the NetCore.Docker.dll file was created.
dir .\bin\Release\net5.0\publish\
Directory: C:\Users\dapine\App\bin\Release\net5.0\publish
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 4/27/2020 8:27 AM 434 NetCore.Docker.deps.json
-a---- 4/27/2020 8:27 AM 6144 NetCore.Docker.dll
-a---- 4/27/2020 8:27 AM 171520 NetCore.Docker.exe
-a---- 4/27/2020 8:27 AM 860 NetCore.Docker.pdb
-a---- 4/27/2020 8:27 AM 154 NetCore.Docker.runtimeconfig.json
Tworzenie pliku dockerfileCreate the Dockerfile
Plik pliku dockerfile jest używany przez docker build
polecenie do tworzenia obrazu kontenera.The Dockerfile file is used by the docker build
command to create a container image. Ten plik jest plikiem tekstowym o nazwie pliku dockerfile , który nie ma rozszerzenia.This file is a text file named Dockerfile that doesn't have an extension.
Utwórz plik o nazwie pliku dockerfile w katalogu zawierającym element . csproj i otwórz go w edytorze tekstu.Create a file named Dockerfile in directory containing the .csproj and open it in a text editor. W tym samouczku zostanie użyty obraz środowiska uruchomieniowego ASP.NET Core (zawierający obraz środowiska uruchomieniowego programu .NET Core), który odpowiada aplikacji konsolowej programu .NET Core.This tutorial will use the ASP.NET Core runtime image (which contains the .NET Core runtime image) and corresponds with the .NET Core console application.
FROM mcr.microsoft.com/dotnet/aspnet:5.0
Uwaga
Obraz środowiska uruchomieniowego ASP.NET Core jest używany w sposób celowy, chociaż mcr.microsoft.com/dotnet/runtime:5.0
można było użyć obrazu.The ASP.NET Core runtime image is used intentionally here, although the mcr.microsoft.com/dotnet/runtime:5.0
image could have been used.
FROM
Słowo kluczowe wymaga w pełni kwalifikowanej nazwy obrazu kontenera Docker.The FROM
keyword requires a fully qualified Docker container image name. Microsoft Container Registry (MCR, mcr.microsoft.com) to zespolone centrum Docker, które hostuje dostępne publicznie kontenery.The Microsoft Container Registry (MCR, mcr.microsoft.com) is a syndicate of Docker Hub - which hosts publicly accessible containers. dotnet/core
Segment jest repozytorium kontenera, gdzie jako aspnet
segment jest nazwą obrazu kontenera.The dotnet/core
segment is the container repository, where as the aspnet
segment is the container image name. Obraz jest otagowany przy użyciu 5.0
, który jest używany do przechowywania wersji.The image is tagged with 5.0
, which is used for versioning. W tym przypadku mcr.microsoft.com/dotnet/aspnet:5.0
środowisko uruchomieniowe programu .NET Core 5,0.Thus, mcr.microsoft.com/dotnet/aspnet:5.0
is the .NET Core 5.0 runtime. Upewnij się, że korzystasz z wersji środowiska uruchomieniowego, która pasuje do środowiska uruchomieniowego wskazywanego przez zestaw SDK.Make sure that you pull the runtime version that matches the runtime targeted by your SDK. Na przykład aplikacja utworzona w poprzedniej sekcji używa zestawu .NET Core 5,0 SDK i obrazu podstawowego, do którego odwołuje się w pliku dockerfile , jest oznaczona jako 5,0.For example, the app created in the previous section used the .NET Core 5.0 SDK and the base image referred to in the Dockerfile is tagged with 5.0.
Zapisz plik pliku dockerfile .Save the Dockerfile file. Struktura katalogów folderu roboczego powinna wyglądać następująco.The directory structure of the working folder should look like the following. Niektóre pliki i foldery bardziej szczegółowe zostały pominięte w celu zaoszczędzenia miejsca w artykule:Some of the deeper-level files and folders have been omitted to save space in the article:
docker-working
└──App
├──Dockerfile
├──NetCore.Docker.csproj
├──Program.cs
├──bin
│ └──Release
│ └──net5.0
│ └──publish
│ ├──NetCore.Docker.deps.json
│ ├──NetCore.Docker.exe
│ ├──NetCore.Docker.dll
│ ├──NetCore.Docker.pdb
│ └──NetCore.Docker.runtimeconfig.json
└──obj
└──...
W terminalu uruchom następujące polecenie:From your terminal, run the following command:
docker build -t counter-image -f Dockerfile .
Platforma Docker będzie przetwarzać każdy wiersz w pliku dockerfile.Docker will process each line in the Dockerfile. .
W docker build
poleceniu polecenie instruuje platformę Docker, aby użyć bieżącego folderu do znalezienia pliku dockerfile.The .
in the docker build
command tells Docker to use the current folder to find a Dockerfile. To polecenie kompiluje obraz i tworzy lokalne repozytorium o nazwie Counter-Image , które wskazuje na ten obraz.This command builds the image and creates a local repository named counter-image that points to that image. Po zakończeniu tego polecenia Uruchom polecenie, docker images
Aby wyświetlić listę zainstalowanych obrazów:After this command finishes, run docker images
to see a list of images installed:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
counter-image latest e6780479db63 4 days ago 190MB
mcr.microsoft.com/dotnet/aspnet 5.0 e6780479db63 4 days ago 190MB
Zwróć uwagę, że te dwa obrazy mają tę samą wartość identyfikatora obrazu .Notice that the two images share the same IMAGE ID value. Wartość jest taka sama między obu obrazów, ponieważ jedyne polecenie w pliku dockerfile było podstawą nowego obrazu na istniejącym obrazie.The value is the same between both images because the only command in the Dockerfile was to base the new image on an existing image. Dodajmy trzy polecenia do pliku dockerfile.Let's add three commands to the Dockerfile. Każde polecenie tworzy nową warstwę obrazu z końcowym poleceniem reprezentującym punkty wejścia repozytorium obrazu licznika do.Each command creates a new image layer with the final command representing the counter-image repository entry points to.
COPY bin/Release/net5.0/publish/ App/
WORKDIR /App
ENTRYPOINT ["dotnet", "NetCore.Docker.dll"]
COPY
Polecenie instruuje platformę Docker, aby skopiował określony folder na komputerze do folderu w kontenerze.The COPY
command tells Docker to copy the specified folder on your computer to a folder in the container. W tym przykładzie folder publikacji jest kopiowany do folderu o nazwie aplikacja w kontenerze.In this example, the publish folder is copied to a folder named App in the container.
WORKDIR
Polecenie zmienia bieżący katalog wewnątrz kontenera na aplikację.The WORKDIR
command changes the current directory inside of the container to App.
Następne polecenie, ENTRYPOINT
, instruuje platformę Docker, aby skonfigurować kontener do uruchamiania jako plik wykonywalny.The next command, ENTRYPOINT
, tells Docker to configure the container to run as an executable. Po uruchomieniu kontenera ENTRYPOINT
polecenie zostanie uruchomione.When the container starts, the ENTRYPOINT
command runs. Po zakończeniu tego polecenia kontener zostanie automatycznie zatrzymany.When this command ends, the container will automatically stop.
W terminalu uruchom polecenie docker build -t counter-image -f Dockerfile .
i po zakończeniu wykonywania polecenia docker images
.From your terminal, run docker build -t counter-image -f Dockerfile .
and when that command finishes, run docker images
.
docker build -t counter-image -f Dockerfile .
Sending build context to Docker daemon 1.117MB
Step 1/4 : FROM mcr.microsoft.com/dotnet/aspnet:5.0
---> e6780479db63
Step 2/4 : COPY bin/Release/net5.0/publish/ App/
---> d1732740eed2
Step 3/4 : WORKDIR /App
---> Running in b1701a42f3ff
Removing intermediate container b1701a42f3ff
---> 919aab5b95e3
Step 4/4 : ENTRYPOINT ["dotnet", "NetCore.Docker.dll"]
---> Running in c12aebd26ced
Removing intermediate container c12aebd26ced
---> cd11c3df9b19
Successfully built cd11c3df9b19
Successfully tagged counter-image:latest
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
counter-image latest cd11c3df9b19 41 seconds ago 190MB
mcr.microsoft.com/dotnet/aspnet 5.0 e6780479db63 4 days ago 190MB
Każde polecenie w pliku dockerfile wygenerowało warstwę i utworzyła Identyfikator obrazu.Each command in the Dockerfile generated a layer and created an IMAGE ID. Końcowy Identyfikator obrazu (będzie inny) to cd11c3df9b19 , a następnie utworzysz kontener oparty na tym obrazie.The final IMAGE ID (yours will be different) is cd11c3df9b19 and next you'll create a container based on this image.
Tworzenie konteneraCreate a container
Teraz, gdy masz obraz zawierający aplikację, możesz utworzyć kontener.Now that you have an image that contains your app, you can create a container. Kontener można utworzyć na dwa sposoby.You can create a container in two ways. Najpierw utwórz nowy kontener, który został zatrzymany.First, create a new container that is stopped.
docker create --name core-counter counter-image
0f281cb3af994fba5d962cc7d482828484ea14ead6bfe386a35e5088c0058851
docker create
Powyższe polecenie spowoduje utworzenie kontenera na podstawie obrazu licznika .The docker create
command from above will create a container based on the counter-image image. Dane wyjściowe tego polecenia pokazują, że Identyfikator kontenera (zostanie inaczej) utworzonego kontenera.The output of that command shows you the CONTAINER ID (yours will be different) of the created container. Aby wyświetlić listę wszystkich kontenerów, użyj docker ps -a
polecenia:To see a list of all containers, use the docker ps -a
command:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f281cb3af99 counter-image "dotnet NetCore.Dock…" 40 seconds ago Created core-counter
Zarządzanie konteneremManage the container
Kontener został utworzony z określoną nazwą core-counter
. Ta nazwa jest używana do zarządzania kontenerem.The container was created with a specific name core-counter
, this name is used to manage the container. Poniższy przykład używa docker start
polecenia do uruchomienia kontenera, a następnie używa docker ps
polecenia do wyświetlania tylko kontenerów, które są uruchomione:The following example uses the docker start
command to start the container, and then uses the docker ps
command to only show containers that are running:
docker start core-counter
core-counter
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f6424a7ddce counter-image "dotnet NetCore.Dock…" 2 minutes ago Up 11 seconds core-counter
Podobnie docker stop
polecenie spowoduje zatrzymanie kontenera.Similarly, the docker stop
command will stop the container. Poniższy przykład używa docker stop
polecenia do zatrzymania kontenera, a następnie używa docker ps
polecenia, aby pokazać, że żaden kontener nie jest uruchomiony:The following example uses the docker stop
command to stop the container, and then uses the docker ps
command to show that no containers are running:
docker stop core-counter
core-counter
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Nawiązywanie połączenia z konteneremConnect to a container
Po uruchomieniu kontenera można nawiązać z nim połączenie, aby wyświetlić dane wyjściowe.After a container is running, you can connect to it to see the output. Użyj docker start
poleceń i, docker attach
Aby uruchomić kontener i wgląd do strumienia wyjściowego.Use the docker start
and docker attach
commands to start the container and peek at the output stream. W tym przykładzie naciśnięcie klawiszy Ctrl + C jest używane do odłączenia od uruchomionego kontenera.In this example, the Ctrl+C keystroke is used to detach from the running container. Naciśnięcie klawisza spowoduje zakończenie procesu w kontenerze, o ile nie określono inaczej, co spowodowałoby zatrzymanie kontenera.This keystroke will end the process in the container unless otherwise specified, which would stop the container. --sig-proxy=false
Parametr zapewnia, że naciśnięcie klawiszy CTRL + C nie spowoduje zatrzymania procesu w kontenerze.The --sig-proxy=false
parameter ensures that Ctrl+C will not stop the process in the container.
Po odłączeniu od kontenera ponownie Dołącz, aby upewnić się, że nadal działa i zlicza.After you detach from the container, reattach to verify that it's still running and counting.
docker start core-counter
core-counter
docker attach --sig-proxy=false core-counter
Counter: 7
Counter: 8
Counter: 9
^C
docker attach --sig-proxy=false core-counter
Counter: 17
Counter: 18
Counter: 19
^C
Usuwanie konteneraDelete a container
Na potrzeby tego artykułu nie ma potrzeby, aby kontenery zostały jedynie obsunięte.For the purposes of this article you don't want containers just hanging around doing nothing. Usuń kontener, który został wcześniej utworzony.Delete the container you previously created. Jeśli kontener jest uruchomiony, Zatrzymaj go.If the container is running, stop it.
docker stop core-counter
Poniższy przykład wyświetla listę wszystkich kontenerów.The following example lists all containers. Następnie używa docker rm
polecenia do usuwania kontenera, a następnie sprawdza drugi czas dla wszystkich uruchomionych kontenerów.It then uses the docker rm
command to delete the container, and then checks a second time for any running containers.
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f6424a7ddce counter-image "dotnet NetCore.Dock…" 7 minutes ago Exited (143) 20 seconds ago core-counter
docker rm core-counter
core-counter
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Pojedynczy przebiegSingle run
Docker udostępnia docker run
polecenie do tworzenia i uruchamiania kontenera jako pojedyncze polecenie.Docker provides the docker run
command to create and run the container as a single command. To polecenie eliminuje konieczność uruchomienia docker create
, a następnie docker start
.This command eliminates the need to run docker create
and then docker start
. Możesz również ustawić to polecenie, aby automatycznie usuwać kontener po zatrzymaniu kontenera.You can also set this command to automatically delete the container when the container stops. Na przykład użyj docker run -it --rm
do dwóch rzeczy, najpierw automatycznie Użyj bieżącego terminalu do łączenia się z kontenerem, a następnie usuń go:For example, use docker run -it --rm
to do two things, first, automatically use the current terminal to connect to the container, and then when the container finishes, remove it:
docker run -it --rm counter-image
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
^C
Kontener przekazuje również parametry do wykonywania aplikacji .NET Core.The container also passes parameters into the execution of the .NET Core app. Aby nakazać aplikacji .NET Core zliczanie tylko do 3 przebiegów 3.To instruct the .NET Core app to count only to 3 pass in 3.
docker run -it --rm counter-image 3
Counter: 1
Counter: 2
Counter: 3
W programie docker run -it
naciśnięcie polecenia CTRL + C spowoduje zatrzymanie procesu, który jest uruchomiony w kontenerze, co z kolei powoduje zatrzymanie kontenera.With docker run -it
, the Ctrl+C command will stop process that is running in the container, which in turn, stops the container. Ponieważ --rm
parametr został dostarczony, kontener jest automatycznie usuwany, gdy proces zostanie zatrzymany.Since the --rm
parameter was provided, the container is automatically deleted when the process is stopped. Sprawdź, czy nie istnieje:Verify that it doesn't exist:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Zmień punkt wejściaChange the ENTRYPOINT
docker run
Polecenie umożliwia również modyfikowanie ENTRYPOINT
polecenia z pliku dockerfile i uruchamianie czegoś innego, ale tylko dla tego kontenera.The docker run
command also lets you modify the ENTRYPOINT
command from the Dockerfile and run something else, but only for that container. Na przykład użyj poniższego polecenia, aby uruchomić polecenie bash
lub cmd.exe
.For example, use the following command to run bash
or cmd.exe
. W razie potrzeby zmodyfikuj polecenie.Edit the command as necessary.
W tym przykładzie ENTRYPOINT
został zmieniony na cmd.exe
.In this example, ENTRYPOINT
is changed to cmd.exe
. Ctrl + C jest wciśnięty, aby zakończyć proces i zatrzymać kontener.Ctrl+C is pressed to end the process and stop the container.
docker run -it --rm --entrypoint "cmd.exe" counter-image
Microsoft Windows [Version 10.0.17763.379]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\>dir
Volume in drive C has no label.
Volume Serial Number is 3005-1E84
Directory of C:\
04/09/2019 08:46 AM <DIR> app
03/07/2019 10:25 AM 5,510 License.txt
04/02/2019 01:35 PM <DIR> Program Files
04/09/2019 01:06 PM <DIR> Users
04/02/2019 01:35 PM <DIR> Windows
1 File(s) 5,510 bytes
4 Dir(s) 21,246,517,248 bytes free
C:\>^C
Najważniejsze poleceniaEssential commands
Platforma Docker ma wiele różnych poleceń, które tworzą i obsługują kontenery i obrazy oraz zarządzają nimi.Docker has many different commands that create, manage, and interact with containers and images. Te polecenia platformy Docker mają kluczowe znaczenie dla zarządzania kontenerami:These Docker commands are essential to managing your containers:
- docker builddocker build
- uruchomienie platformy Dockerdocker run
- docker psdocker ps
- zatrzymanie platformy Dockerdocker stop
- Platforma Docker RMdocker rm
- Docker RMIdocker rmi
- obraz platformy Dockerdocker image
Czyszczenie zasobówClean up resources
W tym samouczku utworzono kontenery i obrazy.During this tutorial, you created containers and images. Jeśli chcesz, usuń te zasoby.If you want, delete these resources. Użyj następujących poleceń, abyUse the following commands to
Wyświetl listę wszystkich kontenerówList all containers
docker ps -a
Zatrzymaj kontenery uruchomione przez ich nazwę.Stop containers that are running by their name.
docker stop counter-image
Usuwanie konteneraDelete the container
docker rm counter-image
Następnie usuń wszystkie obrazy, które nie są już potrzebne na komputerze.Next, delete any images that you no longer want on your machine. Usuń obraz utworzony przez pliku dockerfile , a następnie Usuń obraz platformy .NET Core, na którym oparto pliku dockerfile .Delete the image created by your Dockerfile and then delete the .NET Core image the Dockerfile was based on. Możesz użyć identyfikatora obrazu lub ciągu sformatowanego jako tag .You can use the IMAGE ID or the REPOSITORY:TAG formatted string.
docker rmi counter-image:latest
docker rmi mcr.microsoft.com/dotnet/aspnet:5.0
Użyj docker images
polecenia, aby wyświetlić listę zainstalowanych obrazów.Use the docker images
command to see a list of images installed.
Porada
Pliki obrazów mogą być duże.Image files can be large. Zazwyczaj można usunąć kontenery tymczasowe, które zostały utworzone podczas testowania i opracowywania aplikacji.Typically, you would remove temporary containers you created while testing and developing your app. Obrazy podstawowe z zainstalowanym środowiskiem uruchomieniowym zwykle są zachowywane, jeśli planujesz tworzenie innych obrazów na podstawie tego środowiska uruchomieniowego.You usually keep the base images with the runtime installed if you plan on building other images based on that runtime.
Następne krokiNext steps
- Dowiedz się, jak konteneryzowanie aplikację ASP.NET Coreową.Learn how to containerize an ASP.NET Core application.
- Wypróbuj ASP.NET Core samouczka mikrousług.Try the ASP.NET Core Microservice Tutorial.
- Przejrzyj usługi platformy Azure, które obsługują kontenery.Review the Azure services that support containers.
- Przeczytaj o poleceniach pliku dockerfile.Read about Dockerfile commands.
- Poznaj narzędzia kontenerów dla programu Visual StudioExplore the Container Tools for Visual Studio