Docker / Run Command With Published Ports
Run Command With Published Ports
Runs a container with published ports.
docker run -p <host_port>:<container_port> <image> <command> docker run -p <host_port>:<container_port> <image> <command> #!/bin/bash
# Run Command With Published Ports
docker {{[run|container run]}} {{[-p|--publish]}} {{host_port}}:{{container_port}} {{image}} {{command}} import subprocess
# Run Command With Published Ports
# Make sure to replace <placeholders> with actual values
def run_command():
cmd = [
"docker",
"{{[run|container",
"run]}}",
"-p",
"<host_port>:<container_port>",
"<image>",
"<command>"
]
try:
print(f"Executing: {' '.join(cmd)}")
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
except FileNotFoundError:
print("Error: docker not found. Please install it first.")
if __name__ == "__main__":
run_command() When To Use
When exposing an application service on a specific host port for outside access.
Pro Tip
Consider using the --detach (-d) flag for running the container in the background; this minimizes resource usage during tests.
Command Builder
Tune the command before you copy it
docker run -p <host_port>:<container_port> <image> <command> Terminal Output
Expected runtime feedback
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 my_app:latest "/start.sh" 10 seconds ago Up 10 seconds 0.0.0.0:8080->80/tcp my_app_container Anatomy of Output
Understanding the result
52bc5b857c14c 0.0.0.0:8080->80/tcp nginx:latest "/docker-entrypoint.…" Container ID Unique identifier for the container.
52bc5b857c14c 52bc5b857c14 running nginx:latest Container Status Current status of the container.
52bc5b857c14c host_port:container_port 52bc5b857c14c running Port Mapping Shows the mapped host port to the container.
Troubleshooting
Common pitfalls
Error response from daemon: Ports are already allocated
Solution: Check running containers with 'docker ps' and free the port or use another host port.
Error: No such image: nginx:latest
Solution: Pull the image using 'docker pull nginx:latest' before executing the run command.
Error: failed to create endpoint on network bridge: Bind for 0.0.0.0:8080 failed: port is already allocated
Solution: Ensure no other services are using the same host port or stop those services.
Command Breakdown
What each part is doing
-
docker - Base Command
- The executable that performs this operation. Here it runs Docker before the shell applies any redirect operators.
-
run - run|container run
- The value supplied for run|container run.
-
-p - p| publish
- The value supplied for p| publish.
-
<host_port> - host port
- The port value supplied to this command.
-
<container_port> - container port
- The port value supplied to this command.
-
<image> - image
- The value supplied for image.
-
<command> - command
- The value supplied for command.
-
-p - Command Option
- Tool-specific option used by this command invocation.
How To Run
Execution path
- Step 1
Run the command: `docker run -p 8080:80 my_app /start.sh`
- Step 2
Verify the running container: `docker ps`
Alternative Approaches
Comparable commands in other tools
Alternative containers tools for the same job.