Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker getting exited just after start

So before starting the question here are my understanding for the docker.

  1. Docker has 3 components
  2. Images
  3. Containers
  4. Dockerfile

Now Images are the ones on which the Containers are created and Dockerfile is like a flow what to do. In simple words Images are Classes and Containers are Objects of Images.

Now I don't want to take the approach of the Dockerfile where you specify the steps to perform while creating the container.

I want to install some of the basic entities over Linux like MongoDb,Redis etc and run my server over them.

So I started like this:

  1. I downloaded the Ubuntu image from Docker Hub via docker pull ubuntu which returned me 18261df960118..7a16(big hex key)

  2. Now I have to create a container for this image, to achieve that I did:

    docker create -h abc.com --name abc.com 18261df960118..7a16

which returned me the id of the container.

  1. In order to go into the container I have to first start it and then get attached to it, so for that here are the commands docker start containerId followed by docker attach containerId.

But every time it is saying:

You cannot attach to a stopped container, start it first.

like image 992
Ankur Verma Avatar asked Jul 20 '17 13:07

Ankur Verma


People also ask

Why container is getting exited?

When the process running inside your container ends, the container will exit. Here are a couple of examples: You run a container, which runs a shell script to perform some tasks. When the shell script completes, the container will exit, because there's nothing left for the container to run.

How do I keep my Docker container from exiting?

The simplest way to keep the container running is to pass a command that never ends. We can use never-ending commands in any of the following ways: ENTRYPOINT or CMD directive in the Dockerfile. Overriding ENTRYPOINT or CMD in the docker run command.

What does exited mean in Docker?

Exited: The command that started the container has exited. This may be because of an exception, the docker stop command, or because the command completed. An exited container isn't consuming any CPU or memory.


1 Answers

Edit: In my original post I mention: "try to think like with VMs". I recently fell into this, which says not to do so:

Stop thinking about a container as a mini-VM and instead start thinking about it as just a process.

also, worth-reading article: Containers are not VMs


Original post:

The logic with Docker containers is that they are supposed to have a service up and running. If this service stops, they exit and go to "stopped" state. (As you learn more about Docker, you will understand how this works and you will be able to use ENTRYPOINT and CMD). But let's skip this for a while and try to think like with VMs, run a new container and get inside to type some commands...

this succeeds:

docker container create -it --name test ubuntu
445cad0a3afea97494635361316e5869ad3b9ededdd6db46d2c86b4c1461fb75
$ docker container start test
test
$ docker container exec -it test bash
root@445cad0a3afe:/# your are inside, you can type your commands here!

why yours failed...

when you created the container, you didn't use the -i flag which helps Keep STDIN open even if not attached. This practically means that when the container starts, it uses the CMD set in the official ubuntu Dockerfile, which is bash and then exits immediately.

docker attach VS docker exec --it bash

You can test this with an image like nginx. If you run a new nginx container and try to attach to it, you will see that logs from nginx are being printed out and you are not able to type any command in the shell. This happens because the CMD of the image is the following:

# Define default command.
CMD ["nginx"]

To be able to "attach" to a container like that but also be able to use the shell (some others may also mention this like doing something equivalent to ssh to the container), you will have to run:

docker container exec -it your_container_name bash

I suggest you also read:

  • Is it possible to start a shell session in a running container (without ssh)

  • Docker - Enter Running Container with new TTY

  • How do you attach and detach from Docker's process?

  • Why docker container exits immediately

  • ~jpetazzo: If you run SSHD in your Docker containers, you're doing it wrong!

like image 73
tgogos Avatar answered Oct 06 '22 22:10

tgogos