Home  Tech   How to laun ...

How to launch web app in Docker Swarm mode

To create 2 replicas of a Docker web app image and launch them in Docker Swarm mode, you need to follow these steps:

  1. Initialize Docker Swarm (if you haven’t already).
  2. Create a Docker Compose file that defines the service with the desired number of replicas.
  3. Deploy the stack to Swarm using the Docker Compose file.

Step-by-Step Instructions

1. Initialize Docker Swarm

First, you need to initialize Docker Swarm mode on your machine.

docker swarm init

This command will initialize the Swarm and make your current machine the manager node.

2. Create a Docker Compose File

Create a docker-compose.yml file that defines your service and specifies the number of replicas. Here’s an example:

version: '3.8'

services:
  myservice:
    image: your-image:latest
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"

Replace your-image:latest with the actual name and tag of your Docker image. This example also maps port 80 on the host to port 80 on the container. You can adjust the ports as needed.

3. Deploy the Stack to Swarm

Use the docker stack deploy command to deploy the stack defined in your docker-compose.yml file to the Swarm.

docker stack deploy -c docker-compose.yml mystack

This command will deploy the services defined in docker-compose.yml under a stack named mystack. You can use any name for your stack.

Verify Deployment

To verify that your service is running with the specified number of replicas, you can use the following command:

docker service ls

This will list all the services running in the Swarm, showing the desired and actual number of replicas for each service.

To get more details about a specific service, including the status of each replica, use:

docker service ps mystack_myservice

Replace mystack_myservice with the actual name of your service as shown in the docker service ls output.

Example Output

After running the commands, you should see an output similar to this:

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                  PORTS
ab1234xyz567        mystack_myservice   replicated          2/2                 your-image:latest      *:80->80/tcp

$ docker service ps mystack_myservice
ID                  NAME                      IMAGE                  NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
abcdefg12345        mystack_myservice.1       your-image:latest      node1               Running             Running 10 seconds ago                       
hijklmn67890        mystack_myservice.2       your-image:latest      node1               Running             Running 12 seconds ago
Published on: Jun 16, 2024, 09:45 PM  
 

Comments

Add your comment