Docker Community Forums

Share and learn in the Docker community.

Docker run cli command with parameter --publish

docker run cli command allows container creation just like docker service create --publish option. But it works in wrong order.

docker run --rm --name mytest --publish published=8080,target=80 nginx

command runs without any failure but connects localhost 80 to container 8080. However, docker service create with same options connects ports in correct order.

I know, with “docker run” command to publish a port, i have to use -p 8080:80 option but “docker run” command also accepts the “–publish published=8080,target=80” parameter without giving any error, warning.

#docker run --rm -d --name mytest --publish published=8080,target=80 nginx
#curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused

#docker service create --name mytest --publish published=8080,target=80 nginx
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
#curl localhost:8080
Welcome to nginx!

Either docker run command should produce an error/warning or should work as in service creation. Any comments?