This question already has an answer here:
Dashes in Docker commands
(1 answer)
Closed 1 year ago.
Learning docker so I have some dumb questions
Came across the below command , why is that some arguments have only one hyphen (-) while others have two hypens (--). One of them is not a argument? Like interactive terminal is -it , while remove container is with --rm ?
docker run -it --rm --entrypoint sh debug/ubuntu
What does --rm do in the above command. rm is to remove the container so why are they passing it when running the container?
Answered before here
This is a standard Unix/Linux syntax, not specific to docker. One dash
is used for single letter flags. Two dashes for an option that is more
than one letter. You can merge together multiple single letter options
that don't take arguments, e.g. -i and -t can be merged into -it. You
can run docker --help to see all the options, some of which have both
a long and short format.
The --rm means that the container will be removed automatically after it is stopped. Without --rm, it can be found "dangling" with docker container ls --all
Related
This question already has answers here:
How to show the run command of a docker container
(12 answers)
Closed 1 year ago.
I have a container running in my ubuntu machine. I want to know the exact docker run command that was used to start that container. It is running since a long time and I don't remember what parameters were used to start that container.
You can find all the relevant run parameters for the container with:
sudo cat /var/lib/docker/containers/$(docker container inspect <container id> | jq -r .[0].Id)/config.v2.json | jq
This question already has answers here:
How to show the run command of a docker container
(12 answers)
Closed 3 years ago.
I ran a docker run on a docker image with a long convoluted command syntax that I have since forgotton and lost. Is there any way to derive the syntax that ran a container given the container ID?
I know docker inspect can give you information about the container but I was hoping the docker run syntax would be saved somewhere.
As mentioned by Zeitounator, this was answered here, but just adding it here for completeness:
sudo docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
assaflavie/runlike <Your_Container_ID>
I swear I've used an option some time ago where you can launch a container, then in the next docker command you can do something with that container without explicitly referring to its ID or alias - either it is "the first container in your list of containers" or "most recently created container". But I can't find anything on Google.
My imagination is recalling something like this:
docker run --detach -it ubuntu:latest
docker exec -it {0} bash
Is there any such thing? This is useful when you want to share instructions with someone for spinning something up without them having to copy and paste (or type) whatever their specific container ID was.
Collecting the several solutions, here are some approaches (feel free to update the answer with yours):
Hardcode the container name
This is probably the most compact solution
docker run --detach --name my_container -it ubuntu:latest
docker exec -it my_container bash
Get your most recently created docker container ID
This is the one I had been recalling.
docker run --detach -it ubuntu:latest
docker exec -it $(docker ps --latest --quiet) bash
# you can also filter by ancestor (image) if other containers have been launched
# in the meanwhile:
docker exec -it $(docker ps --latest --quiet --filter ancestor=ubuntu:latest) bash
Use a shell variable
I don't fully understand how $_ would help in this case so can't give an example.
Other tips for easier referencing
You don't have to copy the entire ID. If you type a as the container ID it will find the container starting with that character sequence. If there are multiple matches and the command can accept multiple container IDs it will still work (e.g. docker kill a will kill all containers with IDs that start with the letter a)
I had run few case of docker, run -idt appeared many times.
But in docker doc, run options are not include -idt
wish to figure it out what is it means? and what it for?
That is including three single level options into one batch.
-i interactive
-d detached
-t allocate a TTY
I've built a docker image based on some Dockerfile that I have written.
docker build -t someuser/somerepo:sometag .
But when I run this image with a name attribute it's apparently ignored.
docker run -t someuser/somerepo:sometag -d --name="somename"
Docker ps then gives;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0225d20fddf someuser/somerepo:sometag "/bin/sh -c '/usr/lo 26 seconds ago Up 26 seconds 8600/tcp, 8600/udp tender_curie
Where the name assigned follows dockers random name generator.
The image itself is working as intended and seems to be doing its thing. This is seemingly trivial, I know, but I just can't make the running container accept a name. I've tried as many variations of "--", in/excluding the equal sign, different quote characters etc as I can think of.
So the question is; what I am doing wrong?
Thanks.
look at the syntax of the run command: http://docs.docker.com/reference/run/
docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
[options] go before IMAGE
so you should try
docker run -t -d --name="somename" someuser/somerepo:sometag