Frequently Asked Questions
What is the difference between docker run -d and docker run -it?▼
The -d flag runs the container in detached mode, meaning it runs in the background and prints the container ID. This is the most common mode for running services like web servers and databases. The -it flags combine -i (interactive, keeps STDIN open) and -t (allocates a pseudo-TTY), which is used when you need an interactive shell session inside the container, such as running bash or debugging. You cannot use -d and -it together.
How do I map ports in a docker run command?▼
Use the -p flag followed by hostPort:containerPort to map ports. For example, -p 8080:80 maps port 8080 on your host to port 80 inside the container. You can map multiple ports by using multiple -p flags. To bind to a specific host interface, use the format 127.0.0.1:8080:80 to only accept connections from localhost.
What restart policies are available in Docker?▼
Docker supports four restart policies: no (default, never restart), always (always restart regardless of exit status, also starts on daemon restart), unless-stopped (like always but does not restart if the container was manually stopped before the daemon restart), and on-failure (restart only if the container exits with a non-zero status). For production services, unless-stopped is generally recommended.