4. What is the difference between docker exec and attach?

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 the below 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.

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

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

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

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x