Basic Docker commands

Completed

The following sections discuss basic Docker commands that you'll need to download an image and start a container.

Download an image

To download an image, you need to use the docker pull command, where you can specify the name of the image. The image is stored in a repository, so the full name should be provided.

The following example shows the docker pull command:

docker pull <image_name>

To download the Windows Server core image, use the following logic:

docker pull mcr.microsoft.com/windows/servercore:ltsc2019

The LTSC is a long-term servicing channel that is (normally) updated every three years. An LTSC version is available for Windows Server 2016 and for Windows Server 2019, which indicates the platform that you want to run it on. The base operating system will also be a Windows Server 2019 version.

Until the summer of 2020, Microsoft provided separate images for Business Central, but has since replaced them with artifacts. The different Business Central images/artifacts will be explained in the subsequent unit.

With these images, it's possible to download the latest available Business Central sandbox to run on Windows Server 2019.

docker pull mcr.microsoft.com/businesscentral/sandbox:ltsc2019

The first time when you download an image, it can take a few minutes to hours (depending on your connection speed). Each image is built with different blocks that can be reused when you download another image. If another image also uses Windows Server core version 2019, it can reuse the block that is already downloaded.

Create a container

After downloading the image, it is time to create a container. A container is an instance of an image. You can create an image with the docker run command:

docker run <image_name> --name <container_name>

With the docker run command, you can start a new instance of the image that you provide as parameter. Optionally, you can give this instance a name with the --name option; otherwise, Docker will generate a name (which is always an adjective and a person's name, such as "awesome_john"). We recommend that you use lowercase letters for the name.

docker run mcr.microsoft.com/businesscentral/sandbox:ltsc2019 --name bcdev

The docker run command accepts several other options that won't be discussed in this unit. One option that should be discussed is the -ti option, which is used to create a container in interactive mode. It will start a terminal inside the container to interact with the container. If you pass cmd as a parameter, it will launch a command prompt within the container, and then you can interact by using commands.

docker run -ti mcr.microsoft.com/windows/servercore:ltsc2019 cmd

Show containers

To show the running containers, you can use the docker ps command. If you specify the -a option, you will get a list that shows all containers, even those that are not running.

docker ps
docker ps -a

Each container is identified with a container ID and a name. To run other commands on a container, you can use the ID or the name. The list will also display the image that the container is based on.

CONTAINER ID  IMAGE                                              NAMES
321046e7faca  mcr.microsoft.com/windows/servercore:ltsc2019      awesome_john
11a1be01305d  mcr.microsoft.com/businesscentral/sandbox:ltsc2019 bcdev

Start and stop a container

When you work with a container, you do not need to use the docker run command to start your container because this action would create a new container each time. Starting a container is done by using the docker start command, and stopping a container is done by using the docker stop command.

docker start 11a1be01305d
docker stop 11a1be01305d

Remove a container

To delete a container, which won't delete the image, you can use the docker rm command. You can only delete a container that isn't running. Therefore, to remove a running container, you need to stop the container with the docker stop command and then use the docker rm command. You can also use the -f option on the docker rm command to force the removal.

docker rm 11a1

Use the container ID or name

The previous example showed that only part of the container ID is specified. Actually, it is sufficient to specify a part of the container ID that will uniquely identify a container. The previous examples show two containers, one with ID 321046e7faca and one with ID 11a1be01305d. Already, the first letter of the ID is different between the two containers.

You can use the following commands with this first letter:

docker start 1
docker stop 1
docker rm 1

You can also use the name instead of the ID:

docker start bcdev
docker stop bcdev
docker rm bcdev

Use the Docker extension for Visual Studio Code

Microsoft released an extension for Visual Studio Code that can help you create, manage, and debug containers. Within Visual Studio Code, you can search for Docker in the Extensions tab. Docker for Visual Studio Code.

The extension provides an excellent overview of your images, containers, and registries. Additionally, volumes are simple to create by using this extension.

Screenshot of the Docker extension in Visual Studio Code.