Sunday, December 22, 2024
HomeDevOpsHow to Deploy application Image using Docker Service

How to Deploy application Image using Docker Service

To deploy an application image when Docker Engine is in swarm mode, you create a service. Frequently a 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. For more: Docker

docker service Flow
Image Source: docker.com

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

Starting with a service

Let’s pick a service or a long running container. This could be the sleep 500 command, but we can just as easily make it ping docker.com.

Have a look at the new docker service command’s help page:

# docker service

docker service

Let’s go one by one docker service command.

docker service create

docker service create is synonymous with docker run, so anything we can do with docker run should work the same.

# docker run busybox ping google.com
PING google.com (172.217.6.78): 56 data bytes
64 bytes from 172.217.6.78: seq=0 ttl=46 time=76.507 ms
64 bytes from 172.217.6.78: seq=1 ttl=46 time=76.387 ms
64 bytes from 172.217.6.78: seq=2 ttl=46 time=76.376 ms
64 bytes from 172.217.6.78: seq=3 ttl=46 time=76.325 ms

Here’s the equivalent command with docker service create:

# docker service create --name ping busybox ping foxutech.com
3uyw3cqsdwizqenk7jcgv1sbq

We have create the service with name, which we can refer it easily in future for our reference. This help us to refresh anytime our service and also its generated GUID of assigned a name to the service so that we can reference it easily, but Docker has also generated a GUID of 3uyw3cqsdwizqenk7jcgv1sbq.

docker service ls

Check which services we have running/created type in docker service ls as below:

# docker service ls
ID            NAME  REPLICAS  IMAGE    COMMAND
3uyw3cqsdwiz  ping  1/1       busybox  ping foxutech.com

docker service scale

Scaling this has never been easier, we just type in the name of the service and how many replicas we want.

# docker service scale ping=4

this command make ping service scaled to 4

We will now see that the docker service ls command gives 4/4 replicas as running and docker ps will show 4 unique containers running, too.

# docker service ls
ID                     NAME  REPLICAS  IMAGE     COMMAND
3uyw3cqsdwiz           ping  4/4       busybox   ping foxutech.com

docker scale remove

Once you have finished your work either scale back to 0 or remove the service with docker service rm ping

# docker service rm ping

Update a Service

You can change almost everything about an existing service using the docker service update command. When you update a service, Docker stops its containers and restarts them with the new configuration.

Since Nginx is a web service, it will work much better if you publish port 80 to clients outside the swarm. You can specify this when you create the service, using the -p or –publish flag. When updating an existing service, the flag is –publish-add. There is also a –publish-rm flag to remove a port that was previously published.

Assuming that the webserver service from the previous section still exists, use the following command to update it to publish port 80.

# docker service update --publish-add 80 webserver

To verify that it worked, use docker service ls:

# docker service ls 
ID            NAME       REPLICAS  IMAGE                COMMAND
0nbjrzb080mf  webserver  1/1       motoskia/apache-php

# docker ps

Remove a Service

To remove a service, use the docker service remove command. You can remove a service by its ID or name, as shown in the output of the docker service ls command. The following command removes the ping service.

# docker service remove ping

 

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments