Docker explorations

Docker is probably the most popular containerisation system around.

Concepts

Docker is a containerisation system which sits between the host operating system and the container which contains the application. The container can include everything it needs to function and operates independent of other containers.

Docker is composed of 3 main components:

  • Docker daemon

    The daemon (dockerd) manages the various docker objects and interfaces with the docker client.

  • Docker client

    The docker client (docker) is a commandline interface where users issue commands to send to the daemon.

  • REST API

    This is the channel by which the daemon and client communicate.

There are 4 main types of objects that comprise the docker environment:

  • container -
  • image -
  • network -
  • volume -

The default object is the container. When you issue the command:

# docker run <image name>

This is synonymous to:

# docker container run <image name>

Setup docker environment

Managing images

Running docker images

Managing containers

List containers

Current running containers

# docker container ls

List all containers that have been run previously

# docker container ls -a

or

# docker container ls --all

Stopping a running container

Most often, containers are run in interactive mode, meaning it stays in the foreground and the user is able to interact with the commandline.

It can be closed by merely issuing a CTRL + C. This sends a SIGTERM signal to the terminal.

If it doesn't close immediately, the docker will wait and after a predefined timeout will issue a SIGKILL, which immediately shuts down the container.

If the container is in background mode, meaning there is no terminal open to interact with, you can issue:

# docker container stop <container name>

If, for some reason, the container does not shut down, you can issue:

# docker container kill <container name>

Starting/Restarting a container

Starting a container is performed on a stopped container, whereas restarting a container is performed on a running or stopped container.

Get a list of containers:

# docker container ls --all

This will give you a listing of all the containers that are running and stopped. You can get container name at the far right of the row.

The command is:

# docker container start <container name>

# docker container restart <container name>

Creating a container

Containers are created from images. For example:

# docker container create -p 8080:80 wordpress

Delete containers

Containers that have been stopped or killed remain in the system. They take up space on the system. If they are no longer needed, we can remove them by issuing:

# docker container rm <container name>

There might be instances where you want to discard the container after closing. This can be done using the following command when launching the container:

# docker container run --rm --detach --publish 8080:80 --name some-wordpress wordpress

The --rm option indicates that we want to discard this image as soon as it's shut down.

Executing commands in the container

You may have containers which are running in detached mode and you want to issue a command in that container. You can do this by:

# docker container run <image name> <command>

You could also, in the case of an OS, open a command prompt, i.e., bash, to the OS:

# docker exec -it <container name> /bin/bash

That opens a bash prompt into the container in interactive mode.

Creating your own images

You can create your own docker image by defining a Dockerfile and issuing:

# docker image build .

You can add a --tag option to provide a name to your build:

# docker image build --tag <repo name>:<build name> .

I'm sure there's more to this than what I've noted here, but that's for another time.

List images

# docker image ls

Deleting images

# docker image rm <image name>

To clean up untagged dangling images, issue:

# docker image prune --force

The --force option skips confirmation prompts. You can also use --all or -a to remove all cached images.