Answers: <p>Basically, containers can only run a single process. When you run:</p>
  <pre><code>docker run -d --name nginx1 -p 49699:80 nginx
  </code></pre>
  <p>The container will start by running its <code>CMD</code>. If we look at the Dockerfile for the <a href="https://github.com/nginxinc/docker-nginx/blob/1a8d87b69760693a8e33cd8a9e0c2e5f0e8b0e3c/Dockerfile-debian.template#L98-L108" rel="nofollow noreferrer">official Nginx container</a> we can see that the <code>CMD</code> is:</p>
  <p><code>CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]</code></p>
  <p>So when you start the container it's going to run that command which will start Nginx. You also have the option of passing in your own command and overriding the <code>CMD</code> that is in the Dockerfile. We can see options for that in the <a href="https://docs.docker.com/engine/reference/commandline/run/" rel="nofollow noreferrer">Docker documentation</a> for <code>run</code>.</p>
  <pre><code>docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  </code></pre>
  <p>After the <code>IMAGE</code> name we can specify an optional <code>COMMAND</code> and a list of arguments. When you ran:</p>
  <pre><code>docker run -it --name nginx2 -p 49699:80 nginx bash
  </code></pre>
  <p>You told docker not to run the containers default command but instead run <code>bash</code>. Which is why Nginx isn't running.</p>
  <p>You could omit the bash and daemon flag like this:</p>
  <pre><code>docker run --name nginx1 -p 49699:80 nginx
  </code></pre>
  <p>Which would run the container in the foreground but you wouldn't be able to run any commands on the container, all you would be able to do is see the Nginx logs in your shell.</p>
  <p>Instead I think you are looking for this:</p>
  <pre><code>docker run -d --name nginx_test -p 49699:80 nginx
  </code></pre>
  <p>This would start a container called <code>nginx_test</code> in the background using the <code>-d</code> daemon flag. I also often use <code>--rm</code> which tells Docker to delete the container when it has stopped. After the container has started you can connect to it and run commands using this:</p>
  <pre><code>docker exec -it nginx_test bash
  </code></pre>
  <p>Which will let you connect to your running container and run additional commands.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-04-13T00:17:27.090Z
Answers: Thanks, this is exactly what I was looking for. Sounds like the reason since I've tried running `docker exec -d nginx_test nginx -g "daemon off;"` and then that container which I though was not going to run nginx never because initially I started it interactively with bash, it's now executing nginx and it's reachable from the port. So yeah, it seams that when I `docker run` with bash it overwrote the CMD to start nginx, and that's why even though I started the container again nginx was not working because I killed that original CMD that would turn it on
Answers: Eugenio.Gastelum96
Answers: CC BY-SA 4.0
Answers: 2023-04-13T00:17:49.150Z
Answers: I would add an upvote but I need more reputation to do so
Answers: Eugenio.Gastelum96
Answers: CC BY-SA 4.0
Answers: 2023-04-12T22:48:49.437Z
Answers: 2023-04-12T22:48:49.437Z
Answers: Levi
Body: <p>I have made a docker container from nginx running in background this way:</p>
  <p><code>docker run -d --name nginx1 -p 49699:80 nginx</code></p>
  <p>When I go to <code>localhost:49699</code> on a browser with cleared cache it works properly as I get the nginx vanilla index.html page</p>
  <p>But then, if I launch another container in interactive mode first, and then exit and launch it again it doesn't works. So what I mean here, is that if I first stop and remove container nginx1 mentioned above, and then I:</p>
  <p><code>docker run -it --name nginx2 -p 49699:80 nginx bash</code></p>
  <p>I create a container called &quot;nginx2&quot; and I immediately get into it's bash. The I hit <code>exit</code> without changing anything inside the container. And since it was interactive, the container stops after I exited the bash. So I the re-start it with <code>docker start nginx2</code> and it's running with the expect port forwarding as marked by <code>docker ps</code>.</p>
  <p>If I then go to <code>localhost:4699</code> it doesn't shows me nginx.</p>
  <p>So it seems that if a nginx container was created in interactive mode. Even if we get out and start the process again with <code>docker start &lt;nginx_container&gt;</code> then nginx won't actually work. As if the interactive mode the container was created with is a process that runs above the nginx processes running inside the container.</p>
  <p>I am just trying to understand what the &quot;interactive&quot; mode does to a container that is supposed to have had another process (like nginx) running and ready for port-forwarding. Does interactive mode kills nginx processes?</p>
  
