Controlling Docker

Outside of the ease of standardizing my development teams environments through the usage of containers running on Docker, I’m excited about the possibilities around providing easy access to demo sites for my sales team members as well. Anyone reading with a technical background, may think that this a bit crazy of an idea, but by the end of this review of common commands and actions, you’ll hopefully see how hands-off the possibilities are for a less technical resource to be able to take advantage of Sitecore (or any application really) running on Docker a container.

If your not already familiar with the basic terms and process of Docker, be sure to review my intro post first.

The following assumes you’ve already install Docker Desktop for Windows and switched to using "Containers for Windows"

Basics Commands for Running and Interacting with Images and Containers

There are extensions for Visual Studio Code that provide some level of GUI interface to working with the Docker CLI and Engine, but for the fullest feature set the command line is a must, specifically PowerShell. I find leveraging the Windows Terminal with multiple profiles setup is the easiest way to manage this. I like the different profiles as they a) allow me to launch PowerShell with a pre-defined start path and b) provide the ability for custom images and colors to help me keep straight where I am working at.

All the commands should be ran from a prompt running as Admin.

Each Docker CLI command is rich in options, in the examples I will be demonstrating I will only be calling out the most critical options for simplistic success. If you need to support more complex solutions the Docker documentation is very thorough.

Build an Image

The first thing to do is to build an image from a dockerfile.

> docker image build --no-cache --build-arg TOOL_DIRECTORY=C:\tools `
 --build-arg HOST_URL=thecodeattic.io --tag 'sample-image-name:v01' `
 'C:\full directory path\to the\dockerfile'
Part of Command Description
image build identifies that an image build process should be run, this can also be ran without the ‘image’ identifier, but is helpful for clarity when used in a script
–no-cache optional, when included forces the docker build engine to create all layers and steps new instead of re-using any layers from previous runs
–build-arg optional, this is a key=value pair, where the key identifies a value in the dockerfile which is then substituted with the value. Multiple ‘build-args’ can be defined
–tag required, defines the name and the tag of the image. If no tag (the part after the colon) is defined Docker will default the tag to ‘latest’.
–file, -f, or path to a directory as last value optional, allows you to identify the parent directory of the dockerfile to be built. If not defined, will look for a dockerfile in the current path of the prompt.

Run an Image

Once there is an image it can be initialized into a container for running.

> docker run --detach --expose 4404:80 `
 --name 'sample-site' 'sample-image-name:v01'
Part of Command Description
run command that initializes a new container based on the named image
–detach or -d optional but if this option is exclude from the command the prompt is locked to the running of the container, when the prompt closes the container will stop
–expose or -e optional, exposes a port from the container to the host machine. It is written in the format host machine port : container port, multiple ports can be exposed.
–name optional, a name that can be used to reference this container in other commands.
final parameter is the name of the image with version that should be initialized

Show Containers

> docker ps -a
Part of Command Description
ps lists all running containers
–all or -a _optional, includes any stopped containers in the list. It is recommended that this is always included as it allows you to better detect issues of a container has exited

docker ps

Interactive Command to Running Container

To open a PowerShell on the running container and allow you to run commands in context of the container. To exit the interactive prompt you will need to type ‘exit’, this will return you to the host machine.

> docker container exec -it 'sample-site' PowerShell
Part of Command Description
exec execute command
-i optional, indicates execute the command in interactive mode
-t optional, allocates a pseudo-TTY interface
‘sample-site’ name or id of the container connecting to
PowerShell the command that is to be executed, in this example the PowerShell prompt

Quick Check of a Container Logs

Depending on the container some form of logs maybe exposed to the Docker CLI. In context of Sitecore, the main Sitecore log has been exposed to Docker and can be accessed via this command.

> docker container logs --tail 100 'sample-site'
Part of Command Description
logs command to fetch the container logs
–tail _optional, include with a numeric value of how many lines from the bottom of the file to display
–follow or -f optional, locks the prompt to display new lines from the log as they appear

docker log sample

Docker Compose Create and Start the Containers

> docker-compose up -d
Part of Command Description
up command that tells to bring-up and start all containers in the docker-compose file, the dockercompose file must exist at the current path of the prompt
–detach or -d optional, runs the containers in the background freeing the prompt for further commands

docker-compose

Docker Compose Start the Containers

If the containers already exist but are stopped, this will bring them back to life (ie start them back up again)

> docker-compose start

Docker Compose Stop the Containers

This just stops the containers from running, but holds them for quick restart.

> docker-compose stop

Docker Compose Stop and Remove the Containers

This stops the containers and removes them, freeing up space. Removal does loose any data that has not been saved to a mounted volume.

> docker-compose down

Commands for General System Cleanliness

Part of what makes Docker very fast and efficient at managing images and containers is how it leverages layers and a caching mechanism to allow for re-use. When you are performing doing a lot of image builds it is easy to pollute your system with cached image layers or even old test images that eat up critical disk space.

The following are commands useful for maintaining system cleanliness.

List all images

To see all the images on your machine run

> docker images

image list

  • note the repositories named **** are cached layers from an image build
  • note two, the size shown is not the size on disk but the size of the image when all layers are added up

Prune the System

As you can see in the above image list, Docker doesn’t always cleanup resources after containers are stopped and images building is complete. To help maintain a healthy system running the prune command will clear-out stopped containers, networks, and images no longer needed.

> docker system prune

Remove Old Images

Sometimes after a build process or projects close out you no longer need images left locally (they can always be pulled again from your private registry, you did push them?)

> docker image rm 'sample-image-name:v01'

Combining PowerShell with Docker CLI

The following is a sample of combining Docker CLI with standard PowerShell logic to perform more advance operations

Format, Sort, and Filter Image List

Use the optional format parameter to limit and format a specific display

> docker images --format "{{.Repository}}:{{.Tag}}"

Sort the image list for easier reading

> docker images --format "{{.Repository}}:{{.Tag}}" | Sort-Object $_

Add some logic to display only a subset of images

> docker images --format "{{.Repository}}:{{.Tag}}" `
| Where-Object{ $_.Contains("v77") } | Sort-Object $_

Finally, automate the removal of a series of images

> docker images --format "{{.Repository}}:{{.Tag}}" `
| foreach{ & docker image rm $_ }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.