How to limit to run in the same container

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?

Hi Bill,

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.

Regards,
Sabin

Thanks, @sbasyal.

The idea is, how to start at beginning? How to let docker know to use run or exec/attach

I run CaaC (container as a command) . For example:

alias djq='docker run ryfow/jq'
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' |djq '.[0] | {message: .commit.message, name: .commit.committer.name}'

I got the result, but it generate a lot of containers, if I run many times. Any ideas to fix it?

Does this help: https://gist.github.com/ekristen/11254304?

I got the answer by myself.

with option -rm it will remove the docker container when exit

  --rm=false                 Automatically remove the container when it exits