I'm curious about detached containers in Docker, which I think are created using the -d option in the run command. What exactly is the purpose of running a container this way? Is it just about saving space on my machine? Also, I've heard mixed things about whether you can bind a detached container to a port. And while we're at it, why do containers display two port addresses: one for local and one for the server?
3 Answers
The main point of using detached containers is that they run in the background. If you start a container without detaching it, it will stop once you close your terminal or end your session. So, by using the -d flag, you're essentially allowing it to keep running in the background without tying up your terminal. It's not about saving space; it's more about process management. And you can definitely bind ports to a detached container, so don't worry about that!
When you run a container in detached mode, it's similar to running a command in the background on Linux with a &. This way, the container won't block your terminal with log output. About port bindings, if your container is set to listen on port 80 but you want to access it via port 3000, you can use the -p flag like this: -p 3000:80. This maps your local port 3000 to the container's port 80.
Yes! Running a container in detached mode allows it to operate independently without taking control of your terminal session. And don’t worry, you can bind a detached container to a port. It's just the port mappings that require attention, and it's common to see two addresses—one will refer to the host (local) port, and the other is for the container's port.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String