How to create, start and exec a container without command run? - docker

To understando how to work the commands on docker, i tried create containers without the run command, but it dont work.
I create a container
$ docker create <image id>
so, i tried to start
$ docker start <container id>
and this return the container id. So, i exec this command
$ docker exec <container id> sh
what generate the error:
Error response from daemon: Container 985547c13d7e3434cc32c0c8bdb1b26fd76ebc95771bc55588866b170852e747 is not running
So, how to create a container and exec a shell to attach ( $ docker attach ) without use run command? The create command seens useless if we cant start and exec on follow.

I think you need to do following steps
create image
docker create -t -i <image ID> /bin/bash
start container interactive mode
docker start -a -i <container ID>

Related

How to check open shells on a container?

How could I check if I have currently running interactive shells on a container, like for instance opened with:
docker exec -it mycontainer sh
Is there a way to retrieve this information ?
You can use docker ps to get container ID.
Exec into container with
docker exec -it mycontainer sh
or
docker exec -it mycontainer bash
Then, in the shell of the container, run the command hostname, check output with container ID.

Docker exec command without the container ID

How can do something like:
docker exec -it 06a0076fb4c0 install-smt
But use the name of the container instead
docker exec -it container/container install-smt
I am running a build on CI server so I can not manually input the container ID.
How can I achieve this?
Yes, you can do this by naming the container with --name. Note that your command with container/container is likely referencing an image name and not the container.
➜ ~ docker run --name my_nginx -p 80:80 -d nginx
d122acc37d5bc2a5e03bdb836ca7b9c69670de79063db995bfd6f66b9addfcac
➜ ~ docker exec my_nginx hostname
d122acc37d5b
Although it won't save any typing, you can do something like this if you want to use the image name instead of giving the container a name:
docker run debian
docker exec -it `docker ps -q --filter ancestor=debian` bash
This will only work if you're only running one instance of the debian image.
It does help if you're constantly amending the image when working on a new Dockerfile, and wanting to repeatedly run the same command in each new container to check your changes worked as expected.
I was able to fix this by setting a container name in the docker-compose file, and rundocker exec -it with the name form the file.
#Héctor (tnx)
These steps worked for me:
This will start the container named mytapir and spawn a shell into the docker container:
docker run -d --name mytapir -it wsmoses/tapir-built:latest bash
Upon docker ps to ensure the docker container is running:
docker exec -it mytapir /bin/bash
Will spawned a shell into an existing container named mytapir.
And you can stop the container as usual docker stop mytapir.
And starting it via docker start mytapir, if it is not running.
(check via docker ps -a)

docker start <container ID> doesn't do anything

If I spin up a Docker container with:
docker run -it ubuntu /bin/bash
and then exit. I can see the container using
docker ps -a
However, if I try and restart the container with
docker start <container ID>
I just get echoed back and returned to the command prompt.
What am I missing?
After running docker start <container ID> to restart the container try running a docker ps to ensure it's actually running.
If it IS running and you want to run commands on a bash shell from within the container, you can run the below command. In your case it would be :
docker exec -it <container ID> bash
use docker start with '-ai' so it attaches to container interactively
docker start -ai <container ID>
CAUTION! This will relaunch the process that this container is supposed to run:
If it is defined by a Dockerfile with a CMD instruction, then it will run it.
If it was a container for building a Dockerfile, the last failed instruction will be retried.
You can try:
docker start <container name>

Pull, virtualize, and bash into a docker image

I have a docker image I'd like to poke inside. How can I pull it and get access to a shell on it? I tried sudo docker exec -it verdverm/pypge-experiments bash, but it just gave me Error response from daemon: No such container: verdverm/pypge-experiments. What am I doing wrong?
Replace exec with run:
sudo docker run -it verdverm/pypge-experiments bash

Run command to docker container in a script

I need run command to docker container in a script, i trying with:
docker run -it <container> /bin/bash -c "command"
I had no error but the command is not executed
I can not create a new image, i must not stop the service ... I do it manually
It's possible to invoke a command in a running container:
docker exec <container_id> rm -f /tmp/cache/*/*/*

Resources