avatarBhargav Bachina

Summary

The web content distinguishes between the docker attach and docker exec commands, explaining their usage and differences when interacting with running containers.

Abstract

The article provides a comparison between the docker attach and docker exec commands, which are used to interact with running Docker containers. It highlights that while docker attach allows for attaching a terminal to a container, it can cause the container to stop if the attachment is closed. In contrast, docker exec is used to run a new command within a container without affecting its running state, even if the executed command is exited. The article includes practical examples, such as cloning a repository, building a Docker image, and running a container in interactive mode. It also provides visual aids to illustrate the commands' effects on container status. The conclusion emphasizes the appropriate scenarios for each command: docker exec should be used for non-disruptive interaction with a running container, while docker attach is suitable for attaching to a container's existing process.

Opinions

  • The author suggests that using docker exec is preferable when you need to interact with a running container without stopping it.
  • It is implied that docker attach is more suitable for situations where you want to interact with the primary process of the container.
  • The article conveys that docker exec provides a way to execute commands without interrupting the container's lifecycle, which is beneficial for troubleshooting or running ad-hoc tasks.
  • The author's instructions to clone a specific repository and build a Docker image indicate a hands-on approach to learning, encouraging readers to follow along with the examples provided.
  • The use of docker exec with the -it flags to start a bash session is presented as a best practice for interactive container management.

Docker — attach vs exec commands

attach vs exec

In this article, we will explore the difference between docker attach and docker exec commands. Both are used to explore the running containers but there is a slight difference between these two.

clone the below repo for this article.

git clone https://github.com/bbachi/prod-ready-node-rest-api.git

Build the docker image from Dockerfile.webpack with below command and tag it with the name node-rest-api. you can list the images with command docker images.

docker build -t node-rest-api -f Dockerfile.webpack .
images list

Let’s instantiate the image in a interactive mode with the following command. bash command will override the CMD command in the dockerfile.

docker run -it -p 3090:3070 --name nodeapi node-rest-api bash

you can list all running containers with the following command. we can see container is up about a minute.

docker ps

Docker attach

This command to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. attach the running container with the following command. you need to press enter once execute this command, docker is waiting for your input.

docker attach nodeapi

once attached, you can do all the cli commands like you can check the node version, run the bundle file, exit etc..

But the problem here once you exit out of this, your container is no logger in the up status. your container is also in exit status.

what if you want to do all this stuff without exiting the running container. that’s where docker exec command comes into picture.

Docker exec

The docker exec command runs a new command in a running container. execute the below command after restarting container nodeapi.

// start the container
docker start nodeapi
// execute the exec command
docker exec -it nodeapi bash
interacting with the container

you can exit now and if you list the containers now we can see container is still running.

Conclusion

Both are used to explore the running container if you want to do that with the new command process or without exiting the running container use docker exec.

Docker
Docker Image
Containers
Software Training
Web Development
Recommended from ReadMedium