Tags: <docker><containers><deployment><nginx><ports>
Title: Why does running a nginx image in docker interactive mode makes nginx not working
UserDisplayName: Eugenio.Gastelum96
ThreadText: # Why does running a nginx image in docker interactive mode makes nginx not working
  
  (Asked by: username_0 on 2023-04-09)
  
  I have made a docker container from nginx running in background this way:
  
  `docker run -d --name nginx1 -p 49699:80 nginx`
  
  When I go to `localhost:49699` on a browser with cleared cache it works properly as I get the nginx vanilla index.html page
  
  But then, if I launch another container in interactive mode first, and then exit and launch it again it doesn't works. So what I mean here, is that if I first stop and remove container nginx1 mentioned above, and then I:
  
  `docker run -it --name nginx2 -p 49699:80 nginx bash`
  
  I create a container called "nginx2" and I immediately get into it's bash. The I hit `exit` without changing anything inside the container. And since it was interactive, the container stops after I exited the bash. So I the re-start it with `docker start nginx2` and it's running with the expect port forwarding as marked by `docker ps`.
  
  If I then go to `localhost:4699` it doesn't shows me nginx.
  
  So it seems that if a nginx container was created in interactive mode. Even if we get out and start the process again with `docker start <nginx_container>` then nginx won't actually work. As if the interactive mode the container was created with is a process that runs above the nginx processes running inside the container.
  
  I am just trying to understand what the "interactive" mode does to a container that is supposed to have had another process (like nginx) running and ready for port-forwarding. Does interactive mode kills nginx processes?
  
  **Tags:** docker, containers, deployment, nginx, ports
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-04-12. Score: 2 | Accepted answer
  
  Basically, containers can only run a single process. When you run:
  
  ```
  docker run -d --name nginx1 -p 49699:80 nginx
  
  ```
  
  The container will start by running its `CMD`. If we look at the Dockerfile for the [official Nginx container](https://github.com/nginxinc/docker-nginx/blob/1a8d87b69760693a8e33cd8a9e0c2e5f0e8b0e3c/Dockerfile-debian.template#L98-L108) we can see that the `CMD` is:
  
  `CMD ["nginx", "-g", "daemon off;"]`
  
  So when you start the container it's going to run that command which will start Nginx. You also have the option of passing in your own command and overriding the `CMD` that is in the Dockerfile. We can see options for that in the [Docker documentation](https://docs.docker.com/engine/reference/commandline/run/) for `run`.
  
  ```
  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  
  ```
  
  After the `IMAGE` name we can specify an optional `COMMAND` and a list of arguments. When you ran:
  
  ```
  docker run -it --name nginx2 -p 49699:80 nginx bash
  
  ```
  
  You told docker not to run the containers default command but instead run `bash`. Which is why Nginx isn't running.
  
  You could omit the bash and daemon flag like this:
  
  ```
  docker run --name nginx1 -p 49699:80 nginx
  
  ```
  
  Which would run the container in the foreground but you wouldn't be able to run any commands on the container, all you would be able to do is see the Nginx logs in your shell.
  
  Instead I think you are looking for this:
  
  ```
  docker run -d --name nginx_test -p 49699:80 nginx
  
  ```
  
  This would start a container called `nginx_test` in the background using the `-d` daemon flag. I also often use `--rm` which tells Docker to delete the container when it has stopped. After the container has started you can connect to it and run commands using this:
  
  ```
  docker exec -it nginx_test bash
  
  ```
  
  Which will let you connect to your running container and run additional commands.
  
  > **Comments:**
  > **username_0**: Thanks, this is exactly what I was looking for. Sounds like the reason since I've tried running `docker exec -d nginx_test nginx -g "daemon off;"` and then that container which I though was not going to run nginx never because initially I started it interactively with bash, it's now executing nginx and it's reachable from the port. So yeah, it seams that when I `docker run` with bash it overwrote the CMD to start nginx, and that's why even though I started the container again nginx was not working because I killed that original CMD that would turn it on  
  > **username_0**: I would add an upvote but I need more reputation to do so  
  
  ---
  
  