When I run a hello-world container, it prints the message and exits. The status of the container will then be marked as Exited If I start the container again, the message do not get printed.
But when I run a nginx container and stop the container, the status will be changed to Exited. If I start the container again using the start command, the nginx process starts again. How is the behavior different.
docker run hello-world
docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1dcd009d1fd3 hello-world "/hello" 15 seconds ago Exited (0) 13 seconds ago focused_pike
When you start the exited container it is by default not attached to the stdout / stdin of the process. You can see the output in two ways:
You can attach to the container to see the output using the --attach or -a option to the start command like: docker start -a 1dcd009d1fd3
You can view the output using the logs command such as: docker logs 1dcd009d1fd3
Related
I installed Docker and then ran docker run hello-world which appears to run correctly with that "Hello from Docker message".
However, on subsequent docker container start cool_robinson, I do not see the same terminal output as I saw the first time:
pi#Pavillion:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46f651f9edf8 hello-world "/hello" 18 hours ago Exited (0) 10 minutes ago cool_robinson
pi#Pavillion:~$ docker container start cool_robinson
cool_robinson
pi#Pavillion:~$
I am not familiar with C, but isn't the main function in hello.c executed every time the container is run?
When you run the container with docker run, it starts the container in the foreground and so that's is why you see the container logs in the terminal.
docker run hello-world
So it stop as its just print hello message and die.
and when you start the same container again, it print message but in detach mode and does not allocate tty, to checks logs you need to run two command.
docker container start cool_robinson
docker logs cool_robinson
You will see hello message two times.
Or you can try with `attache mode to run in foreground with container command as mentioned by #Leopal.
docker container start -a cool_robinson
I build and run a docker image with
docker run --detach --name api rkevinburton/myagsourceapi
But when I 'docker ps -a' I get a message that this container has exited.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee956fbf1d7f rkevinburton/myagsourceapi "/bin/bash" 10 seconds ago Exited (0) 8 seconds ago api
So I want to find out why it exited. So I issue the command
docker logs ee
But this command returns nothing. As the docker host is a Windows machine I looked on ~\AppData\Local\Docker but the information in the log*.txt or install-log.* didn't seem to help me any. How can I get more information on why the container 'Exited'?
Docker containers exit when their main process finishes. That is why you don't get any logs.
The docker logs command batch-retrieves logs present at the time of execution.
(see: https://docs.docker.com/engine/reference/commandline/logs/)
An example:
The following will create a new container and start it:
docker run -d --name=my_test_container alpine ping -c 20 127.0.0.1
[----run----] [--------name--------] [image][-----command------]
Try to use the following, before ping command stops:
docker logs my_test_container
docker logs --follow my_test_container
The first one shows what has been printed out by ping (until then) and the second one gives you logs as ping prints out new lines.
After 20 ping requests, the ping command finishes and the container stops.
ubuntu#ubuntu:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8f371000146 alpine "ping -c 20 127.0.0.1" 29 seconds ago Exited (0) 9 seconds ago my_test_container
I run the tomcat server 8 in docker, it runs correctly when I run using docker run command and the command prompt don't come back, now I press ctrl+c and prompt come back, but now tomcat server has stopped so when I check on http://localhost:8080 the tomcat page not appear. So how to make it run continuously or so called system level process in container.
Here is my docker file. Help me with this
FROM scratch
FROM ubuntu:16.04
RUN mkdir /opt/java8
RUN mkdir /opt/tomcat8
ENV JAVA_HOME /opt/java8
ENV CATALINA_HOME /opt/tomcat8
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
ADD jdk1.8.0_112 /opt/java8
ADD apache-tomcat-8.0.38 /opt/tomcat8
ADD M_UserTP.war /opt/tomcat8/webapps
EXPOSE 8080
CMD ["catalina.sh", "run"]
Running startup.sh not even help me.
docker run distinguishes between a foreground and a detached mode (source). Your troubles are caused by the fact that you run the container in foreground mode. To run it in the background as a daemon, use the detached mode:
docker run -d [IMAGE] [COMMAND]
This starts the container in the background, and keeps it running as long as the process inside continues to run. You can see what's going on inside the container by either looking at its logs (docker logs [CONTAINER_ID]) or by jumping on a shell inside the container (docker exec -it [CONTAINER_ID] /bin/sh).
If you are done working with the container, use docker stop [CONTAINER_ID] to stop it. If you are unsure whether you have a container running, use docker ps.
Instead of ctrl + c, you should type ctrl + p and ctrl + q to detach
There are 2 ways to get into a running container:
1) Attach to the process/container
Used to attach your terminal’s standard input, output, and error to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
To stop a container, use CTRL-c. This key sequence sends SIGKILL to the container. You can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.
# docker run -it ubuntu:15.0 /bin/bash
root#9391b08536ae:/#
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9391b08536ae ubuntu:15.0 "/bin/bash" 6 seconds ago Up 6 seconds 0/tcp confident_albattani
# docker attach 939
root#9391b08536ae:/#
root#9391b08536ae:/# exit
exit
root#labadmin-VirtualBox:~/RAGHU/python# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2) Execute the container
The docker exec command runs a new command in a running container.
# docker exec -it 939 /bin/bash
root#9391b08536ae:/# exit
exit
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9391b08536ae ubuntu:15.0 "/bin/bash" 25 seconds ago Up 25 seconds 0/tcp confident_albattani
Hope this helps.
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 have a docker container running
sudo docker ps -a
result:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cd8d5166717 32bit/ubuntu:14.04 /bin/bash 15 hours ago Up About an hour hadoop-test
How can I bring this running container again to -it mode and grab the terminal of ubuntu system ?
You can attach it:
docker attach 0cd8d5166717
Type Ctrl-PCtrl-Q to detach it again.