Docker
Docker is an open-source platform that allows developers to automate the deployment, scaling, and management of applications by using containerization. It provides a way to package and run an application and its dependencies in a standardized and isolated environment called a container.
Networking
Compose
Volumes
You can set up external volumes for your docker container.
volumes:
network_name:
name: my_external_network_name
external: true
Networking
Docker compose sets up a default network called appname_default
.
Specify an external network.
networks:
my_network:
name: my-pre-existing-network
external: true
Kompose
- Translate
docker-compose.yml
to kubernetes withkompose convert -f docker-compose.yml
- Filter to use specific services with
kompose convert -f docker-compose.yml --filter service=app
src/docker.py
"""Module for docker classes and functions."""
import subprocess as sup
from hax import log
from pathlib import Path
from typing import Optional
class Container:
"""The container class provides methods for working with docker containers."""
# TODO: Move this module to cicd.py
@staticmethod
def run(repo: Path,
container: str,
ports: Optional[tuple[int, int]] = None,
command: Optional[str] = None,
detached: bool = False):
"""Start up container.
Arguments:
- repo: Path to repository.
- container: Name of container.
- ports: Tuple of ports to expose.
- command: Command to run in container.
"""
if detached:
log.info(f"Starting container '{container}' at {ports[0]}:{ports[1]} in repo '{repo}' with command '{command}'")
sup.run(f"cd {repo} && docker-compose run -d --rm -p {ports[0]}:{ports[1]} {container} {command}", shell=True)
elif command and ports:
log.info(f"Starting container '{container}' at {ports[0]}:{ports[1]} in repo '{repo}' with command '{command}'")
sup.run(f"cd {repo} && docker-compose run -it --rm -p {ports[0]}:{ports[1]} {container} {command}", shell=True)
elif command and not ports:
log.info(f"Starting container '{container}' in repo '{repo}' with command '{command}'")
sup.run(f"cd {repo} && docker-compose run -it --rm {container} {command}", shell=True)
elif not command and ports: # This case will probably never happen.
log.info(f"Starting container '{container}' at port '{ports[0]}:{ports[1]}' in repo '{repo}'.")
sup.run(f"cd {repo} && docker-compose run -it -p {ports[0]}:{ports[1]} {container}", shell=True)
else:
log.info(f"Starting container '{container}' in repo '{repo}' in detached mode.")
sup.run(f"cd {repo} && docker-compose up {container} -d", shell=True)