I use docker compose to create and start my containers. I would like to create a minecraft container with a --noconsole arg. Like this :
docker run -it -d --name spigot -e EULA=TRUE -e TYPE=SPIGOT itzg/minecraft-server nogui --noconsole
I've no idea how to add the --noconsole parameter in the compose file.
How can I do this ?
Thanks
Arguments after the image name in docker run are not docker arguments, they are passed as the command to run inside the container. You need to define the command for the image you are running inside your compose file. The resulting syntax looks like:
command: "nogui --noconsole"
See the compose file documentation for more details: https://docs.docker.com/compose/compose-file/#command
Related
When writing a bash script that starts a docker container, it is useful to refer to the started docker container. How do you get the specific container id of a docker container when you start it?
P.S. I know that I can use --name to name the container, which I can use to filter the list of containers using docker ps -aqf "name=containername", but this will fail if I ever start the script twice. And then there's the possibility of name conflicts. Besides, what's the point of container IDs if you can't use them?
When you start a detached container, it returns the container ID. e.g.:
$ docker run -d ubuntu:18.04
71329cf6a02d89cf5f211072dd37716fe212787315ce4503eaee722da6ddf18f
In bash, you can define a new variable from the output like this:
CID=$(docker run -d ubuntu:18.04)
Then, later you can use this variable to refer to your container like this:
docker stop $CID
docker rm $CID
In the documentation for docker run under "capture container id", they advise using the --cidfile flag for this purpose.
--cidfile takes a file name as an argument and will write the long ID of the container to that location. E.g.,
docker run --cidfile /tmp/hello-world.cid hello-world && cat /tmp/hello-world.cid
This is useful when you don't want to run the image in a detached state, but still want access to the ID.
I want to run a command like:
docker run --network host ...
But I can't actually change my docker run command. Is there another way to have Docker do essentially the same thing, like reading from a config file or environment variables?
For example, I know I can set a HOSTNAME env in my Dockerfile, which accomplishes the same thing as
docker run --hostname my_hostname
Is there a way to do this more generally with other arguments?
Thanks.
I am looking for a way to create a Docker volume and put some data on it just before a specific container is started - which needs the configuration on startup.
I do not want to modify the container. I would like to use a vanilla container straight from the Docker Hub.
Any ideas?
Update
I did not mention that all this has to be done in a compose file. If I would do it manually, I could wait for the configuration injecting container to finish.
Absolutely! Just create your volume beforehand, attach it to any container (A base OS like Ubuntu would work great), add your data, and you're good to go!
Create the volume:
docker volume create test_volume
Attach it to an instance where you can add data:
docker run --rm -it --name ubuntu_1 -v test_volume:/app ubuntu /bin/sh
Add some data:
Do this within the container; which you are in from the previous command.
touch /app/my_file
Exit the container:
exit
Attach the volume to your new container:
Of course, replace ubuntu with your real image name.
docker run --rm -it --name ubuntu_2 -v test_volume:/app ubuntu /bin/sh
Verify the data is there:
~> ls app/
my_file
For logging purposes I want to know the name of the Docker instance that my program is running under.
For example if I start it as:
docker run -d --name my-docker-name some/image
how can i find the actual docker name (my-docker-name, in this example) from a program running in it?
TL;DR: --hostname option.
Issue
Container's program cannot access its container's name.
Solution a) -dirty and not easy-
https://stackoverflow.com/a/36068029/5321002
Solution b)
Add the option -h|--hostname="" matching the same name as the docker container name. Then you just need to query the hostname from the program and you're done.
edit
Solution c)
Provide, as you suggested, a env-variable with the name. The overall command would look like as follow:
$name="custom-uniq-name"
$docker run -h $name --name $name -e NAME=$name image-to-run
if you add
-v /var/run/docker.sock:/var/run/docker.sock
to your
docker run
command, you expose docker socket to the container, and you will be able to launch docker commands, such as
docker ps --filter
Keep in mind that this is potentially dangerous, now your container has a privileged access to the host.
I have a Docker container running, and I want to create another one similar to it. How can I find out what command was used to start the container? There's docker inspect, but I'd have to go through and look at each of the config options one by one.
Edit: I want to get the full command used to start the container, including environment variables, links, volumes, etc. For example:
docker run -d --name foo -v /bar:/bar --link baz:baz -e DEBUG=True image bash
The following will show the environment variables, the ENTRYPOINT of the Dockerfile, the CMDLINE, the volumes from, the volumes, the links.
docker inspect -f '{{ .Config.Env}} {{ .Config.Entrypoint}} {{ .Config.Cmd}} {{ .VolumesFrom}} {{.Volumes}} {{ .HostConfig.links}}' container_id