What's the deal with detached containers in Docker?
Asked by
kept the wrapper
I'm curious about the purpose of using detached containers when running Docker commands with the -d flag. Is it mainly for saving resources on my machine? Also, I heard that you can't bind a detached container to a portโcan anyone clarify that? And while we're on the topic of ports, I've noticed that containers display two port addresses: one for the local machine and one for the server. What's the reason behind that?
Answers
Yes! Think of a detached container like stashing changes in Gitโyou donโt have to worry about it for now. And regarding ports, only one program can bind to a specific port at a time, so itโs important to set your ports correctly to avoid conflicts.
Reply to this answer
Using the -d flag for detaching a container lets it run in the background, which keeps it from locking up your terminal with log outputs. Think of it as running a command with a '&' at the end in Linux. It doesnโt save space, but it makes managing processes easier! Also, you *can* bind a port to a detached container, so thatโs a common misconception. Just use the -p flag for that!
Reply to this answer
Detaching a container means it wonโt interfere with your terminal session, so you can continue to use your terminal while the container runs in the background. And as for ports, when you see the two addresses, itโs usually the local port that directs traffic to the container's port. For instance, if your container is set up to listen on port 80 but you want to access it via port 3000 in your browser, you'd use the -p flag like this: -p 3000:80. Itโs all about routing traffic!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions