java12399900
java12399900

Reputation: 1671

Docker: Keep Ubuntu container running after starting?

I am trying to start a docker container using the ubuntu image:

docker container run -d --name ubuntu_assignment_4 6e4f1fe62

However as soon as I start the container it stops again.

Why does this happen and how can I ensure the container stays running?

The image I am trying to run here is: ubuntu:14.04

Upvotes: 5

Views: 6509

Answers (2)

Z4-tier
Z4-tier

Reputation: 7988

I can't say exactly what is happening without seeing the complete Dockerfile that was used to build the image, but I am pretty certain that the trouble you are having is just because whatever task that is being started inside the container is finishing and exiting.

Docker containers work by having some command assigned (using ENTRYPOINT or CMD directives in the Dockerfile, or as an argument to docker start or docker run on the command line) which is the program that is started when the container loads. The container will live for as long as that task continues to run, and once that program finishes the container will terminate.

To specify the startup entrypoint at the command line, try:

docker create -it [image] /bin/bash

Then start it like this:

 docker start -ia [Container ID]

The container will exit once the shell exits, because this is assigning the shell as the entry point.

cURL may not be installed by default. It is possible to install it using apt-get. But again, once the shell is closed, the container will stop and any changes will be lost. As a start, try creating a new directory somewhere, and then add a file called Dockerfile with this content:

FROM ubuntu:latest
RUN  apt-get update && apt-get install -y curl
ENTRYPOINT ["/bin/bash"]

That will create a new image with curl installed. then, from inside the new directory where the Dockerfile was created, use:

docker build .
docker images

which will build a new image, using the Dockerfile as the blueprint. Once the build finishes, find the image ID for the new container, and run it using:

docker run -it [image id]

Ultimately, to make Docker really useful, the typical approach is to replace that last line in the Dockerfile (ENTRYPOINT ["command"]) with something that will continue running forever (like ENTRYPOINT ["apache2"] or ENTRYPOINT ["redis"] or similar). If you have experience using regular desktop/server OS installs, and full virtual machines like VMWare or VirtualBox, just remember that Docker is very different; the way it works and the patterns used to deploy it are not the same.

Upvotes: 1

franklinsijo
franklinsijo

Reputation: 18290

If you are going to use the ubuntu:14.04 image without any modifications to it, you would not require a separate Dockerfile. And it is not possible to keep the plain ubuntu:14.04 image running as a container.

You can directly launch the container with an interactive shell using the ubuntu:14.04 image.

docker run -it ubuntu:14.04 /bin/bash

But the plain ubuntu:14.04 image does not have curl pre-installed on it.

You will need a custom Dockerfile for this.

Upvotes: 6

Related Questions