{"id":507,"date":"2023-08-26T19:18:51","date_gmt":"2023-08-27T01:18:51","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=507"},"modified":"2023-08-26T19:18:52","modified_gmt":"2023-08-27T01:18:52","slug":"lifecycle-of-a-container-in-docker","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/lifecycle-of-a-container-in-docker\/","title":{"rendered":"Lifecycle of a container in docker"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<ul>\n<li>the container process, is attached to a process in the image (\/bin\/bash in some cases) when we&nbsp;<code>exit<\/code>&nbsp;we stop the process, so the container stops as well<\/li>\n\n\n\n<li>to keep it running, i can specific the command that i want it to run<ul><li><code>tail -f \/dev\/null<\/code>&nbsp;is the command (it will get PID 1 in the container)<\/li><li><code>-d<\/code>&nbsp;is the detach<\/li><li>in this case,&nbsp;<code>tail -f \/dev\/null<\/code>&nbsp;will keep running the container<\/li><\/ul><code>dockerrun--namejamecho-dubuntutail-f\/dev\/null<\/code><\/li>\n\n\n\n<li>to run a command in a running container (perritos is the name of the container)<code>dockerexec-itperritosbash<\/code>\n<ul>\n<li>to verify when logged in into the container&nbsp;<code>ps -aux<\/code><\/li>\n\n\n\n<li>to exit:&nbsp;<code>exit<\/code><\/li>\n\n\n\n<li>the container will keep running, because the main process has not stopped yet<\/li>\n\n\n\n<li>to stop it<\/li>\n\n\n\n<li><code>docker stop &lt;containerName&gt;<\/code>\u00a0or in mac:\u00a0<code>docker kill &lt;containerName&gt;<\/code>\n<ul>\n<li>Example:\n<ul>\n<li><code>docker stop perritos<\/code><\/li>\n\n\n\n<li><code>docker stop &lt;container_id&gt;<\/code>&nbsp;or&nbsp;<code>docker stop &lt;container_name&gt;<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>or get the pid ion the local Linux system\n<ul>\n<li><code>docker inspect --format '{{.State.Pid}}' perritos<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>and kill the process using the pid obtained\n<ul>\n<li><code>kill -9 &lt;pid&gt;<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>to run a stopped container\n<ul>\n<li><code>docker start &lt;container_name&gt;<\/code><\/li>\n\n\n\n<li><code>docker start perritos<\/code><\/li>\n\n\n\n<li>check&nbsp;<code>docker ps -a<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>to log into the previous started container (iamge name: perritosm, command: bash)\n<ul>\n<li><code>docker exec -it perritos bash<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>to exit:\n<ul>\n<li><code>exit<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>to stop it\n<ul>\n<li><code>docker stop &lt;containerName&gt;<\/code>&nbsp;or in mac:&nbsp;<code>docker kill &lt;containerName&gt;<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>to check the output of a command (it the command is\u00a0<code>tail -f \/dev\/null<\/code>\u00a0the terminal might be stucked, recommended using with bash)\n<ul>\n<li><code>dockerstart-ai&lt;container_name><\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>to restart a container\n<ul>\n<li><code>dockerrestart&lt;container_name><\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":466,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,20,1,47,76,46,8],"tags":[74,75,73,5],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/507"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":2,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/507\/revisions\/509"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/466"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}