Reputation: 5169
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
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.
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.
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.
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
Reputation: 2467
When you run following command it performs 2 operations.
$ docker run -p 8081:80 --name eg -it epgg/eg bash
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