0

When I run or start a Docker container, it will not stay running. Docker start will just return the name of whatever container I gave it, but wont actually do anything. Docker run (ex $ docker run -p 8080:80 --name hello -d hello-world) will create it but it will exit immediately.

If I run docker ps after one of these, it will show nothing listed as currently running. If I run docker ps -a, it will show all of my containers and show the one that I just attempted to run having exited a few seconds ago.

Is this common and how do I get my containers to stay running? I am trying to learn how to use Docker and it has been one of the worst experiences. Thank you for any help or suggestions

farmer1010
  • 41
  • 1
  • 1
  • 6
  • 1
    Check the container logs if there is an error in starting the process inside the container. (`docker logs ...`). – Henry Aug 07 '20 at 16:12
  • 1
    **Is this common and how do I get my containers to stay running?** Is your docker running a long running process, like a web server, database, or just a script like the hello world shows? Check self-guided tutorials https://docs.docker.com/get-started /resources/ – abestrad Aug 07 '20 at 16:22

7 Answers7

2

A docker container exits when its main process finishes. The hello-world main process just prints some text and exits, so container exits too.

You can run this command straightly to see it's text:

docker run hello-world

If you want a running container, maybe you can try a nginx demo:

docker run --name nginx-demo -p 8080:80 -d nginx

then you can visit http://localhost:8080 using your web browser.

Gavin
  • 47
  • 5
1

In your command, you specify the -d flag (aka detach), which means Run container in background and print container ID (from Docker docs). See more discussion about this here: Docker container will automatically stop after "docker run -d"

docker run -p 8080:80 --name hello -d hello-world

If you run it without the -d flag, it should run in the foreground and send output to your terminal

docker run -p 8080:80 --name hello hello-world

You don't see it running in docker ps -a because that container just executes the hello-world script and exits. If the container starts a long running process then you'll be able to find it in docker ps -a. To verify this, you can try running the nginx demo containers (e.g. nginx-hello) which serve up 'hello world'/demo pages.

honk
  • 616
  • 4
  • 9
1

To know what's wrong with your container use (docker logs (your container name)) command.

then you can sort it out what went wrong with your container

1

Docker containers are generally used to run applications/processes in an isolated environment.

When you run the hello-world image, it creates a container which has only purpose of printing out the name using standard output. That is the only process that ran and the container was done with its work. That is why you see nothing when done docker ps.

In order to keep a container running, you need to have a process inside that container that will run (for example: a server, database, application etc.)

Try creating a container form mysql image, and then check the running container.

aryanveer
  • 628
  • 1
  • 6
  • 17
0

Is this common and how do I get my containers to stay running?

What happen when you start a Docker container ?
By default, it executes the command/the entrypoint specified in the Dockerfile image. Generally that command or the entrypoint is a script or a program located in the image.
When that script/program exits, the container exits too. That's all.

To keep a container alive, the script/program has to stay running.
You start an hello image container, a "hello" container says "hello" and exits. That may be a script as simple as :

#!/bin/sh
echo "hello"

So that is expected to finish and exit the container. Run a database or a web server and you will see a different behavior. The script/program keeps running... while you don't stop that. So the container also stays running while you don't stop that.

To experiment, you can run your hello-world container with an endless command :

docker run -p 8080:80 --name hello -d hello-world --entrypoint tail -f /dev/null

You will see that the container stays running.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

I had a similar issue when i tried to start a mysql container. I was directing the data to a directory, mysql_data, but when I tried to start the container it would exit immediately. Having checked the logs I discovered it was because the directory I was using to write the data was not empty. I created a new empty directory and the container started and stayed running just fine.

Here's my commands.

docker run --name container-name -e MYSQL_ROOT_PASSWORD=my-pword -v /Users/user_name/user_dir/mysql_data:/var/lib/mysql -p 3306:3306 -d mysql

docker start container-name to start the container and then check if it was running by using

docker ps

codeskin
  • 147
  • 1
  • 1
  • 8
0

in my case, while setting up a Postgresql container, I forgot to add the POSTGRES_PASSWORD, which is a must. check out the psql image description

Danh Le
  • 95
  • 3
  • 10