Udostępnij za pośrednictwem


My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 7

My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 1 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 2 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 3 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 4 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 5 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 6 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 7 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 8 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 9 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 10 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 11 Click Here
My Take on an Azure Open Source Cross-Platform DevOps Toolkit–Part 12 Click Here

Stopping the previously running container (myapp)

As we enter step 5 we will now begin to think about testing the running container.

But before we can do that we have to make sure that firstly stop all running containers. Once we're sure that all containers are stopped, we can run the container we just built. So this post is about stopping the container.

So between steps 3 and 4 we will stop the old running container and run the new one.
snap1

Figure 1: The big picture

Jenkins Pipeline Definition

The next 2 sections of our pipeline that need to be built.
image

Figure 2: The current work for our pipeline

Explanation for StopRunningContainersLocally.py

Line(s) Explanation
23-64 Two objects. DockerResultList maintains a list of DockerResult objects. Each DockerResult object is a running container.
9-21 Top level function that issues "docker ps" to get list of running containers. Each running container is stored as DockerResult object.
55-64 Looks for an instance of a container. In our case we are looking for myapp (Container Name) to see if it is running. findContainer() returns a DockerResult object if found, else returns None. Docker result contains all the attributes of our running container, such as container id.
66-69 Check pipeline for previous failures. If found, abort this Python script (StopRunningContainersLocally.py).
73 Calls the high level function to kick off the whole process of discovering locally running containers.
81-86 If "myapp" is running as container, issue "docker stop" command to terminate it.
88-91 As always, record the state of this running container.

Source code for StopRunningContainersLocally.py

image

Figure 3: Source code for StopRunningContainersLocally.py

Validating correct operation of StopRunningContainersLocally.py

The docker ps command reveals that myapp is running. Then, python3 StopRunningContainersLocally.py successfully stop myapp from running. Finally, we run ShowPipelineState.py to validate that the state is correctly being recorded.

blog0012

Figure 4: Testing StopRunningContainersLocally.py

Let's run a docker ps to make sure that the container is not running (myapp).

blog0013

Figure 5: Proof that the container is not running

Conclusion

We finished the work for stopping our running container. This important because our pipeline needs to do these steps:

  1. Build a new container
  2. Stop the old container that is running
  3. Run the new container

In the next post we will illustrate how to start the container that is freshly built.