How to name a container with docker run - docker

How is possible to assign a name to a container while using docker run with interactive mode?
For example, running this command
docker run -d -it docker_image_already_created sh
when checking with docker ps the name is autogenerated. How can the container name be passed?

Provide the --name option:
docker run -d --name your_name -it docker_image_already_created sh

Related

How to start a docker container from a image with a given name?

I am trying to run an image with a given container name.
How to achieve this?
I am running this command:
docker run -it -d macgyvertechnology/tensorflow-gpu:basic-jupyter --name hugging-face-models-run --gpus all
Docker run command format:
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
You need to update your command to
$ docker run -it -d --name hugging-face-models-run --gpus all macgyvertechnology/tensorflow-gpu:basic-jupyter
Reference:
https://docs.docker.com/engine/reference/commandline/run/#usage

how to use a hosts ip on a docker container?

I am running a metasploitable2 docker container on a server. Here is the docker command to create this docker container:
docker run --name victumb-it tleemcjr/metasploitable2:latest sh -c "/bin/services.sh && bash" --security-opt apparmor=unconfined -privileged true --network host
I then ran an exploit on Kali linux container on a different server targeting the docker image, however it failed.
use exploit/unix/ftp/vsftpd_234_backdoor
msf5 exploit(unix/ftp/vsftpd_234_backdoor) > set RHOST 134.122.105.88
RHOST => 134.122.105.88
msf5 exploit(unix/ftp/vsftpd_234_backdoor) > run
[-] 134.122.105.88:21 - Exploit failed [unreachable]: Rex::ConnectionTimeout The connection timed out (134.122.105.88:21).
I am confused as to why this exploit failed. Due to the --network host i thought that the traffic would be mirrored into the container. Is their anyway to fix this networking error, so that the hack is successful?
Here is the tutorial I was loosely following: https://medium.com/cyberdefendersprogram/kali-linux-metasploit-getting-started-with-pen-testing-89d28944097b
Because the option --network host should be placed before the image
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
This should work:
docker run --name victumb-it --network host --security-opt apparmor=unconfined --privileged tleemcjr/metasploitable2:latest sh -c "/bin/services.sh && bash"
Here sh is the command, and everything after that is arguments passed to sh command.
The docker run options like --network, --security-opt and --privileged are placed before the image.
If you run docker inspect container_id you'll see at the Args key the arguments passed to the command. It means they are not arguments to docker run.

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

Error message when creating docker container

I'm trying to create a new docker container using the following command:
docker run -d -it --name compsci -v /c/Users/garre/Documents/CPSC_Courses:/homechapmanfse/computing-resources:cs_base
However, it gives me this error message:
"docker run" requires at least 1 argument.
See 'docker run --help'.
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
How would I fix this?
You have to provide the name of the image that you want to run. This is currently missing in your command.
For example, if I were to run mysql, I would execute this:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
See the last argument, mysql? That is the name of the image.
Think that you it has build image in your machine. You must inform name of image run.
docker run image-name
This command --name is necessary only you specific name for your container. And the -it command must be entered only when entering the executed container.
docker run -d -it -v
/c/Users/garre/Documents/CPSC_Courses:/homechapmanfse/computing-resources:cs_base
--name 'the name you want to give' 'official name of the image'

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)

Resources