Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker in Production Docker Swarm Basics init join service stack configs secrets
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-in-production-docker-swarm-basics-init-join-service-stack-configs-secrets

Docker Docker Docker in Production Docker Swarm Basics init join service stack configs secrets

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Create a Docker Swarm cluster by initializing a manager node and joining worker nodes. docker swarm init

2. Step-by-Step

  1. Check if Docker is installed and running: docker --version
  2. Initialize a Docker Swarm cluster: docker swarm init --advertise-addr <manager-ip>
  3. Output: Swarm initialized, manager is
  4. Clean up: docker swarm leave
  5. ⚠️ Delete all stopped containers: docker container prune -f
  6. Join a worker node to the Swarm cluster: docker swarm join --token <token> <manager-ip>:2377
  7. Output: This node joined a swarm as a worker.
  8. Clean up: docker swarm leave
  9. Create a new service: docker service create --name <service-name> <image-name>
  10. Output: Service created
  11. Clean up: docker service rm <service-name>
  12. Create a new stack: docker stack deploy --compose-file <compose-file> <stack-name>
  13. Output: Stack created
  14. Clean up: docker stack rm <stack-name>

3. Real Commands with Examples


Example 1: Create a service

  • Purpose: Run a web server on port 80
  • Command: docker service create --name web-server --publish 80:80 nginx
  • Brief explanation of flags: --name sets the service name, --publish maps port 80 on the host to port 80 in the container

Example 2: Create a stack

  • Purpose: Run a web server and a database on separate containers
  • Command: docker stack deploy --compose-file docker-compose.yml web-app
  • Brief explanation of flags: --compose-file specifies the Docker Compose file to use, --name sets the stack name

Example 3: Create a config

  • Purpose: Set environment variables for a service
  • Command: docker config create --label <label> <config-name> <config-value>
  • Brief explanation of flags: --label sets a label for the config, --name sets the config name

4. Common Errors


Error 1: Swarm mode not enabled

  • Error message or symptom: Error response from daemon: Swarm is not initialized.
  • Why it happens: Docker Swarm is not enabled
  • One-line fix: docker swarm init

Error 2: Service not created

  • Error message or symptom: Error response from daemon: Service not found: <service-name>
  • Why it happens: Service does not exist
  • One-line fix: docker service create --name <service-name> <image-name>

Error 3: Config not found

  • Error message or symptom: Error response from daemon: Config not found: <config-name>
  • Why it happens: Config does not exist
  • One-line fix: docker config create --name <config-name> <config-value>

5. Quick Checks


Scenario 1: Check if Swarm is initialized

  • Situation: You want to check if Docker Swarm is initialized
  • Command to run: docker info
  • What a correct response looks like: Swarm: active

Scenario 2: Check if service is running

  • Situation: You want to check if a service is running
  • Command to run: docker service ps <service-name>
  • What a correct response looks like: Name and Status columns with running services

Scenario 3: Check if config exists

  • Situation: You want to check if a config exists
  • Command to run: docker config ls
  • What a correct response looks like: ID and Name columns with existing configs

6. Last-Minute Reference

  • docker swarm init: Initialize a Docker Swarm cluster
  • docker swarm join: Join a worker node to the Swarm cluster
  • docker service create: Create a new service
  • docker stack deploy: Create a new stack
  • docker config create: Create a new config
  • docker config rm: Remove a config
  • docker service rm: Remove a service
  • docker stack rm: Remove a stack
  • docker swarm leave: Leave the Swarm cluster
  • docker container prune: Remove stopped containers
  • docker system prune: Remove unused data
  • ⚠️ docker swarm init --advertise-addr : Specify the manager IP address
  • ⚠️ docker swarm join --token :2377: Specify the join token and manager IP address
  • ⚠️ docker config create --label : Specify the config label and value


ADVERTISEMENT