Why docker container acts differently? - docker

As I understood c120809b91b5 == ubuntu
[dev#fedora ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c120809b91b5 ubuntu "bash" 11 days ago Up 39 minutes ubuntuContainer
[dev#fedora ~]$ docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash
root#c760c5696300:/# cd testfolder
bash: cd: testfolder: No such file or directory
but why they not similar?
[dev#fedora ~]$ docker exec -it c120809b91b5 bash
(base) root#c120809b91b5:/# cd testfolder
(base) root#c120809b91b5:/testfolder#
What I do wrong?
I need to run c120809b91b5 container with --add-host=host.docker.internal:host-gateway but can't because ubuntu container acts like another with another internal environment.

What is happening here is that with docker run you are creating a new container from the ubuntu image. So when you run docker run -it --add-host=host.docker.internal:host-gateway ubuntu bash you are just creating a new container from the ubuntu image which is given a new containerId: c760c5696300
If you run docker ps -a you will see that you now have two containers with containerIds c120809b91b5 and c760c5696300
Please read docs https://docs.docker.com/engine/reference/run/

try adding "detached"
docker run -d -it --add-host=host.docker.internal:host-gateway ubuntu bash
and then look at the result.
I hope it helps.
You probably have some exited docker containers by now look at them with:
docker ps -a

Related

How do I inspect the stopped docker container files

Step 1:
docker ps -a
container Id: dd5cf6b519b4
I need to inspect inside the stopped docker container which is cannot start.
I tried with docker exec -it container-id bin/bash But this is for running container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
0dfd54557799 ubuntu "/bin/bash" 25 seconds ago Exited (1) 4 seconds ago peaceful_feynman
Commit the stopped image
$ docker commit 0dfd54557799 debug/ubuntu
now we have a new image
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debug/ubuntu <none> cc9db32dcc2d 2 seconds ago 64.3MB
create a new container from the "broken" image
$ docker run -it --rm --entrypoint sh debug/ubuntu
inside of the container we can inspect - for example, the file system
$ ls /app
App.dll
App.pdb
App.deps.json
You can start container with specific entrypoint
docker run --entrypoint sleep YOUR_IMAGE 3600
It will block current terminal for 3600 seconds. You can open new terminal tab(do not close current one) and you can verify if your container is working with the
docker ps
If you do not want to block current terminal, you can add -d flag to docker run:
docker run -d --entrypoint sleep YOUR_IMAGE 3600
Above command will start docker which will be doing nothing, then you can ssh into the container when it is working with
docker exec -ti CONTAINER HASH sh

Ubuntu container immediately exits after `docker run ubuntu`

I am on Windows 10 and I have WSL2 enabled. When I do docker pull ubuntu followed by docker run ubuntu, a new ubuntu container with a randomly generated name shows up in my dashboard and it starts for half a second, but then immediately stops. If I press the start button the same behaviour is observed. I tried running these commands from Command Prompt, PowerShell, and my downloaded Ubuntu 18.04 distro (which is also my default WSL2 distro) all with the same result.
How do I fix this?
Also, docker logs <container_name> doesn't result in anything and double-clicking the container name in the dashboard doesn't show any logs.
A docker container exits when its main process finishes its execution. Now when you check Dockerfile of Ubuntu image you can see the
CMD ["/bin/bash"] which get execute when we start the container.
so if you need to run the container in the background you can do
docker run -id --name=myubuntu ubuntu
Or you can directly launch the container with an interactive shell using
docker run -it --name=myubuntu ubuntu /bin/bash
$ docker run -d ubuntu bash -c "tail -f /dev/null"
8181cb08da63e8c2b43696155088f1a7da58023d426f11dbc52ec4867a2f5bdf
Using a command that hangs (e.g tail -f /dev/null) ensures that the container won't close early.
You can then enter the container using
$ docker exec -it 8181cb08da63e8c2b43696155088f1a7da58023d426f11dbc52ec4867a2f5bdf /bin/bash
To make it easier you could name the container by using the --name flag
$ docker run -d --name=myubuntu ubuntu bash -c "tail -f /dev/null"
fd534285934a6d62e6955ee330134e342d97a6900f4531f5ee60f729d4c6d43d
Then enter the container using
$ docker exec -it myubuntu /bin/bash

Create new image based on standard one

I have installed Docker and have running some Ubuntu image with command:
sudo docker run ubuntu
I would like to create some text file on it and find it next time the same image will run. How to achieve that?
UPD.
Got problems with attaching to docker.
I have running docker
docker ps -a
aef01293fdc9 ubuntu "/bin/bash" 6 hours ago Up 6 hours priceless_ramanujan
Since it is Up mode, I suppose I don't need to execute command:
docker start priceless_ramanujan
So, I run command attach
docker attach priceless_ramanujan
And got nothing in output while command not returns.
Why I can't get to container's bash?
Simple example:
$ docker run -it ubuntu
root#4d5643e8c1a8:/# echo "test" > test.txt
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/# exit
exit
$ docker run -it ubuntu
root#cdb44750bffc:/# cat test.txt
cat: test.txt: No such file or directory
root#cdb44750bffc:/#
docker run image_name
This command creates and starts a new container based on the provided image_name. If a name is not set for the container, a random one is generated and assigned by docker. In the above example 2 containers were created based on ubuntu.
with docker ps -a we can see that modest_jennings and optimistic_leakey are the random names created:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb44750bffc ubuntu "/bin/bash" About a minute ago Exited (1) 4 seconds ago optimistic_leakey
4d5643e8c1a8 ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago modest_jennings
cat test.txt failed the 2nd time because the file didn't exist. The container started from a "clean" ubuntu image.
Actually, we created test.txt inside modest_jennings only.
docker start container_name
This command starts a stopped container. So, in our case, the file is still there:
$ docker start modest_jennings
modest_jennings
$ docker attach modest_jennings
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/#
docker commit container_name image_name
This command is to create a new image, so that you can use it later and run containers based on that image. Continuing our example...
$ docker commit modest_jennings my_ubuntu
sha256:a4357f37153ac0b94e37315595f1a3b540538283adc3721df4d4e3b39bf8334f
$ docker run -it my_ubuntu
root#2e38616d532a:/# cat test.txt
test
root#2e38616d532a:/#
If you want a custom image, you can create a Dockerfile
`FROM ubuntu:16.04
ADD ./test.txt /tmp/`
after you can build it docker build -t ubuntu:custom .
and finally run your custom image docker run --name myubuntu ubuntu:custom sleep 3000
You can check your file with docker exec -it myubuntu /bin/bash and more /tmp/test.txt

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)

Error response from daemon: Container CONTAINER_NAME is not running

I have a docker image dajobe/hbase and its been built from Ubuntu. I created a container of this image and named it hb.
$ docker run -d --name hb dajobe/hbase
e1f68ff8b3b6c5e474426e2566f8c087d6a785fc5eeb58cd2aeb86176068651d
I then started the /bin/bash on hb, and checked for the availability of the vi editor.
$ docker exec -it hb /bin/bash
root#e1f68ff8b3b6:/# vi
bash: vi: command not found
I then installed vi editor using apt-get
# apt-get install vim
Reading package lists...
DoneBuilding dependency tree
Reading state information... Done
.....
.....
I wanted to commit the changes so that vi editor could persist.
$ docker commit hb dajobe/hbase
1be196188efc5a52562dc8ee1b63d0fd560ea163c49331c10dc435848d75ef64
then, when i again started dajobe/hbase, it automatically stopped.
$ docker run -d --name hb dajobe/hbase
c3e7b9f48077ef854efc6f9bab5e85986e265c98de5423bece0000c973206c38
$ docker exec -it hb /bin/bash
FATA[0000] Error response from daemon: Container hb is not running
Why is the container not running ?
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3e7b9f48077 dajobe/hbase:latest "/opt/hbase-server" 11 secs ago Exited (0) 8 secs ago hb
Why is the status "Exited" ? Before committing, this wasn't the case, the status was "Up".
I would expect the status to be Exited. Perhaps the original image you were using had an ENTRYPOINT that did something that kept the container running while you exec'ed to it. You can try this:
docker run -d --name hb dajobe/hbase sleep 60
Then try your exec, for the next 60 seconds you will connect with your interactive shell. After that, you will get the same message again.
The -d makes the container a daemon. It needs something to do, though, otherwise it just exits! Have you tried just doing the run line with the -it?
docker run -it --name hb dajobe/hbase bash
You get a shell prompt there, too, where you can make your updates to the image.
-g
In my case, it solved starting the container again, as easy as it may sound.
Search for the container name with docker ps -a (column "NAMES") if you do not know it; in your case it is known: "hb".
Then docker start hb.
Then docker exec -it hb /bin/bash.
You have used run and not start when you wanted to start an already existing but exited container. I do not see any start in your commands.
then, when i again started dajobe/hbase, it automatically stopped.
$ docker run -d --name hb dajobe/hbase
c3e7b9f48077ef854efc6f9bab5e85986e265c98de5423bece0000c973206c38
$ docker exec -it hb /bin/bash FATA[0000] Error response from daemon:
Container hb is not running
I also face the same as you to run the couchdb bash command
docker exec -it my-couchdb bash
and
docker exec -it my-couchdb /opt/couchdb/bin/remsh
Error message:
Error response from daemon: Container 54ca56353e3839ff0b824cf5468973aff021d14ad6b2531b85a1b95437b2ae13 is not running
For me the solution was...
I did the all kind of setup whenever I have created the container
Command:
docker run -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password --publish=5984:5984 --name my-couchdb --volume=$HOME/Desktop/BigData/docker-couchdb/data:/opt/couchdb/etc/local.d -d couchdb
If someone face the same problem as mine then try first others solution and if all of the others solution not work then please try once mine, hope it will work finally.

Resources