I have only one docker instance running. My actual task to write shell script around this container.
However the first step is to get the container ID via a shell command.
Is there a shell command to do it?
you can do either
docker ps -q
or also, as the only running container is also the last one
docker ps -lq
Related
I have created a docker container which is up and running in the background. I tried to attach the container in my terminal using the container id. It enters the container but I can't do anything in it. So, I have to exit every time using ctrl+c.
If the comment by Lawrence Cherone did not help you consider the following options:
If you want to invoke a command in a already running container, use docker exec.
You can exec into you container by it's name or id. This means you can run a command in a running container. See docs.docker.com: docker exec.
Example:
The docker exec command runs the command bash in a running container.
docker exec -it <container-id> bash
Note: This will really invoke a new command and do not attach to the currelty running command.
If you just want to get the putput of a container, you can use docker logs. Again you use your container name or id.
See docs.docker.com: docker logs
Example:
docker logs <container-id>
When writing a bash script that starts a docker container, it is useful to refer to the started docker container. How do you get the specific container id of a docker container when you start it?
P.S. I know that I can use --name to name the container, which I can use to filter the list of containers using docker ps -aqf "name=containername", but this will fail if I ever start the script twice. And then there's the possibility of name conflicts. Besides, what's the point of container IDs if you can't use them?
When you start a detached container, it returns the container ID. e.g.:
$ docker run -d ubuntu:18.04
71329cf6a02d89cf5f211072dd37716fe212787315ce4503eaee722da6ddf18f
In bash, you can define a new variable from the output like this:
CID=$(docker run -d ubuntu:18.04)
Then, later you can use this variable to refer to your container like this:
docker stop $CID
docker rm $CID
In the documentation for docker run under "capture container id", they advise using the --cidfile flag for this purpose.
--cidfile takes a file name as an argument and will write the long ID of the container to that location. E.g.,
docker run --cidfile /tmp/hello-world.cid hello-world && cat /tmp/hello-world.cid
This is useful when you don't want to run the image in a detached state, but still want access to the ID.
when calling docker ps the list is empty, although I got an id:
(dcbb6aeaa06ba43fcb.....)
My steps:
Step 1: I created an image (imagekommando) of an running js.file:
Step 2: I created a container (in background) based on my image
docker run -d --name containerkommando imagekommando
I got an id! (container-id??)
Step 3: But docker ps shows empty list:
But when I repeat Step 2, I'm told, that the container (containerkommando) already exists:
docker run -d --name containerkommando imagekommando
Could you help me, understanding the logic behind?
And how can I get the container running (by ID)?
That means that the docker container exited with an error but clean up is required. With --rm option you can tell the docker to remove the container when the container has exited.
docker run --rm .....
Also to check the reason for the container exiting...you can use
docker logs <container_id>
What probably takes place here:
docker run ... creates and starts your container
your container exits
docker ps doesn't list stopped containers (default shows just running), so it made you think that it's not there.
docker run ... fails because you are trying to create and run a container with a name that already exists.
Further reading:
What are the possible states for a docker container?
Why docker container exits immediately
In Docker, a container is automatically exited when the task is finished. You have to specify a correct entrypoint to keep your docker container up.
You can check the exited containers with the command docker ps -a. This exited container will prevent you from using the name again.
So, you may want to use docker rm <container-name> before creating your new container. In a test environement, you can also use docker system prune to clean all unused container/networks.
docker ps only shows the active containers (the running ones).
Your container most probably exited right after you started it. You can use the container ID and do docker logs <container-id> to examine the reason why the container failed.
If you want to see the stopped containers together with the running containers you can do docker ps -a to get a list of all these.
Execute
docker logs <CONTAINER ID>
to view the logs of docker container run.
I faced a similar issue found out there was space issue win my docker. After clearing space the container was able to run.
What is the difference between docker run and docker create commands?
I usually use run but sometimes in documentation I see create.
Docker's --help tells
create Create a new container
run Run a command in a new container
Does it mean that run is used when we need to pass a command to a new container? What's the aim of create then?
docker run = docker create + docker start.
From docker documentation
The docker create command creates a writeable container layer over the
specified image and prepares it for running the specified command. The
container ID is then printed to STDOUT. This is similar to docker run
-d except the container is never started. You can then use the docker start command to start the container at any point.
This is useful when you want to set up a container configuration ahead
of time so that it is ready to start when you need it. The initial
status of the new container is created.
docker create command creates a writeable container from the image and prepares it for running.
docker run command creates the container (same as docker create) and starts it.
The other answers have this covered but I thought I'd show the equivalent shell command-lines because it makes it really clear:
$ docker run myimage
is the same as
$ docker start -a $(docker create myimage)
Here, docker create is used to create a container from the named image and outputs the created container id and docker start is used to start the container with that id. The -a option causes the terminal to attach so that the container runs in the foreground which is the default behaviour of docker run.
A container that has been created but never started will have a Created status; this can be seen with docker container ls -a.
I'm new to docker and just got around to playing with it;
My take is that docker run essentially does the following: (in the order of..) docker create, docker start, docker attach , since it immediately attaches to the active shell after you do the 'run' command.
to create a container:
to start a container:
to create and start with a single command:
Now to understand we must dig deep with create and start.
Process of creating a container is taking the file system from image, and kind of prep it for use in the new container. When we create the container we are just prepping or setting up the file system snapshot to be used to create the container to actually start the container.
So creating container is about the file system starting it is about actually executing the startup the command.
And to start the container, we actually execute the start up command that might start up the process.
Lets see it in terminal:
When I run command "sudo docker create hello-world" it prints bellow output.
In the output we saw characters printed out. This is the ID of the container that was just created, Now I can actually execute the hello world command inside of this container by running Docker start.
So what happened here, first off we kind of prop the container by getting the file system ready.
Then after that we actually executed the primary start up command in there with Docker start.
-a in the docker start command is for watching output from the container and print it out to your terminal.
So there is very small difference between Docker run and docker start, by default Docker run is going to show you all the logs or all the information coming out of the container. By default Docker start is the opposite Docker start is not going to show you information coming out of the terminal.
Now you know when you need to use Run / Create / Start
Docker run is basically for running commands in the container.
docker run -it <Container Name> /bin/bash
The above is for creating a bash terminal. And make us use bash commands in the container.
Docker create is to create a container from an Docker Image.
docker create -d /var/lib:/var/lib --name docker-ubuntu ubuntu
The above is to create a docker a container of the name "docker-ubuntu" from the image "ubuntu"
In practice to start a container I do:
docker run a8asd8f9asdf0
If thats the case, what does:
docker start
do?
In the manual it says
Start one or more stopped containers
This is a very important question and the answer is very simple, but fundamental:
Run: create a new container of an image, and execute the container. You can create N clones of the same image. The command is:
docker run IMAGE_ID and not docker run CONTAINER_ID
Start: Launch a container previously stopped. For example, if you had stopped a database with the command docker stop CONTAINER_ID, you can relaunch the same container with the command docker start CONTAINER_ID, and the data and settings will be the same.
run runs an image
start starts a container.
The docker run doc does mention:
The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
That is, docker run is equivalent to the API /containers/create then /containers/(id)/start.
You do not run an existing container, you docker exec to it (since docker 1.3).
You can restart an exited container.
Explanation with an example:
Consider you have a game (iso) image in your computer.
When you run (mount your image as a virtual drive), a virtual drive is created with all the game contents in the virtual drive and the game installation file is automatically launched. [Running your docker image - creating a container and then starting it.]
But when you stop (similar to docker stop) it, the virtual drive still exists but stopping all the processes. [As the container exists till it is not deleted]
And when you do start (similar to docker start), from the virtual drive the games files start its execution. [starting the existing container]
In this example - The game image is your Docker image and virtual drive is your container.
run command creates a container from the image and then starts the root process on this container. Running it with run --rm flag would save you the trouble of removing the useless dead container afterward and would allow you to ignore the existence of docker start and docker remove altogether.
run command does a few different things:
docker run --name dname image_name bash -c "whoami"
Creates a Container from the image. At this point container would have an id, might have a name if one is given, will show up in docker ps
Starts/executes the root process of the container. In the code above that would execute bash -c "whoami". If one runs docker run --name dname image_name without a command to execute container would go into stopped state immediately.
Once the root process is finished, the container is stopped. At this point, it is pretty much useless. One can not execute anything anymore or resurrect the container. There are basically 2 ways out of stopped state: remove the container or create a checkpoint (i.e. an image) out of stopped container to run something else. One has to run docker remove before launching container under the same name.
How to remove container once it is stopped automatically? Add an --rm flag to run command:
docker run --rm --name dname image_name bash -c "whoami"
How to execute multiple commands in a single container? By preventing that root process from dying. This can be done by running some useless command at start with --detached flag and then using "execute" to run actual commands:
docker run --rm -d --name dname image_name tail -f /dev/null
docker exec dname bash -c "whoami"
docker exec dname bash -c "echo 'Nnice'"
Why do we need docker stop then? To stop this lingering container that we launched in the previous snippet with the endless command tail -f /dev/null.
daniele3004's answer is already pretty good.
Just a quick and dirty formula for people like me who mixes up run and start from time to time:
docker run [...] = docker pull [...] + docker start [...]
It would have been wiser to name the command "new" instead of "run".
Run creates a container instance of an existing (or downloadable) image and starts it.