Docker
Docker
Docker creates and runs containers, which are sand-boxed processes, isolated from all other processes, with their own allocated cpu/memory/etc.
Each docker container is "meant to do exactly one thing, and do it well". So it's common to have a docker stack comprised of multiple containers. For example, one container to run your main application, and another for your database.
Docker Sub Parts
Core Parts:
- Image - A definition of docker logic, which can be spun up and run as a container.
- Due to how docker works, any number of containers can be build from this image, and it will run on any OS that supports docker.
- Container - A unique, running instance of a docker image.
- Multiple containers can be created from a single image.
- Volume - An location for docker to persist data to long-term.
- Allows data to last even as a container is started, stopped, and restarted.
- Also allows multiple containers to share a set of data, so long as the data is put into the volume.
Setup:
- docker-compose.yml - A file that defines the docker service to run.
- Dockerfile - A file that defines the actual commands the docker instance should execute once it is created, in order to setup the intended container service.
These two files should be put into the same directory. Then cd
into the directory and build it with docker-compose run
.
Finally, start the service with docker-compose up -build -d
.
Docker Commands
Commands to Launch Containers
Launch a built image as a new container instance:
docker run -d <image_name>
Where image_name
- Corresponds to the name of the image you wish to create a container of.
- See below sections for additional useful optional args.
Example:
docker run -d my-cool-image
Port Mapping Subsection
To expose ports of a container instance to the host, we use port mapping.
Command follows the format of:
-p <host>:<host_port>:<container_port>
Where:
host
- Corresponds to the host address to connect to. Often127.0.0.1
.host_port
- Corresponds to the port to use on the host machine for mapping.container_port
- Corresponds to the port to connect to on the container.
Example:
docker run -d -p 127.0.0.1:3000:3000 my-cool-image
Command Mount Subsection
To persist data long term, we need to add some form of mount subcommand.
Volume Mount:
- Volume Mount - Allows persisting data for docker instances.
- Follows the format
--mount type=volume,src=<volume_name>,target=<filepath_in_container>
.- Where:
volume_name
- Corresponds to the name of the already-created docker volume to mount.filepath_in_container
- Corresponds to the filepath the volume should mount to, within the container. Ex:/etc/my_volume
.
- Where:
- Follows the format
Example:
docker run -d --mount type=volume,src=my-volume,target=/etc/my_volume my-cool-image
Bind Mount:
- Bind Mount - Allows sharing data between host machine and container, via linking filepaths (similar to a symlink).
- Follows the format
--mount type=bind,src=<filepath_on_host>,target=<filepath_in_container>
.- Where:
filepath_on_host
- Corresponds to a folder located on HOST machine, to share with container.filepath_in_container
- Corresponds to the filepath the volume should mount to, within the container. Ex:/etc/my_volume
.
- Where:
- Follows the format
Example:
docker run -d --mount type=bind,src=/srv/my_project/share_folder,target=/etc/my_volume my-cool-image
Command Network Subsection
Docker networks are used to allow containers to connect and communicate among each other.
Attach Network:
--network <network_name>
Where network_name
- Is the docker virtual network to connect in.
Example:
docker run -d --network my_network my-cool-image
General Commands
List all live docker containers:
docker ps
List all (including stopped) docker containers:
docker ps --al
Access a given running container:
docker exec <container_id>
Where container_id
is the id of the container you wish to access.
Build docker image from the Dockerfile
in current directory:
docker build -t <image_name>
Where image_name
is the name you wish to give the image.
Stop running docker container:
docker stop <container_id>
Where container_id
is the id of the container you wish to stop.
Remove existing docker container:
docker rm <container_id>
OR combine the above two commands with:
docker rm -f <container_id>
Where container_id
is the id of the container you wish to stop.
Create a new volume to allow persisting data long-term:
docker volume create <volume_name>
Where volume_name
is the name the volume should use.
Kubernetes
Kubernetes is a an architecture that runs and manages docker-like containers for applications.
Kubernetes Sub Parts
- Control Plane - Where decisions are made.
- Generally has 3 of these running, to ensure availability.
- Worker Node - A location where something might be running.
- Can have any number of these, as needed.
- Parts:
- Kubletes - Manages one or more PODS, each POD being a group of docker-style containers.
- Kube Proxy - Ensures communication is fluid between all parts.
- Controller - Helps control all other aspects, to ensure it does what it's meant to.
- External Resources - Various outside resources that the Control Plane and Worker Nodes do work with.
- Ex: Storage, load balancers, etc.
- API - An interface for humans to interact with the Control Planes.
- Most interactions with the API involve telling Kubernetes "this is the state I want everything to be in". Kubernetes then figures out how to make that happen.
- Parts:
- Scheduler - Determines what should happen, when, and where.
- Controller Manager - Manages the controllers.
- Key-Value Store - Effectively a "database of possible states".