Every time, when I run below command, it generate a new container.
docker run busybox echo hello world
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f22b8133353 busybox:latest "echo hello world" 5 seconds ago Exited (0) 4 seconds ago furious_thompson
f5aca929e534 busybox:latest "echo hello world" 3 minutes ago Exited (0) 3 minutes ago mad_brattain
Is it possible to run in the same container? I know we can use docker exec, but docker exec need give the container id as input. How can let docker know if the container is not created, use docker run, if container is exist, use exec?
Whenever you use docker run command, it creates a new container. This is how it has been designed.
If you need to re-use to container, use docker exec or docker attach commands. If you do not want to use the container id, you can use --name parameter to give a meaningful name to your container and user that name with docker exec command.