Is there a difference in "connecting" to the container via Docker attach or Docker exec?

Asked

Viewed 341 times

0

My question is: is there a difference in "connecting" to a container using the instruction docker attach and docker exec -it container /bin/bash?

My container was created this way: docker run -it -p 80:80 --name dev debian:stretch /bin/bash

I even read some posts on the subject, as far as I could understand, docker attach is an instruction used to connect to the container without opening a new process, i.e., it connects to the instantiated Shell at the time of container creation.

In case I run: docker exec -it container /bin/bash will be creating a new process.

Since Docker is a fairly new world for me, I’m not sure if this understanding is correct.

1 answer

1


The docker exec is used to send a command to the container in question, when you do docker exec -it container /bin/bashan instruction is sent to the container to open a bash process and keep it open until it closes. Looking at the documentation it is possible to better understand this question.

Whereas the docker attach attaches the container stdio to your terminal, it is as if you used an ssh, this is written in documentation.

In any case, if you want to view the logs of a container use the docker logs. If you want to run a command in the container, create a dockerfile, because the container’s idea is to be reusable and if you make the command with the container running, when you restart it (possibly it will happen), you will need to do the command again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.