This question already has answers here:
How to continue a Docker container which has exited
(14 answers)
Closed 6 years ago.
A related question & answer on How to start a docker container (ubuntu image) suggest using docker run -it ubuntu to start a ubuntu container and connect to it. However the run command creates and starts a new ubuntu container.
How do we start an existing docker container (ubuntu image) given it's CONTAINER_ID without creating a new container?
Example:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f297d02f419 ubuntu "/bin/bash" 3 seconds ago Exited (0) 1 seconds ago cranky_wilson
How do we start 9f297d02f419 ?
you can start the stopped container using docker start command -
eg:
docker start 9f297d02f419
If you just use run on the Ubuntu image it will start a container that's running no command, which will immediately stop. You can docker start it but it will stop again. You can see it with docker ps -a.
The accepted answer in that question is very old and not very good. If you run that command on the current Docker version you get an error No command specified!
What you need to do is tell that container to run a command:
docker run ubuntu date
Will run a container from the image, run the date command, then exit. If you want to keep it running indefinitely, try something like:
docker run -d ubuntu tail -f /dev/null
You should see that the container is now running. The -d makes it run in the background, otherwise it will occupy your shell. And the final piece of the puzzle: since we have a container now that's configured to run a command, you can use docker ps to find its ID, and you can docker stop and docker start it at will.
Related
I ran a docker container on one of the terminals, on the other terminal:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80b6be3a7d56 rbonghi/isaac-ros-tutorial:realsense-camera "/ros_entrypoint.sh …" 9 seconds ago Up 8 seconds inspiring_almeida
and then I run
$ docker attach inspiring_almeida
now nothing seems to happen, cursor moves to a new line.
$ docker attach inspiring_almeida
What am I doing wrong? I expected to see something like root#80b6be3a7d56
P.s. I'm accessing the machine that I run docker via SSH - if that matters.
Run docker exec -it inspiring_almeida /bin/bash. It runs a shell inside the container.
attach just connects your input/output terminal with the container's input/output.
docker ps shows instances, but when I try to log in to the instance, it says it's not running?
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaa62ff2df11 monitor_kibana "/usr/local/bin/dumb…" 4 months ago Up 9 days kibana
613dc901f2e1 monitor_elasticsearch-search "/usr/local/bin/dock…" 4 months ago Up 9 days elasticsearch-search
$ docker exec -it eaa62 bash
Error response from daemon: Container eaa62ff2df11547744c5f7cf82cad16bf576820d2a209c4f19f173cca68f5511 is not running
$
Could it be that the container only runs for a very short time? If you use the -a flag in your statement to get only active containers, like so:
docker ps -a
Does it still show up? It could be that it runs and just uses something like ECHO. In that case, because the program run succesfully the container is immediately terminated.
Is this an official image? If so, try to run the container without the -d (for deamon) flag. This should output the run information to terminal and give you some information on what is going on.
sudo docker exec -it eaa62 bash
Im trying to create 2 docker process in the background.Both creates
Nginx - Create a background image
docker ps - list this process
Ubuntu - Create a background image
docker ps - list only the nginx.
docker ps -a - list both the containers
My questions is related to 4) - Why the ubuntu image is not listed when I try 'docker ps'
docker run ubuntu without any further args will use the default command for this image the follows the below logic:
The command is a shell that takes its input from stdin.
Once stdin is closed the shell will exit.
When the process that starts the container exits, the container is stopped.
Docker ps without the -a will only list the running containers.
Note that the process for nginx, rather than being a shell, is a web server that does not depend on stdin.
To see the ubuntu container continue to run, but in the background, you can include the -id options on the command line, e.g.:
$ docker run -id ubuntu
ef672b3750e62c309afdf656d7d82951d302db79274b7369e620e5381f806654
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef672b3750e6 ubuntu "/bin/bash" 7 seconds ago Up Less than a second brave_newton
I'm new to docker.
I have an image that I want to run, but I want docker to see if that image is already running from another terminal...if it is running I don't want it to load another one...
is this something that can be done with docker?
if it helps, I'm running the docker with a privileged mode.
I've tried to search for singleton docker or something like that, but no luck.
updates-
1.working from ubuntu.
My scenario- from terminal X I run docker run Image_a
from terminal Y I run docker run Image_a
when trying to run from terminal Y, I want docker to check if there is already a docker running with Image_a, and the answer is true - I want docker not to run in terminal Y
You can use the following docker command to get all containers that running from specific image:
docker ps --filter ancestor="imagename:tag"
Example:
docker ps --filter ancestor="drone/drone:0.5"
Example Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fb00087d4c1 drone/drone:0.5 "/drone agent" 6 days ago Up 26 minutes 8000/tcp drone_drone-agent_1
This approach uses docker api and docker daemon, so it doesnt matter if the run command executed in background or other terminal.
Aother approach:
If you have a single container form a single image:
Try naming your containers, You cant have 2 containers with the same name:
docker run --name uniquecontainer Image_a
Next time you run the above command you will get an error. Btw consider using -d so you dont have to switch terminals.
docker run -d --name uniquecontainer Image_a
If command "docker run ubuntu bash" the container won't last.
but if I command "docker run -it ubuntu bash"
the container will make a pseudo-tty and keep this container alive.
my question is
is there any way I can make a Dockerfile for building an image based on ubuntu/centos then I just need to command "docker run my-image" and
the container will last.
apologize for my poor english, I don't know if my question is clear enough.
thanks for any response
There are three ways to run containers:
task containers - do one thing and then exit, like docker run ubuntu ls /
interactive containers - open a connection to the container with -it, like docker run -it ubuntu bash
background containers - keep a container running detached in the background with -d, like docker run -d ubuntu:14.04 ping localhost
Docker keeps the container running as long as there is an active process in the container. The first container exits when the ls command completes. The second container will exit when you exit the bash session. The third container will stay running as long as the ping process keeps running (note that ping has been removed from the recent Ubuntu images, which is why the example specifies 14.04).