In the first article, “Creating a Docker Swarm Cluster on Azure Container Service,” we discussed creating a Docker Swarm cluster on Azure Container Service (ACS). Each virtual machine (master or agent) has Docker running when the Swarm is created. In this continuation article, we shall discuss using Docker on the Swarm created on the Azure Container Service. We shall discuss using both standalone Docker and a Docker Swarm mode service. This article has the following sections:
- Setting the Environment
- Listing Docker Info
- Running a Hello World Docker Image
- Running a Nginx Docker Image
- Listing Docker Containers
- Accessing the Nginx Docker Container in a Browser
- Removing a Docker Container
- Initializing the Docker Swarm Mode
- Creating a Docker Service
- Listing Docker Services
- Listing Docker Service Tasks
- Listing Docker Containers for Docker Service
- Exploring Logs Generated by a Docker Service Container
Use the same environment as in the first article, “Creating a Docker Swarm Cluster on Azure Container Service.” Create a Swarm cluster on ACS, as discussed in the article. In this article, we shall use the Swarm cluster consisting of three master nodes and one agent node to run a standalone Docker container and a Docker Swarm mode service.
Start Cloud Shell as discussed in the article “Creating a Docker Swarm Cluster on Azure Container Service.” Connect to a Swarm master VM using a SSH RSA private key, user name, and Public IP Address of the master, as discussed in the earlier article also. The SSH command to connect to the master VM is as follows; the SSH RSA private key, user name, and Public IP address would be different for different users.
ssh -i /home/deepak/.ssh/id_rsa -p 2200 [email protected]
A connection gets established and a command prompt for the master VM gets displayed.
[email protected]:~$ ssh -i /home/deepak/.ssh/id_rsa -p 2200 [email protected] Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 3.19.0-65-generic x86_64) * Documentation: https://help.ubuntu.com/ System information as of Tue Oct 10 22:40:17 UTC 2017 System load: 0.27 Processes: 131 Usage of /: 4.2% of 28.80GB Users logged in: 0 Memory usage: 2% IP address for eth0: 172.16.0.5 Swap usage: 0% IP address for docker0: 172.17.0.1 Graph this data and manage this system at: https://landscape.canonical.com/ Get Cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud New release '16.04.3 LTS' available. Run 'do-release-upgrade' to upgrade to it. Last login: Tue Oct 10 22:40:17 2017 from 220.127.116.11
Three master nodes were configured in the Swarm provisioned in the article “Creating a Docker Swarm Cluster on Azure Container Service.” The number of master VMs may be 1, 3, or 5. When running multiple master VMs, the SSH command to connect to each of the master VMs is the same except for the SSH port, which is 2200 for the 1st master, 2201 for the 2nd master, 2202 for the 3rd master, 2203 for the 4th master, and 2204 for the 5th master. With three master VMs, the SSH ports used are 2200, 2201, and 2202. To connect to a 2nd master, the SSH command is as follows:
[email protected]:~$ ssh -i /home/deepak/.ssh/id_rsa -p 2201 [email protected]
And, the SSH command to connect to the 2nd master is as follows:
[email protected]:~$ ssh -i /home/deepak/.ssh/id_rsa -p 2202 [email protected]
The Docker daemon is running at 172.16.0.5:2375. To display system-wide information, run the docker info command. Output (partial) from the command is listed:
[email protected]:~$ docker -H 172.16.0.5:2375 info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Role: primary Strategy: spread Nodes: 1 swarm-agent-D50F2361000001: 10.0.0.5:2375 Is Manager: false Kernel Version: 3.19.0-65-generic Operating System: linux Architecture: amd64 CPUs: 2 Total Memory: 7.145GiB
Each of the virtual machines in an ACS Swarm is a Docker host and a standalone Docker container may be run on any of the Swarm VMs. As an example, run the “hello-world” Docker image on a master VM.
[email protected]:~$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 5b0f327be733: Pull complete Digest: sha256:b2ba691d8aac9e5ac3644c0788e3d3823f9e97f757f01d2 ddc6eb5458df9d801 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/ [email protected]:~$
The Docker image hello-world is running on the Swarm master VM and not on the Swarm. The master VM is just one of the VMs in the Swarm. The Docker Swarm is listening to the endpoint 172.16.0.5:2375. To run Docker containers on the Swarm, the Swarm endpoint must be provided, as we shall discuss in the next section. When a Docker container is run on a Swarm, the Swarm master assigns a Swarm agent to run the Docker container on.
The hello-world Docker image used in the preceding section is a very basic Docker image with not much Dockerization and just outputs a message. In this section, we shall run a standalone container with Docker image nginx for a Nginx server. The following command creates a Docker container called hello-nginx and exposes port 80 on the host.
docker run --name hello-nginx -d -p 80:80 nginx
The output from the docker run command is listed:
[email protected]:~$ docker run --name hello-nginx -d -p 80:80 nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx bc95e04b23c0: Pull complete 110767c6efff: Pull complete f081e0c4df75: Pull complete Digest: sha256:004ac1d5e791e705f12a17c80d7bb1e8f7f01aa7dca7deee 6e65a03465392072 Status: Downloaded newer image for nginx:latest b3679edba090ff41482f3754b4d852d295985b501f6fa2a08b46c04bd0014c2e
The preceding docker run command runs a Docker container on the Swarm master VM. The docker run command may be run on the Swarm by providing the Swarm endpoint IP and port as follows:
[email protected]:~$ docker -H 172.16.0.5:2375 run --name hello-nginx -d -p 80:80 nginx ddcbc335988f7b5dba762b436bfde4c22d01144d67313813ba140a5391bac8df
List the Docker containers on a master VM with the docker ps command. The hello-nginx Docker container gets listed. A Swarm manager container, which is not user created but is a system container, is created when a Swarm is created.
|Editor’s Note: Please note that many of the following code listings are too wide to display correctly. Please scroll the code box to the right to see the entire listing.|
[email protected]:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3679edba090 nginx "nginx -g 'daemon..." 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp hello-nginx f82ebb6efa4f swarm:1.1.0 "/swarm manage --r..." 5 minutes ago Up 3 minutes 0.0.0.0:2375->2375/tcp containers_swarm_1
The Docker containers on the Swarm endpoint may be listed as follows:
[email protected]:~$ docker -H 172.16.0.5:2375 ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ddcbc335988f nginx "nginx -g 'daemon..." 36 seconds ago Up 35 seconds 10.0.0.5:80->80/tcp swarm-agent-D50F2361000001/hello-nginx
The Docker container is running on a Swarm agent as indicated by the Docker container name swarm-agent-D50F2361000001/hello-nginx.
As discussed earlier, when a Docker container is run on the Swarm using the Swarm endpoint, the Swarm master schedules the container on one of the Swarm agents in the Swarm. To access the Nginx server running in Docker container on the Swarm endpoint, the Swarm agent Public IP must be used. The Swarm we have used has only one Swarm agent. The Swarm agent Public IP may be obtained from the Resources listing, as shown in Figure 1.
Figure 1: Swarm Agent Public IP Address
Alternatively, click on the Swarm agent Load balancer in the Resources, as shown in Figure 2.
Figure 2: Swarm Agent Load Balancer
In the Swarm agent details page obtain the IP Address (see Figure 3).
Figure 3: IP Address of Swarm Agent
The IP Address also may be obtained from the Swarm agent load balancer details page, as shown in Figure 4.
Figure 4: Swarm Agent Public IP on the Load Balancer Page
In a browser on a local machine, navigate to the Public IP address of the Swarm agent. The Nginx server welcome page gets displayed, as shown in Figure 5.
Figure 5: Invoking Nginx Server in a Browser
To remove a Docker container, run the docker rm command. A running Docker container cannot be removed. First, stop the Docker container hello-nginx running on the Swarm.
[email protected]:~$ docker -H 172.16.0.5:2375 stop hello-nginx hello-nginx
Subsequently, the stopped container may be removed with docker rm.
[email protected]:~$ docker -H 172.16.0.5:2375 rm hello-nginx hello-nginx
As discussed before, the Azure Container Service Swarm does not have the Swarm mode enabled by default. The Swarm mode has to be initialized with the docker swarm init command.
[email protected]:~$ docker swarm init --advertise-addr 18.104.22.168 Swarm initialized: current node (ia4uj7431stu8y0j5h2yvdp27) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5ksj7uqccgv1vnnmie09qmt191ldpupof4ihg4vhm083c20xsh -6uyq5653uo62e521bcoatzyyz22.214.171.124:2377
To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
List the Swarm mode nodes with the docker node ls command. The Swarm manager node gets listed.
[email protected]:~$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ia4uj7431stu8y0j5h2yvdp27 * swarm-master-D50F2361-0 Ready Active Leader
Having initialized the Docker Swarm mode, create a Docker service with the docker service create command. As an example, create a Docker service consisting of two replicas using Docker image alpine and ping docker.com.
[email protected]:~$ docker service create --replicas 2 --name helloworld alpine ping docker.com tg1ywqiyei3jw9prco6890ol1
As another example, create a Docker service with the Docker image tutum/hello-world and expose the service on the host on port 8080.
[email protected]:~$ docker service create > --name hello-world > --publish 8080:80 > --replicas 2 > tutum/hello-world 3e38c1595cddg0ckmoyasrnnu
List the Docker services with the docker service ls command. The two services created in the preceding section get listed.
[email protected]:~$ docker service ls ID NAME MODE REPLICAS IMAGE PORTS 3e38c1595cdd hello-world replicated 2/2 tutum/hello-world:latest *:8080->80/tcp tg1ywqiyei3j helloworld replicated 2/2 alpine:latest
List the Docker service tasks for the helloworld service based on the Docker image alpine.
[email protected]:~$ docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 0mal82mskbge helloworld.1 alpine:latest swarm-master-D50F2361-0 Running Running 33 seconds ago iqz0bqkldxs2 helloworld.2 alpine:latest swarm-master-D50F2361-0 Running Running 32 seconds ago
List the Docker service tasks for the hello-world service based on the tutum/hello-world Docker image.
[email protected]:~$ docker service ps hello-world ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS yok48ja4o835 hello-world.1 tutum/hello-world:latest swarm-master-D50F2361-0 Running Running 38 seconds ago x5tvcuglwzd0 hello-world.2 tutum/hello-world:latest swarm-master-D50F2361-0 Running Running 38 seconds ago
The Docker containers for a Docker service on a Docker host in the Swarm may be listed with the docker ps command.
[email protected]:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b6b069102300 tutum/hello-world:latest "/bin/sh -c 'php-f..." 6 minutes ago Up 6 minutes 80/tcp hello-world.2.x5tvcuglwzd05olktbf3tpfqe 62e26d4bc0ff tutum/hello-world:latest "/bin/sh -c 'php-f..." 6 minutes ago Up 6 minutes 80/tcp hello-world.1.yok48ja4o835nc7cog8hhjlr7 db4827da350b alpine:latest "pingdocker.com" 7 minutes ago Up 7 minutes helloworld.2.iqz0bqkldxs2sg8ny50je7e7y 92dab9f7ae97 alpine:latest "pingdocker.com" 7 minutes ago Up 7 minutes helloworld.1.0mal82mskbgecbcwh3op18nrr
Docker containers for the hello-world and helloworld services get listed.
The logs generated, if any, in a Docker container for a Docker service may be listed with the docker logs command. As an example, obtain the container ID for an alpine image based service and list the logs:
[email protected]:~$ docker logs db4827da350b PING docker.com (126.96.36.199): 56 data bytes
As the output indicates, the docker.com domain gets pinged and 56 bytes of data are exchanged.
In this article, we discussed using Docker in a Docker Swarm cluster on Azure Container Service. We used standalone Docker containers and Docker services on the Swarm. A Swarm cluster on ACS exposes a Swarm endpoint to run a standalone Docker container. When Docker containers are run on the Swarm endpoint, the Swarm runs standalone Docker containers on Swarm agent/s. The Swarm mode is not enabled by default and has to be initialized on a Swarm master VM.