DOCKER - the absolute basics

Some terminology (which may or may not become clearer at a later date.)

Getting Started

First, get docker desktop, install that on your development machine.

No link included, as the location and the name will change before I commit this file.

You can install it on Windows or Linux or Mac. You can't install it on iOS or Android or Gameboy (as far as I know).

What is docker?

When someone says "docker" they can mean a lot of different things! Docker this, docker that, docker all the things. Sometimes you'll feel you're out of your docking mind!

Docker is: a specific commandline tool; docker refers to the whole (waves hands vaguely) family of docker things; a docker file is a Dockerfile (one word) -- a "text file" that contains a pattern for making a docker image...; and docker is the first word in "docker compose" -- or "docker-compose.yml" -- a file that can describe a group of docker images that work together, a fleet of images, a symphony of images, an orchestrated milieu of containers, a deck of docks, a caravan of containers, an infestation of images, a cartload of containers...

In bullet form -- let's say:

About Port numbers

When specifying port numbers used by containers, they are specified like this:

<outside>:<inside>

For example 8888:80 means "port 8888 on the outside world, the host computer" will be mapped to "port 80 inside the container."

If Narnia is a container running inside a wardrobe, then 1234:100 would mean the wardrobe's port 1234 is mapped to Narnia's port 100.

Note also that we would call Narnia the container and the wardrobe would be properly referred to as the host.

Docker on the command line

docker, (docker.exe on windows) is a command line tool for doing all sorts of dockery things.

Here is an example where we use docker to create a volume, and then create a container, based on an image, that uses that volume.

docker volume create a-sql-volume
docker run -d -e "ACCEPT_EULA=Y" -v a-sql-volume:/var/opt/mssql -e "SA_PASSWORD=p@$$w0rD" -p 1066:1433 -d mcr.microsoft.com/mssql/server:2019-latest

The first command creates a local "volume" (some storage space) that can be used by your containers, and names it a-sql-volume

The next command has a lot more going on -- let's break it down...

The general structure is:

docker run {options} {IMAGE}

And the options specified above, if listed one at a time, are:

-d
-e "ACCEPT_EULA=Y"
-v a-sql-volume:/var/opt/mssql
-e "SA_PASSWORD=p@$$w0rD"
-p 1066:1433
-d

Followed by the base image, in this case: mcr.microsoft.com/mssql/server:2019-latest

tip -- For help on the run command and its options, use docker run --help

Explaining these one at a time:

Finally, mcr.microsoft.com/mssql/server:2019-latest is the name of the image on which the container is based.

Docker cp

When a container is running, you can copy files to or from it, using docker cp {FROM_PATH} {TO_PATH}.

e.g. To copy a file from a container called "my_container", to your local file system's current path:

docker cp my_container:/var/www/html/.htaccess ./

And conversely, to copy a file from your local file system to a path on a running container called "my_container":

docker cp ./.htaccess my_container:/var/www/html/.htaccess

Building an image from a Dockerfile

Simplest thing that can possibly work:

With the docker daemon running (i.e. with Docker Desktop running)

In bash (or gitbash or windows subsystem for linux) or even in PowerShell -- in the same folder as your Dockerfile

docker build .

After which if you use docker images you may see that the first image listed is the image you created.

Boom.

During the build you can also set metadata of your image -- e.g.

docker build . --label "test=true"

... will then be found by:

docker images --filter "label=test=true"

Or if there are multiple found, you find just the first one by:

docker images --filter "label=test=true" -q | head -1

or in PowerShell

docker images --filter "label=test=true" -q | select -first 1

(due to -q for "quiet", returns just the identifier for the image)

Get that identifier as a variable:

export id=$(docker images --filter "label=test=true" -q | head -1)

Create an container, from that image, with a name of your own choosing...

docker create --name "My_New_Container_Name" $id

Copy files from that container:

docker cp My_New_Container_Name:/src/tests/MyProject/TestResults

(In this case we're copying some test results out)

List Images in More Detail

The --format 'json' parameter lets you explore the data more completely.

In Powershell I pipe that json to ConvertFrom-Json and turn it into objects I can really do things with.

docker images --all --format 'json' | ConvertFrom-Json | select *

(Or, still in powershell, you can pipe to Out-GridView to explore it in a grid control)

See also