Docker: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Add initial kubernetes section)
(Add command)
 
(4 intermediate revisions by the same user not shown)
Line 5: Line 5:
== Docker ==
== Docker ==


The {{ ic |docker-compose.yml}} file defines the docker service to run.
[https://www.docker.com/ Docker] creates and runs '''containers''', which are sand-boxed processes, isolated from all other processes, with their own allocated cpu/memory/etc.


The {{ ic |Dockerfile}} file defines the actual commands the docker instance should execute once it is created, in order to get our service.
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.
{{ todo |Are the below two commands useful? Needed?}}
These two files should be put into the same directory. Then {{ ic |cd}} into the directory and build it with {{ ic |docker-compose run}}.
These two files should be put into the same directory. Then {{ ic |cd}} into the directory and build it with {{ ic |docker-compose run}}.


Finally, start the service with {{ ic |docker-compose up -build -d}}.
Finally, start the service with {{ ic |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 {{ ic |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
{{ warn |With the above command, data will NEVER persist long term. If the container is ever restarted or rebuilt, all data in the container will be lost. See Mounting Subsection for solutions.}}
===== 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:
* {{ ic |host}} - Corresponds to the host address to connect to. Often {{ ic |127.0.0.1}}.
* {{ ic |host_port}} - Corresponds to the port to use on the host machine for mapping.
* {{ ic |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 {{ ic |<nowiki>--mount type=volume,src=<volume_name>,target=<filepath_in_container></nowiki>}}.
*** Where:
**** {{ ic |volume_name}} - Corresponds to the name of the already-created docker '''volume''' to mount.
**** {{ ic |filepath_in_container}} - Corresponds to the filepath the volume should mount to, within the container. Ex: {{ ic |/etc/my_volume}}.
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 [[Linux/Hard Drive Management#Sym_Links | symlink]]).
** Follows the format {{ ic |<nowiki>--mount type=bind,src=<filepath_on_host>,target=<filepath_in_container></nowiki>}}.
*** Where:
**** {{ ic |filepath_on_host}} - Corresponds to a folder located on HOST machine, to share with container.
**** {{ ic |filepath_in_container}} - Corresponds to the filepath the volume should mount to, within the container. Ex: {{ ic |/etc/my_volume}}.
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 {{ ic |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 {{ ic |container_id}} is the id of the container you wish to access.
Build docker image from the {{ ic |Dockerfile}} in current directory:
docker build -t <image_name>
Where {{ ic |image_name}} is the name you wish to give the image.
Stop running docker container:
docker stop <container_id>
Where {{ ic |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 {{ ic |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 {{ ic |volume_name}} is the name the volume should use.
Examine logs of running docker instance:
sudo docker logs -f <container_name>




== Kubernetes ==
== Kubernetes ==


Kubernetes is a an architecture that runs and manages docker-like containers for applications.
[https://kubernetes.io/ Kubernetes] is a an architecture that runs and manages docker-like containers for applications.
 
{{ note | This is a simplified overview of useful commands for using Kubernetes. For full official docs, see https://kubernetes.io/docs/concepts/overview/}}
 


=== Kubernetes Sub Parts ===


Consists of:
* '''Control Plane''' - Where decisions are made.
* '''Control Plane''' - Where decisions are made.
** Generally has 3 of these running, to ensure availability.
** Generally has 3 of these running, to ensure availability.
Line 36: Line 180:
*** '''Controller Manager''' - Manages the controllers.
*** '''Controller Manager''' - Manages the controllers.
*** '''Key-Value Store''' - Effectively a "database of possible states".
*** '''Key-Value Store''' - Effectively a "database of possible states".
=== Kubernetes Commands ===
{{ todo | Figure out common/useful commands and add here. }}

Latest revision as of 22:42, 19 July 2023

ToDo: Page is very incomplete. Notes of learning docker/kubernetes here.


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.


ToDo: Are the below two commands useful? Needed?

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
Warn: With the above command, data will NEVER persist long term. If the container is ever restarted or rebuilt, all data in the container will be lost. See Mounting Subsection for solutions.


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. Often 127.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.


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.


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.


Examine logs of running docker instance:

sudo docker logs -f <container_name>


Kubernetes

Kubernetes is a an architecture that runs and manages docker-like containers for applications.

Note: This is a simplified overview of useful commands for using Kubernetes. For full official docs, see https://kubernetes.io/docs/concepts/overview/


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".

Kubernetes Commands

ToDo: Figure out common/useful commands and add here.