user1584421
user1584421

Reputation: 3863

Docker fails to start container in detached mode

I have a Google VM that i am trying to start a container, that will start up a web server.

Since i want to be able to do other things with my terminal, i tried starting the container in detached mode.

This is what i typed:

sudo docker run -d -p 5001:5001 -v $(pwd):/mnt/translation -w="/mnt/translation" -e "TERM=xterm-color" f_translate

After this, i get back a string

9f8cc86f1e4a262bff8ff4f40f1a9036c686472b0ec3fded84980a60d26f6980

However, i couldn't connect to the server with docker exec.

I typed docker ps, to see all running containers, but there are none running.

EDIT: This is the result of the docker ps -a:

CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
9f8cc86f1e4a   f_translate    "/bin/bash"   28 minutes ago   Exited (0) 28 minutes ago             reverent_sinouss
i
faeec783edce   f_translate    "/bin/bash"   37 minutes ago   Exited (0) 37 minutes ago             adoring_chaum
1b96a5117aef   f_translate    "/bin/bash"   25 hours ago     Exited (129) 24 hours ago             xenodochial_noet
her
b364de5733b9   f_translate    "/bin/bash"   41 hours ago     Created                               nice_lumiere
5b042e459548   f_translate    "/bin/bash"   41 hours ago     Exited (0) 41 hours ago               hardcore_elbakya
n
ea97b56d6822   f_translate    "/bin/bash"   42 hours ago     Created                               practical_lumier
e
2392c2b074a1   f_translate    "/bin/bash"   42 hours ago     Created                               hardcore_easley
e24c579f3094   f_translate    "/bin/bash"   42 hours ago     Created                               inspiring_merkle
3e4e412c551b   f_translate    "/bin/bash"   42 hours ago     Created                               crazy_engelbart
8c198931c560   f_translate    "/bin/bash"   42 hours ago     Exited (0) 41 hours ago               hardcore_cannon
67c5ad80b074   f_translate    "/bin/bash"   42 hours ago     Exited (0) 42 hours ago               dreamy_margulis
b2b8773a9d77   a2f276018a81   "/bin/bash"   42 hours ago     Exited (127) 42 hours ago             jolly_greider
2c3c74ee073c   a2f276018a81   "/bin/bash"   42 hours ago     Exited (0) 42 hours ago               pensive_chandras
ekhar
8563a35a96a7   cq_image       "/bin/bash"   47 hours ago     Exited (127) 46 hours ago             gallant_rubin
44431d47308c   cq_image       "/bin/bash"   2 days ago       Exited (129) 2 days ago               peaceful_tesla

When i type docker logs 9f8cc86f1e4a262bff8ff4f40f1a9036c686472b0ec3fded84980a60d26f6980, i get nothing back.

Upvotes: 1

Views: 724

Answers (2)

F1ko
F1ko

Reputation: 4274

It seems as if there is a need to explain what containers do:

Containers have been designed to do one job. You tell them which command to execute and they will do that until it is done. Usually this is specified within a Dockerfile, in there you can define ENTRYPOINT and CMD to tell the container what to do when starting up. The container will then do that particular job until its done and then shut down.

And that's the case with your container. Your container executes the command /bin/bash as can be seen in the COMMAND column of the docker ps -a output. So it actually is doing what it is supposed to do. It simply is executing the bash and since the command finished at that point it is shutting down, therefore you cannot connect to it anymore.

To be able to exec into a container it must be up and running, to do so you will have to change the entrypoint of your container. You can either do so by building a new image via a Dockerfile or you can overwrite it on the fly with the docker run command. For debugging purposes, especially when wanting to exec into the container manually, you are most likely going to choose a command that does nothing but runs forever, perhaps something like sleep infinity.

TL;DR

With docker run after mentioning the image name you can define which command the container shall execute when starting up. Execute the following command to ensure that your container does not exit immediately:

sudo docker run -d -p 5001:5001 -v $(pwd):/mnt/translation -w="/mnt/translation" -e "TERM=xterm-color" f_translate /bin/sh -c sleep infinity

Upvotes: 0

quentino
quentino

Reputation: 1201

You're run the container only with the bash command. This command returns exit code 0 immediately after the run. Then container finishing the work.
You have to run the container with some process that not returns the exit code right after a run.
This is a normal behavior that container finishing work when the job is finished with the success or with the error.

Upvotes: 2

Related Questions