I have container restarted itself 3 times and still working.
How can I know why it restarted? and how i can see the logs for each time the container has exit?
You can look for the id with:
docker ps -a
Let's say you see your container with Exited status, and you can check the logs of that container with
docker logs your_container_here
You can also follow the logs by
docker logs -f your_container_here
You can check logs of stopped container by container id with docker logs command
sample :
docker logs 99e691c271c9
and You can get container ids by running
docker ps -a (for all containers)
or
docker ps -al (for latest/last stopped container)
Related
Docker sometimes exists but still keeps running.
Started with :
docker run -v $PWD:/host --rm -it Ubuntu_2018
After "exit" :
Docker container ls; docker container ls -al
This still shows container running
How to make sure that docker container is gracefully ended?
Using the option "--rm" and exiting the docker with "exit" should do the needful. But it still sometimes remains.
After exit, "docker container ls; docker container ls -al" should not show the docker at all
How to ensure confirmed graceful exit of docker?
Not by looking at the docker container ls -al output, which as commented shows the stopped containers.
But by looking at:
the logs of a stopped container, for an application message stating the stop was graceful (so that depends more on the containerized application, less on Docker itself).
its Exit status code: docker inspect -f '{{.State.ExitCode}}' <container SHA>
For that, see "Gracefully Stopping Docker Containers" by Brian DeHamer, who reminds us that When you use docker stop or docker kill to signal a container, that signal is sent only to the container process running as PID 1.
See also "Life and death of a container" from Luis Herrera Benítez who points out the existence of "docker inspect --format='{{.State.Health.Status}}' <containerName>": if it is unhealthy... chances are your subsequent stop might not be graceful.
when calling docker ps the list is empty, although I got an id:
(dcbb6aeaa06ba43fcb.....)
My steps:
Step 1: I created an image (imagekommando) of an running js.file:
Step 2: I created a container (in background) based on my image
docker run -d --name containerkommando imagekommando
I got an id! (container-id??)
Step 3: But docker ps shows empty list:
But when I repeat Step 2, I'm told, that the container (containerkommando) already exists:
docker run -d --name containerkommando imagekommando
Could you help me, understanding the logic behind?
And how can I get the container running (by ID)?
That means that the docker container exited with an error but clean up is required. With --rm option you can tell the docker to remove the container when the container has exited.
docker run --rm .....
Also to check the reason for the container exiting...you can use
docker logs <container_id>
What probably takes place here:
docker run ... creates and starts your container
your container exits
docker ps doesn't list stopped containers (default shows just running), so it made you think that it's not there.
docker run ... fails because you are trying to create and run a container with a name that already exists.
Further reading:
What are the possible states for a docker container?
Why docker container exits immediately
In Docker, a container is automatically exited when the task is finished. You have to specify a correct entrypoint to keep your docker container up.
You can check the exited containers with the command docker ps -a. This exited container will prevent you from using the name again.
So, you may want to use docker rm <container-name> before creating your new container. In a test environement, you can also use docker system prune to clean all unused container/networks.
docker ps only shows the active containers (the running ones).
Your container most probably exited right after you started it. You can use the container ID and do docker logs <container-id> to examine the reason why the container failed.
If you want to see the stopped containers together with the running containers you can do docker ps -a to get a list of all these.
Execute
docker logs <CONTAINER ID>
to view the logs of docker container run.
I faced a similar issue found out there was space issue win my docker. After clearing space the container was able to run.
When we restart a container using 'docker restart command', docker first stops and then starts the container.
My question is when the container is stopped? I wanted to know the exit status of the container.
i dont really get what you trying to say. .but if you wanna know the exit status, you can just issue
docker ps -a
command to list all exited container with their status code,
but if you want to check it with more specific condition, you can use something like :
docker ps -a --filter 'exited=0' that mean the container exited successfully,
or
docker ps -a --filter 'exited=137' 137 code meaning a SIGKILL(9) killed them.
here s more about docker filtering reference
oh, try using some punctuation marks in your sentence next time
EXIT status can be seen by using
docker ps -a
And you can check docker logs.
docker logs CONTAINER_NAME
for more information check - Docker logs
I launch a docker container from an image with the following command:
$ docker run -d myimage /bin/bash -c "mycommand"
When "mycommand" is finished, the container is stopped (I suppose it is stopped), but it is not deleted, because I can see it with this command:
$ docker ps -a
Is there any way to restart this container with the same parameters and keep data generated by mycommand?
Yes, when the initial command finish its execution then the container stops.
You can start a stopped container using:
docker start container_name
If you want to see the output of your command then you should add -ai options:
docker start -ai container_name
PS. there is a docker restart container_name but that is used to restart a running container - I believe that is not your case.
First, $ docker ps -a shows all containers (the ones that are running and the stopped ones), so that is the reason you are not seeing your stopped container listed.
Second, you can easily start a stopped container running:
$ docker start container_name
Once the container has been started, you can run your command by:
$ docker exec -it container_name bash -c "mycommand"
The stuff you create in your container will remain inside your container as long as it exists. If you want to keep data even if your container is removed you can use a volume.
It should be
$ docker restart container_id # OR
$ docker restart container_name
From the above picture we see one container is up and other status is Exited.
When a container is exited we can still start it back up, because a container stop doesn't mean that it's like dead or cannot be used again we can very easily stop and then start containers again at some point in the future. To start a container backup we can take it's ID and then execute docker start and paste the ID end.
sudo docker start container_id
command for exited container in the above picture will be.
sudo docker start -a bba606a95392
Out put:
By the way: While restarting a container we can not replace the default command, as soon as you started up with the default command is set for the container, for example if we start our container overriding the default command let's see what happened:
Docker is thinking we are trying to start and attach multiple container at the same time.
So when we up a container and let it exit, we can start it back up again which is going to reissue the default command that was used when the container was first created. It is part of docker container lifecycle.
Unfortunately, if you restart your VM/System and it shows
mysql-tls:5.7 "docker-entrypoint.s…" 18 hours ago Exited (255) 44 seconds ago
Answer :
Start the Container
docker start mysql
or
docker start your_container_name
I am running a systemd in a docker container.
I want to get all the messages logged by jounrnald, when i use
docker logs <containername>
Right now the whole messages goe into the journal in the container and nothing appears when i use "docker logs". How can i turn this off or redirect so that everything appears in docker logs?
You need to create the container with systemd with tty allocated:
docker run -t ...
Then, inside of the container, edit /etc/systemd/journald.conf file with:
ForwardToConsole=yes
Now just restart systemd-journald inside of the container and you should be good to go:
systemctl restart systemd-journald