Understanding code executed after run command in Docker - docker

I just saw the below script in the Docker doc's.
$ docker restart db
db
$ docker run -t -i --rm --link db:db training/webapp /bin/bash
root#aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7 aed84ee21bde
. . .
172.17.0.9 db
What is happening after the run cammand is executed ? , the below part i mean:
root#aed84ee21bde:/opt/webapp# cat /etc/hosts
Can somebody explain this line by line, I fail to understand. Please this is really important to me.

docker run -t -i --rm --link db:db training/webapp /bin/bash
This line executes the command /bin/bash in a container created from the image training/webapp. /bin/bash is an interactive shell, and so executing it means that you're now in a shell inside the fake machine that is the Docker container. root#aed84ee21bde:/opt/webapp# is the shell's prompt, indicating that you are root on host aed84ee21bde with current directory /opt/webapp. cat /etc/hosts means the same thing here that it does outside the container, except that here it's referring to the /etc/hosts file inside the container, which is likely different from the one on your main system.

Related

Any commands hang inside docker container

Any commands hang terminal inside docker container.
I login in container with docker exec -t php-zts /bin/bash
And then print any elementary command (date, ls, cd /, etc.)
Command hang
When I press ctrl+c I going back to host machine.
But, if I run any command without container - it's work normally
docker exec -t php-zts date
Wed Jan 26 00:04:38 UTC 2022
tty is enabled in docker-compose.yml
docker system prune and all cleanups can not help me.
I can't identify the problem and smashed my brain. Please help :(
The solution is to use the flag -i/--interactive with docker run. Here is a relevant section of the documentation:
--interactive , -i Keep STDIN open even if not attached
You can try to run your container using -i for interactive and -t for tty which will allow you to navigate and execute commands inside the container
docker run -it --rm alpine
In the other hand you can run the container with docker run then execute commands inside that container like so:
tail -f /dev/null will keep your container running.
-d will run the command in the background.
docker run --rm -d --name container1 alpine tail -f /dev/null
or
docker run --rm -itd --name container1 alpine sh # You can use -id or -td or -itd
This will allow you to run commands from inside the container.
you can choose sh, bash, or any other shell you prefer.
docker exec -it container1 alpine sh

Docker: how to map host directory?

I am trying to reproduce steps to create an Ubuntu based image + nginx, described there:
https://www.howtoforge.com/tutorial/how-to-create-docker-images-with-dockerfile/
My host machine is Windows.
The image is built, then I have created d:\webroot folder on host, index.html file inside and try to run
docker run -v /d/webroot:/var/www/html -p 80:80 --name doom nginx_image
standard_init_linux.go:211: exec user process caused "no such file or directory"
What may be the reason and how to fix it?
The issue is with the start.sh script which is loaded from Windows. Excerpt below:
#!/bin/sh
/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
You need to change the change line ending from CRLF to LF for the start.sh.
And then run: docker run -v /d/webroot:/var/www/html -p 80:80 --name doom nginx_image

Write to /etc/hosts in docker container when running docker container

I am trying to write to the file in /etc/hosts within a docker container when I perform the run command, but when I shh into the container and check the hosts file, nothing has been written.
What is the correct command to do this?
I am running my image with the following command:
docker run -it -p 3000:3000 <imageName> bash echo 192.168.56.101 mypath.dev >> /etc/hosts
Use the "add-host" parameter when running the container:
docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts

How to edit a file dynamically in a running docker container

Background
I had build a npm server(sinopia) docker image(https://github.com/feuyeux/docker-atue/blob/master/docker-images/feuyeux_sinopia.md), and in the CMD line, it will run the start.sh every time when the container is generated.
CMD ["/opt/sinopia/start.sh"]
This shell will create a yaml file dynamically.
sed -e 's/\#listen\: localhost/listen\: 0.0.0.0/' -e 's/allow_publish\: admin/allow_publish\: all/' /tmp/config.yaml > /opt/sinopia/config.yaml
Question
I wish I could edit this config.yaml when the container is running, because I hope the content should be changed on demand.
see the snapshot photo
As shown above, the first line runs a sinopia container, and in this container, there's /opt/sinopia/config.yaml. But I don't know how to obtain this running container and edit and check this file.
If I did as the line of sinopia-ls, there's a new container runs instead of the before running one.
Thanks guys!
Answer(details please see below what I accepted)
sudo nsenter --target $PID --mount --uts --ipc --net --pid
root#58075317e47d:/# ls /opt/sinopia/
config.yaml config_gen.js start.sh storage
root#58075317e47d:/# cat /opt/sinopia/config.yaml
With docker 1.3, there is a new command docker exec. This allows you to enter a running docker:
docker exec -it <container-id> bash
You named your container, so you can find it using that name.
Then use nsenter (man nsenter) to send the command you want to do.
nsenter --target $$(docker inspect --format {{.State.Pid}} <container_name_or_ID>) --mount --uts --ipc --net --pid <cmd>
More info and solution on how to write inside of a running container : If you run SSHD in your Docker containers, you're doing it wrong!
you just need to mount the folder using -v as an option. i give an example
let's say i have /home/awan/config.yml <--- this file is always dynamic must not put it inside container
i run my container so i can mount that folder into my container
#sudo docker run -i -t -v /home/awan:/home/ubuntu/awan ubuntu/14.04 /bin/bash
after that you just edit config.yml in your /home/awan/config.yml every changes that you applied automaticaly applied inside your docker container (/home/ubuntu/awan/config.yml) because you mount it

Docker linking container

I read the Docker guide and I've a question about this page : https://docs.docker.com/userguide/dockerlinks/
Near the end, it's written :
In addition to the environment variables Docker adds a host entry for the linked parent to the /etc/hosts file. Let's look at this file on the web container now.
root#aed84ee21bde:/opt/webapp
cat /etc/hosts
172.17.0.7 aed84ee21bde
. . .
172.17.0.5 db
What's the command line I should execute to have a look in the web container?
Thanks in advance !
Just before, they did: docker run --rm --name web2 --link db:db training/webapp env. In order to have the shell prompt, simply run bash in interactive mode: docker run --rm -it --name web2 --link db:db training/webapp bash then you can cat /etc/hosts
Alternatively, you can directly do docker run --rm --name web2 --link db:db training/webapp cat /etc/hosts
In addition to #creack response on how to look at the hosts file using a shell from inside a container, you can also access the file from the host machine.
sudo cat `docker inspect --format '{{.HostsPath}}' web2`
The above one liner inspects the container named web2 and uses a template (specified with --format) to return the path to the hosts file that is given to the container. Then it just uses cat to display it (it also uses sudo, to get the right access level since docker assigns the file to root)
An interesting side effect of this is that you can update the hosts file of a container (if you get root access to the host).

Resources