0

i'm trying to run the start command in docker after creating a container. for example :

$ docker create busybox echo hi there

it gives me the id of that container like 4e59d0fe8584bb4dcaf44dbce100253b6767bf51546edc27f29f39f52ed57957

and when i try to start that container without any flags like: -a flag it works, but it only gives me that id back again. but when i try to show the output using the attach -a flag, actually nothing happened, even it didn't give me an error, simply the command still running without anything happened.

i also couldn't kill the command and stop the execution by Ctrl+c, so the only option i had to close the terminal

enter image description here i tried to make the problem clear as possible as i can

Elfeqy
  • 61
  • 8

2 Answers2

0

You can run the image via this command:

docker run -it busybox

It goes you to shell environment and you have -i (interactive) -t (tty) terminal, which means you see the terminal.

Saeed
  • 3,255
  • 4
  • 17
  • 36
  • oh sorry, my bad. the command was busybox not hello world, i'm gonna edit it now – Elfeqy Mar 07 '21 at 23:01
  • That's no problem, you should do the same command. Just replace `ubuntu` with `busybox`, so that you go into `sh` shell of the busybox container. – Saeed Mar 07 '21 at 23:05
0

The default CMD(PID1) for busybox image is sh, see this.

For docker create busybox echo hi there, the COMMAND becomes echo hi there. This means after container starts, it will first execute echo hi there, then as PID1 exit, the container exit too. If you use docker ps, you won't find your container, you could just find your exited container with docker ps -a.

So,

  • If you intend to run a one time task, then as the container finish its task, it's normal you could not enter into container anymore.

  • If you intend to run a daemon task, leave the container service there, you should choose a command which won't finish after run, then your container will still there.

For your case, to quick let you understand it, you could use next to have a quick understanding, use tail -f /dev/null to let the container not exit:

# docker create busybox sh -c "echo hi there; tail -f /dev/null"
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root@shlava:~# docker start 840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root@shlava:~# docker logs 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26
hi there
root@shlava:~# docker exec -it 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26 /bin/sh
/ #
atline
  • 28,355
  • 16
  • 77
  • 113