Mounting volumes to a Kali Linux docker container stops interactive bash shell aesthetic features? - bash

When running my Kali Linux docker container headerless-kali-linux without mounting volumes using the command:
docker run --rm -it headerless-kali-linux
The bash terminal within the container has colours and aesthetic features, which I want. They look like this:
However, when I mount my volumes to the container with the command:
docker run --rm -it -v %cd%\root:/root -v %cd%\postgresql:/var/lib/postgresql headerless-kali-linux
The aesthetic features go, as if volumes should somehow affect that, and I'm left with plain bash in my terminal. If anyone has any idea why this is happening, please let me know! Thanks in advance!

why this is happening
You are mounting /root so you are overwriting shell startup files, that could set up the configuration.

This problem seems to be fixed when creating docker volumes as opposed to using a local source in the C drive. You can also create a container first, not remove it, see its ID with docker ps -a and then populate local directories with docker cp <container ID>:/path/to/dir /path/to/local/volume. From here you can attach the volumes to the local sources with the -v /path/to/local/volume:/path/to/dir option when running.

Related

How to check if the docker image has all the files?

Is there a way to check if the docker image has all of the files that the Dockerfile copies over and to understand if the image is built as configured in the Dockerfile? My situation is that the image is built successfully, however when I try running it, docker complains that it cant find some file or other and the container fails to run, so I cant exec on it.
Doing docker inspect is not helping since it does not report on the files in the image. Is there some method?
You can run a shell based on that image:
docker run -it <image-name> bash
Use sh instead if there is no bash available. There you can search for files as any shell.
But maybe you have not bash in the image, so use sh:
docker run -it <image-name> sh
But maybe you have an odd entrypoint, so override it:
docker run -it --entrypoint sh <image-name>
You can see the history of file and check if all the required files are present at the time of image creation
docker image history --no-trunc [image_name] > [file_name]

How to restore a mongo Docker container on the Mac

I removed my mongo container
docker rm myMongoDB
Did I lose all my data, or I can restore it? If so, how?
When I try to run another container from the image
docker run -p 27017:27017 -d mongo --name myMongo2
it won't run and its STATUS says Exited (2) 8 seconds ago.
The official mongo image on Docker Hub (https://hub.docker.com/_/mongo/) defines two volumes to store data in the Dockerfile. If you did not explicitly specify a -v / --volume option when running the container, Docker created anonymous (unnamed) volumes for those, and those volumes may still be around. It may be a bit difficult to find which volumes were last used by the container, because they don't have a name.
To list all volumes that are still present on the docker host, use;
docker volume ls
Which should give you something like this;
DRIVER VOLUME NAME
local 9142c58ad5ac6d6e40ccd84096605f5393bf44ab7b5fe51edfa23cd1f8e13e4b
local 4ac8e34c11ac7955b9b79af10c113b870edd0869889d1005ee17e98e7c6c05f1
local da0b4a7a00c4b60c492599dabe1dbc501113ae4b2dd1811527384a5dc26cab13
local 81a40483ae00d72dcfa2117b3ae40f3fe79038544253e60b85a8d0efc8f3d139
To see what's in a volume, you can attach it to a temporary container, and check what's in there. For example;
docker run -it -v 81a40483ae00d72dcfa2117b3ae40f3fe79038544253e60b85a8d0efc8f3d139:/volume-data ubuntu
That will start an interactive shell in a new ubuntu container, with the volume 81a40483ae00d72dcfa2117b3ae40f3fe79038544253e60b85a8d0efc8f3d139 mounted at /volume-data/ inside the container.
You can then go into that directory, and check if it's the volume you're looking for:
root#08c11a34ed44:/# cd /volume-data/
root#08c11a34ed44:/volume-data# ls -la
once you identified which volumes (according to the Dockerfile, the mongo image uses two), you can start a new mongo container, and mount those volumes;
docker run -d --name mymongo \
-v 4ac8e34c11ac7955b9b79af10c113b870edd0869889d1005ee17e98e7c6c05f1:/data/db/ \
-v da0b4a7a00c4b60c492599dabe1dbc501113ae4b2dd1811527384a5dc26cab13:/data/configdb/ \
mongo
I really suggest you read the Where to Store Data section in the documentation for the mongo image on Docker Hub to prevent loosing your data.
NOTE
I also noted that your last command puts the --name myMongo2 after the image name; it should be before mongo (the image name). Also myMongo2 is an invalid container name, as it is not allowed to have uppercase characters.

Mounting a directory located on an alternate host computer volume. Possible? (Docker)

Problem: I want to mount a directory to my docker container, but its located on another hard drive.For example:
/Volumes/alternateDrive/files!
A better explanation below:
Lets take the following scenario:
(I'm using OSX and Boot2Docker)
Say I have a directory of files located at:
/Volumes/SomeHardDrive/project/ ... files!
I would like to mount this directory in a docker container as follows:
docker run -it --name data_volume -v /Volumes/SomeHardDrive/project:/files ubuntu /bin/bash
I will then be dropped into a bash shell where i can run the following command:
ls -lah /files
No joy.
However, this works just dandy:
docker run -it --name data_volume -v ~/test_dir_of_files:/files ubuntu /bin/bash
Thoughts?

how to modify files in a container using a script

I am trying to run a container and modify certain files in it. I am trying to do this using a script. If I use:
docker run -i -t <container> <image>, it is giving me
STDERR: cannot enable tty mode on non tty input
If I use:
docker run -d <container> <image> bash, the container is not starting.
Is there anyway to do this?
Thanks
Run the docker image in background using:
docker run -d <image>:<version>
Check running docker containers using:
docker ps
If there is only one container running you can use below command to attach to a running docker container and use bash to browser files/directories inside container:
docker exec -it $(docker ps -q) bash
You can then modify/edit any file you want and restart the container.
To stop a running container:
docker stop $(docker ps -q)
To run a stopped container:
docker start -ia $(docker ps -lq)
So to start off, the -i -t is for an interactive tty mode for interacting with the container. If you are invoking this in a script then it's likely that this won't work as you expect.
This is not really the way containers are meant to be used. If it is a permanent change, you should be rebuilding the image and using that for the container.
However, if you want to make changes to files that are reflected in the container, you could consider using volumes to mount directories from the host into the container. This would look something like:
docker run -v /some/host/dir:/some/container/dir -d container
At this point anything you change within /some/host/dir will be within the container at /some/container/dir. You can then make your changes with a script on the host, without having to invoke the docker cli.

Docker. How to get bash\ssh inside runned container (run -d)?

I want to ssh or bash into runned docker container. Please, see example:
$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg
now I want to get something like this (go into runned container):
$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root#665b4a1e17b6:/#
Previously I used Vagrant so I want to get behavior similar to vagrant ssh. Please, could anyone help me?
After the release of Docker version 1.3, the correct way to get a shell or other process on a running container is using the docker exec command. For example, you would run the following to get a shell on a running container:
docker exec -it myContainer /bin/bash
You can find more information in the documentation.
The answer is docker attach command.
For information see: https://askubuntu.com/a/507009/159189

Resources