Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop and remove docker container #874

Merged
merged 4 commits into from
Sep 28, 2023

Conversation

tomargovind
Copy link
Contributor

@tomargovind tomargovind commented Sep 25, 2023

Closes: #708

Describe the changes you've made

Earlier after stopping keploy, it used to stop the docker container but not remove it. Now, the container will be stopped and removed.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Please let us know if any test cases are added

Please describe the tests(if any). Provide instructions how its affecting the coverage.

Describe if there is any unusual behaviour of your code(Write NA if there isn't)

A clear and concise description of it.

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes

@github-actions
Copy link

github-actions bot commented Sep 25, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you and congratulations 🎉 for opening your very first pull request in keploy

@tomargovind
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve the mentioned issues

// StopUserApplication stops the user application
func (h *Hook) StopUserApplication() {
if h.userAppCmd != nil && h.userAppCmd.Process != nil {
h.logger.Debug("the process state for the user process", zap.String("state", h.userAppCmd.ProcessState.String()), zap.Any("processState", h.userAppCmd.ProcessState))
if h.userAppCmd.ProcessState != nil && h.userAppCmd.ProcessState.Exited() {
return
}

// Stop Docker Container and Remove it
h.StopAndRemoveDockerContainer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to stop docker only if we know that application is ran via docker. In native case this function shouldn't be executed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also if the user runs container and gives the container name or id then also we shouldn't stop the container.

@@ -25,6 +25,12 @@ import (
"go.keploy.io/server/pkg/models"
)

type DockerInfo struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct can be part of client package . Keeping it in this Package may cause circular dependency in future .

@@ -334,13 +342,48 @@ func (h *Hook) findAndCollectChildProcesses(parentPID string, pids *[]int) {
}
}

// Stop and Remove the docker container
func (h *Hook) StopAndRemoveDockerContainer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a function of dockerInfo struct or dockerClient instead of hooks. All docker related things can be moved to client package itself.

@@ -202,6 +210,7 @@ func (h *Hook) processDockerEnv(appCmd, appContainer, appNetwork string) error {
case e := <-messages:
if e.Type == events.ContainerEventType && e.Action == "create" {
// Fetch container details by inspecting using container ID to check if container is created
h.dockerDetails.containerID = e.ID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use setter and getter functions instead of assigning it directly


container, err := dockerClient.ContainerInspect(context.Background(), containerID)
if err != nil {
fmt.Println(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logger instead of print statements

container, err := dockerClient.ContainerInspect(context.Background(), containerID)
if err != nil {
fmt.Println(err)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is err not being returned..?


err = dockerClient.ContainerRemove(context.Background(), containerID, removeOptions)
if err != nil {
fmt.Println(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use log statements

Signed-off-by: 404fixer <govindtomar94@gmail.com>
Signed-off-by: 404fixer <govindtomar94@gmail.com>
Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else looks good to me. Please correct the logger statement

// Stop Docker Container and Remove it if Keploy ran using docker
if len(h.idc.GetContainerID()) != 0 {
err := h.idc.StopAndRemoveDockerContainer()
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an error log saying failed to stop container/ remove container , zap the error and suggest user to delete it manually

Signed-off-by: 404fixer <govindtomar94@gmail.com>
Signed-off-by: 404fixer <govindtomar94@gmail.com>
Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it!

Copy link
Contributor

@re-Tick re-Tick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@re-Tick re-Tick merged commit 3948ecb into keploy:main Sep 28, 2023
7 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug]: docker containers doesn't stop/get remove after running the application with Keploy
3 participants