The software described in this documentation is either no longer supported or is in extended support.
Oracle recommends that you upgrade to a current supported release.

6.2.1 Creating a service and deploying containers

Containers can be deployed to the swarm in much the same way as containers are run on a single host. A service is created and the image that should be used to deploy the container is specified. Additionally, port mappings and the number of replica containers to deploy across the swarm can be specified as required. The following example shows how to create a service consisting of three replica containers deployed within the swarm. The service is given a name that makes it easy to identify. Port mappings are defined. In this example, the service uses the nginxdemos/hello image.:

# docker service create --replicas 3 --name hello -p 80:80 nginxdemos/hello

If the image is not already available to each node, the image is automatically pulled from the Docker Hub. Therefore, for this example to work, all nodes must either have direct internet access or should be configured to use a proxy. In a production environment, images would usually be hosted in a local registry and each node could pull the image from the local registry.

You can check which services are running on a swarm at any time:

# docker service ls

Typical output for this command is as follows:

ID            NAME        REPLICAS  IMAGE             COMMAND
64w8p8smco49  hello  3/3       nginxdemos/hello  

From the output, it is possible to see the ID assigned to the service, the name of the service, how many replica containers are running, the image that was used to spawn the containers and any commands issued to run in the container when it was started. For more detail on the status of each container instance running within the service, you list the tasks running in the service as follows:

# docker service ps ID|SERVICE

For example:

# docker service ps hello
ID                NAME     SERVICE  IMAGE             LAST STATE             DESIRED STATE  NODE
3lb26aj...vsl540  hello.1  hello    nginxdemos/hello  Running 13 seconds     Running        ol2.example.com
00hmy0p...b4kgsp  hello.3  hello    nginxdemos/hello  Running About an hour  Running        ol4.example.com
cfb8ctb...59nvwp  hello.4  hello    nginxdemos/hello  Running About an hour  Running        ol3.example.com