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

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

Related

while starting a docker container I have to execute a script inside docker container

while starting a docker container I have to execute a script inside docker container. Can I do it using docker run command or docker start command mentioning the path in docker? I know I have to use CMD in docker file but dockerfile is not present
Have you tried
docker run -it <image-name> bash "command-to-execute"
To enter a running Docker container (get a Bash prompt inside the container), please run the following:
docker container exec -it <container_id> /bin/bash
You can get the container_id by listing running Docker containers with:
docker container ps -a or docker ps -a
docker run --name TEST -d image sh -c " CMD "
in CMD section you can give the path of shell script

Check contents of folder in Docker container

I'm trying to find out if /opt/refresh_key.sh exists in my docker container. I've tried the likes of docker container inspect container_name and also docker run -it image_name sh but neither seem to be what I need.
Docker run launch a new container.
If you want to dive inside your existing container you should do:
docker exec -it <container-name> /bin/bash
and then you will have access to the filesystem of the existing container.
You can find container-name by doing docker ps.
Try the following :
docker exec -it <contailer-id> [[ -f "/bin/sh" ]] && echo "exists"
Place your file name in place of /bin/sh

Alpine execute command in a new shell

I'm using an Alpine image in docker and I wanted to know if there is a command to open a new terminal and execute a command.
Like :
gnome-terminal -e <command>
I've already searched in ash man but didn't find what I wanted
You always have a choice of running commands on running containers irrespective of the OS type.
docker image pull nginx
docker container run -d --name nginx -p 80:80 nginx
docker exec -ti nginx sh -c "echo 'Hello World'"

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)

Understanding code executed after run command in 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.

Resources