I need to get the name of the docker container that I am currently running, from within that container. The command that I use to run the container is
docker exec -it 9d05bea23030 bash
I am able to retrieve the ID of the container by typing
cat /etc/hostname
but I don't know how to get the name of the image. I am referring to the name that is shown under IMAGE when I type docker ps outside of the container. I need that specific name, as it contains crucial information.
This type of information/metadata is not passed into the container by default. There are 2 reasons for this.
if every metadata about the container was passed into the container, it could potentially pose a security risk when someone breaks into it
container shouldn't need this kind of information anyway
The reason why you can get access to container's ID from within the container is simply because it is used as the container's hostname by default. If you would specify hostname when running the container with container run --hostname ..., you wouldn't have access to the ID either.
I don't know why you need this information but the only way to get it when you are inside of the container is to first pass it to the container in one way or another. For example via an environment variable.
If you don't have access to this information from the outside of the container (which seems strange if you are able to use docker exec), you will not get it from the running container.
Related
I'm trying to run a program called Isaac-Sim from inside of an outer docker container (called container A for reference), but Isaac-Sim already runs in its own docker container. The end goal is to modify this to use Isaac output, but for now I just need to make a basic container that can run Isaac inside of it to get started. In addition to needing access to the specific image, it also has a lot of volumes that mount files from the main OS it needs to properly work. I'm pretty unfamiliar with docker and have tried looking stuff up, but am just not quite sure where to start.
From my understanding so far, I would need to mount the volumes Isaac needs inside of container A and define the volumes for the Isaac container relative to the container A file path rather than the main OS. I would also need to make a volume in container A -v /var/run/docker.sock:/var/run/docker.sock. This should allow me to run a docker run command inside of container A and have it start a parrallel container. However, what I'm not sure of is how to get access to the actual image for the Isaac container inside of container A. It's a rather lengthy installation process, so I don't want to reinstall it on container A every time I run it, I'd rather just volumize where it already is...but I'm not quite sure where that is.
I want to execute a perl script inside a docker container as root (SUDO on the host), and get the real user id inside the perl script.
For eg., below execution should enable myscript.pl to get the real userId (mat in this case), without having to pass it explicitly as an additional parameter to myscript.pl.. (The script inside the container when invoked from the host should find the real user invoking the script)
[mat#dev01-nrg02 ~]$ sudo docker exec -it MY_API_LAYER myscript.pl
Setting as docker environment variables during start-up of the container is ruled out, since
The script would be executed by any user, and i need to get that user's real Id.
The docker container is started up using an Init service...so this would mean technically also there is no possibility to set any user detail...
Also..
I do not want to trouble the users of the script by having them to pass their userId (which could appear as redundant, also they could pass any Id, if such an option is provided..)
Please let me know if anyone has had such a problem and resolved it?
You can't find this out.
A Docker container is pretty isolated from the calling user. This is doubly true in the case of a docker exec call: at some point in time some host user invoked the root Docker daemon to start a container, and then at some later point in time some other host user is making a call to the root Docker daemon to run a process as some other user inside the container.
If you need a user to run a script, as their own ID, on their own files, it will be significantly easier to install the dependencies required so they can run the script as themselves, without a sudo or docker layer
$ myscript.pl
I already have a running container for both postgres and redis in use for various things. However, I started those from the command line months ago. Now I'm trying to install a new application and the recipe for this involves writing out a docker compose file which includes both postgres and redis as services.
Can the compose file be modified in such a way as to specify the already-running containers? Postgres already does a fine job of siloing any of the data, and I can't imagine that it would be a problem to reuse the running redis.
Should I even reuse them? It occurs to me that I could run multiple containers for both, and I'm not sure there would be any disadvantage to that (other than a cluttered docker ps output).
When I set container_name to the names of the existing containers, I get what I assume is a rather typical error of:
cb7cb3e78dc50b527f71b71b7842e1a1c". You have to remove (or rename) that container to be able to reuse that name.
Followed by a few that compain that the ports are already in use (5432, 6579, etc).
Other answers here on Stackoverflow suggest that if I had originally invoked these services from another compose file with the exact same details, I could do so here as well and it would reuse them. But the command I used to start them was somehow never written to my bash_history, so I'm not even sure of the details (other than name, ports, and restart always).
Are you looking for docker-compose's external_links keyword?
external_links allows you reuse already running containers.
According to docker-compose specification:
This keyword links to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services. external_links follow semantics similar to the legacy option links when specifying both the container name and the link alias (CONTAINER:ALIAS).
And here's the syntax:
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
You can give name for your container. If there is no container with the given name, then it is the first time to run the image. If the named container is found, restart the container.
In this way, you can reuse the container. Here is my sample script.
containerName="IamContainer"
if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerName}\$"; then
docker restart ${containerName}
else
docker run --name ${containerName} -d hello-world
fi
You probably don't want to keep using a container that you don't know how to create. However, the good news is that you should be able to figure out how you can create your container again by inspecting it with the command
$ docker container inspect ID
This will display all settings, the docker-compose specific ones will be under Config.Labels. For container reuse across projects, you'd be interested in the values of com.docker.compose.project and com.docker.compose.service, so that you can pass them to docker-compose --project-name and use them as the service's name in your docker-compose.yaml.
I'm still a newbie and trying to learning the docker concept. I want to read the JSON file present in one Ubuntu container from the another Ubuntu container. How to do this in docker? Note that, I have to send the JSON from the first container through HTTP. Any idea on how to implement this? Any explanation or sample code on this would be really great.
If your first docker container declare a VOLUME, the other can be run with --volumes-from=<first_container>.
That would mount the declared path of the first container into the second one, effectively sharing a file or folder from the first container in the second.
Note that a container which is just created (not docker run, but docker create) is effectively a data volume container, there only to be mounted (--volumes-from) by other containers.
With http, that means the second container must know about the first (and its EXPOSE'd ports)
You will run the second container with --link=alias:firstContainer: that will allow you to contact alias:port, which is actually the url+port of the first container.
See "Communication across links"
I have a docker container that I want to use to partition client access to a database. I'd like to be able to have one container per client. If I start multiple copies of the container they all have the same name, the only difference being the port the container is assigned to.
How can I copy/rename the containers in such a way that I can differentiate the container without having to consult a lookup table that matches the assigned port to the client?
The docker rename command is part of Docker 1.5. Link to commit:
docker github
I'm using docker 1.0.1 and the following allows me to rename an image:
docker tag 1cf76 myUserName/imageName:0.1.0
All containers have a uniq name. When you do docker ps You can see that the first column is the ID. You can then manipulate your containers with this ID.
You actually need this ID in order to perform any operation on the container (stop/start/inspect/etc..)
I am unsure of what you are trying to do, but for each client, you can start a new container and then link the container ID with your user ID.
At the moment, there is no container naming within Docker, so you can't name nor rename a container, you only can use its ID.
In future versions, naming for container will be implemented.