Docker Domain1

Removing a container named myContainerName

$ docker rm $(docker ps -aq --filter name=myContainerName)

@TODO list

  • https://linuxacademy.com/blog/linux-academy/docker-certified-associate-preparing-for-the-exam/

Orchestration 25% of the exam

Complete the setup of a swarm mode cluster, with managers and worker nodes

Use the Docker Engine CLI to create a swarm of Docker Engines where you can deploy application services. You don’t need additional orchestration software to create or manage a swarm.

Docker Engine uses a declarative approach to let you define the desired state of the various services in your application stack. For example, you might describe an application comprised of a web front end service with message queueing services and a database backend.

Swarm

We are going to be using docker swarm. Swarm is the cluster management and orchestration.

  • A swarm consists of multiple Docker hosts which run in swarm mode and act as managers
  • A given Docker host can be a manager, a worker, or perform both roles
  • When you create a service, you define its optimal state (number of replicas, network and storage resources available to it, ports the service exposes to the outside world, and more)
  • Docker works to maintain that desired state
Nodes
  • A node is an instance of the Docker engine participating in the swarm
  • You can run one or more nodes on a single physical computer or cloud server
  • To deploy your application to a swarm, you submit a service definition to a manager node
  • The manager node dispatches units of work called tasks to worker nodes.
  • Worker nodes receive and execute tasks dispatched from manager nodes.
  • By default manager nodes also run services as worker nodes, but you can configure them to run manager tasks exclusively and be manager-only nodes
Services and tasks
  • A service is the definition of the tasks to execute on the manager or worker nodes.
  • It is the central structure of the swarm system and the primary root of user interaction with the swarm.
  • When you create a service, you specify which container image to use and which commands to execute inside running containers.
  • In the replicated services model, the swarm manager distributes a specific number of replica tasks among the nodes based upon the scale you set in the desired state.
  • For global services, the swarm runs one task for the service on every available node in the cluster.
  • A task carries a Docker container and the commands to run inside the container
  • It is the atomic scheduling unit of swarm
  • Once a task is assigned to a node, it cannot move to another node.
Load Balancing
  • The swarm manager uses ingress load balancing to expose the services you want to make available externally to the swarm
  • The swarm manager can automatically assign the service a PublishedPort or you can configure a PublishedPort for the service
  • You can specify any unused port. If you do not specify a port, the swarm manager assigns the service a port in the 30000-32767 range.
  • All nodes in the swarm route ingress connections to a running task instance.
  • Swarm mode has an internal DNS component that automatically assigns each service in the swarm a DNS entry
  • The swarm manager uses internal load balancing to distribute requests among services within the cluster based upon the DNS name of the service

Hello world docker image

Since we have gotten the basics out of our way and have an understanding about how docker works in swarm more, let’s get one hello world docker application ready to be used later on.

# create a hello world docker image
# make sure the tag name uses this pattern `<docker_hub_user_name>/<name_of_the_application>`
docker built -t tolgaakyuz/go-hello-world

$ docker login
$ docker push tolgaakyuz/go-hello-world:latest

# after this point you should be able to see the uploaded image in your docker hub repository list https://cloud.docker.com/repository/list

Let’s crate a swarm with 2 master 2 worker nodes with virtual box.

FOMR alpine
COPY . /root
# to list the machines in your swarm
$ docker-maching ls

# if you have unwanted machines listed, you can get rid of them with this command
# first observe the name of the machines from the previous command
$ docker-machine stop <name>
$ docker-machine rm <name>

# let's create our swarm

# create machines
$ docker-machine create --driver virtualbox swarm-vm1
$ docker-machine create --driver virtualbox swarm-vm2
$ docker-machine create --driver virtualbox swarm-vm3

# get the necessary environment variables
$ docker-machine env swarm-vm1

# and run them
$ eval $(docker-machine env swarm-vm1)

# creating the swarm, this one is for setting the master
$ docker-machine ssh swarm-vm1 "docker swarm init --advertise-addr <ip_address*>"
* ip_address: can be obverseved from running `docker-machine ls`

# creating the nodes
$ docker-machine ssh swarm-vm3 "docker swarm join --token <token*> <ip_addresss*>"
* token: can be observed from the output of the above command
* ip_address: can be observed from the output of the above command

# list the docker swarm nodes
$ docker-machine ssh swarm-vm1 "docker node list"

$ docker stack deploy --compose-file docker-compose.yml swarm-hello-world

# to list the running services
$ docker service ls

# to see the logs of a service
$ docker service logs swarm-hello-world_hello-world

# testing the service, any ip of the nodes works here
$ curl http://192.168.99.102:8080

# listing the tasks
# tasks are little applications running on worker nodes and making sure the desired state of the node
$ docker stack ps swarm-hello-world

# upgrade services
$ docker service update --replicas 10 frontend
initializing a cluster of Docker Engines in swarm mode
adding nodes to the swarm
deploying application services to the swarm
managing the swarm once you have everything running

State the differences between running a container vs running a service

In short: Docker service is used mostly when you configured the master node with Docker swarm so that docker containers will run in a distributed environment and it can be easily managed.

Docker run: The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.

That is, docker run is equivalent to the API /containers/create then /containers/(id)/start

source: https://docs.docker.com/engine/reference/commandline/run/#parent-command

Docker service: Docker service will be the image for a microservice within the context of some larger application. Examples of services might include an HTTP server, a database, or any other type of executable program that you wish to run in a distributed environment.

When you create a service, you specify which container image to use and which commands to execute inside running containers. You also define options for the service including:

  • the port where the swarm will make the service available outside the swarm
  • an overlay network for the service to connect to other services in the swarm
  • CPU and memory limits and reservations
  • a rolling update policy
  • the number of replicas of the image to run in the swarm

source: https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/#services-tasks-and-containers

Demonstrate steps to lock a swarm cluster

Extend the instructions to run individual containers into running services under swarm

Interpret the output of “docker inspect” commands

Convert an application deployment into a stack file using a YAML compose file with “docker stack deploy”

Manipulate a running stack of services

Increase # of replicas

Add networks, publish ports

Mount volumes

Illustrate running a replicated vs global service

Identify the steps needed to troubleshoot a service not deploying

Apply node labels to demonstrate placement of tasks

Sketch how a Dockerized application communicates with legacy systems

Paraphrase the importance of quorum in a swarm cluster

Demonstrate the usage of templates with “docker service create”