littleworth
littleworth

Reputation: 5169

How to re-enter to docker containter prompt after exiting

I'm running docker following this procedure:

    $ sudo service docker start
    $ docker pull epgg/eg
    $ docker run -p 8081:80 --name eg -it epgg/eg bash
    root@35f54d7d290f:~#

Notice at the last step it creates a root prompt root@35f54d7d290f:~#. When I do

 root@35f54d7d290f:~# exit
 exit

The docker process end and the Apache inside the container is dead. How can I exit the container safely, and how can I re-enter the docker container prompt back.

Upvotes: 3

Views: 2479

Answers (2)

BlimBlam
BlimBlam

Reputation: 366

Since the purpose of any container is to launch/run any process/processes, the container stops/exits when that process is finished. So to keep running the container in the background, it must have an active process inside it.

  1. You can use -d shorthand while running the container to detach the container from the terminal like this:

    docker run -d -it --name {container_name} {image}:{tag}
    

    But this doesn't guarantee to run any process actively in the background, so even in this case container will stop when the process comes to an end.

  2. To run the apache server actively in the background you need to use -DFOREGROUND flag while starting the container:

    /usr/sbin/httpd -DFOREGROUND     (for centOS/RHEL) 
    

    This will run your apache service in background.

  3. In other cases, to keep your services running in the detached mode simply pass on the /bin/bash command, this will keep the bash shell active in the background.

    docker run -d -it --name {container_name} {image}:{tag} /bin/bash
    

Anyway, to come outside the running container without exiting the container and the process, simply press: Ctrl+P+Q.

To attach container again to the terminal use this:

docker attach {container_name}

Upvotes: 4

fly2matrix
fly2matrix

Reputation: 2467

When you run following command it performs 2 operations.

$ docker run -p 8081:80 --name eg -it epgg/eg bash
  • It creates a container named eg
  • It has only one purpose/process bash that you have overridden using cmd parameter.
    • That means when bash shell is over/complete/exit container has no objective to run & hence your docker container will also entered into stopped stage.

Ideally you should create a container to run apache-server as the main process (either by default entry-point or cmd).

$ docker run -p 8081:80 --name eg -d epgg/eg

And then using following command you can enter inside the running container.

$ docker exec -it eg bash

here name of your container is eg (Note since you already have a container named "eg" you may want to remove it first)

$ docker rm -f eg

Upvotes: 2

Related Questions