How to alias docker-compose to docker compose? - docker

From my understanding, docker compose is the latest version. This is the one at least I installed from docker official documentation.
I am working on a project that use docker-compose in their makefile.
How could I forward all the calls (and their arguments) from docker-compose to docker compose ?
I created an alias:
alias docker-compose="docker compose"
It works fine in terminal. But not in the make file.

One solution I've come up with is to create a little script.
File: /bin/docker-compose
Content:
docker compose "$#"
Alternatively, in order to respect previous container naming convention (with underscore delimiter _ over -):
docker compose --compatibility "$#"
To make the script executable: sudo chmod +x /bin/docker-compose.
You can check it with docker-compose version.

Docker desktop comes with the option to alias docker compose v2 in Settings > General:

If you wish to avoid any additional scripts, I recommend you to :
create your alias (in your ~/.profile | ~/.bash|zshrc file)
alias docker-compose="docker compose --compatibility $#"
source your ~/.profile|.zsh|bashrc file in your makefile.

Related

config file changes to VerneMQ docker image

I want to make some changes to the config file of the VerneMQ image running on docker. Is there any way to reach the config file so that changes could be made?
If you exec into the container docker exec -it <containerID> bash, you'll see that the vernemq.conf file is located under /etc/vermnemq/. Its just the matter of replacing this default conf by your own config file. Keep your vernemq.conf in same directory as where Dockerfile is and then add
following line into Dockerfile
COPY vernemq.conf /etc/vernemq/vernemq.conf
The above line copies your config file into container at given location and replaces the existing one. Finally build the image. For more advanced stuff, do checkout this!
Another approach could be to simply set your options as environment variables for the docker image.
From the official docker hub page:
VerneMQ Configuration
All configuration parameters that are available in vernemq.conf can be
defined using the DOCKER_VERNEMQ prefix followed by the confguration
parameter name. E.g: allow_anonymous=on is -e
"DOCKER_VERNEMQ_ALLOW_ANONYMOUS=on" or
allow_register_during_netsplit=on is -e
"DOCKER_VERNEMQ_ALLOW_REGISTER_DURING_NETSPLIT=on". All available
configuration parameters can be found on
https://vernemq.com/docs/configuration/.
This is especially useful for compose-like yml-based deployments.
You can create a new Dockerfile to modify image contents -
FROM erlio/docker-vernemq
RUN Modify Command
Use the new Dockerfile to build new image & run container using that.

How to execute Linux command in host-machine and copy the output to image build by docker file?

I want to copy the my.cnf file present in the host server to child docker image wherever I run docker file that uses a custom base image having below command.
ONBUILD ADD locate -i my.cnf|grep -ioh 'my.cnf'|head -1 /
but above line is breaking docker file. Please share correct syntax or alternatives to achieve the same.
Unfortunately, you can't declare host commands inside your Dockerfile. See Execute command on host during docker build
.
Your best bet is probably to tweak your deployment scripts to locate my.cnf on the host before running docker build.

Running testcontainers inside a Docker container for Windows

As said in documentation, if I want to run testcontainers inside a docker I have to consider the following points:
The docker socket must be available via a volume mount
The 'local' source code directory must be volume mounted at the same path inside the container that Testcontainers runs in, so that Testcontainers is able to set up the correct volume mounts for the containers it spawns.
How to comply with 2nd point, mainly with the -v $PWD:$PWD condition if I use Docker for Windows?
On windows, instead socket, docker uses named pipes.
docker run -v \\.\pipe\docker_engine:\\.\pipe\docker_engine
But you need Windows v1709 and special version of Docker for Windows, since this feature is experimental.
More info:
https://blog.docker.com/2017/09/docker-windows-server-1709/
As for the $PWD, on windows cmd you can use %CD% variable which does this same job. Powershell also has a $pwd, same as in linux. But unfortunatelly, they does not work with docker-compose, as they're not true environment variables.
I think easiest would be to execute a short script to create .env file on windows where PWD= will be set to the current dir:
echo PWD=%cd% > .env
and you can use $PWD in docker-compose same as on linux.

How to open/run YML compose file?

How can I open/run a YML compose file on my computer? I've installed Docker for Windows and Docker tools but couldn't figure out how.
If you are asking how to open YAML files, then you can use some common editors like NotePad++ in windows or vim in linux.
If your question is about running the compose yaml file, then run this command from the directory where compose file is residing:
docker-compose -f {compose file name} up
You can avoid -f if your filename is docker-compose.yml
To manage .yml files you have to install and use Docker Compose.
Installation instructions can be found here: https://docs.docker.com/compose/install/
After the installation, go to your docker-compose.yml directory and then execute docker-compose up to create and start services in your docker-compose.yml file.
If you need more information, take a look to Docker Compose docs: https://docs.docker.com/compose

Get docker-compose.yml file location from running container?

I have a few running docker containers created by executing docker-compose up.
Is there any way to get the exact file path of the corresponding docker-compose.yml file used to start these containers, just by inspecting the running containers?
As far as I can see, docker inspect CONTAINER_NAME does not provide this information, nor does docker-compose provide a method to get compose-related information from a running container.
What I'd like to do in a script:
list certain running containers on a docker host
get the corresponding docker-compose.yml file locations
use docker-compose to restart all containers of the corresponding docker-compose projects at once
The answer to this question seems to have changed with new versions of docker-compose.
There is a label "com.docker.compose.project.working_dir": "/var/opt/docker", that points to the directory where I started docker-compose. I have not checked if that is pwd or the actual location of the docker-compose.yml file.
This got me interesting information about docker-compose:
samuel#vmhost1:~$ docker inspect fc440a1afbaa | grep com.docker.compose
"com.docker.compose.config-hash": "89069285a4783b79b421ea84f2b652becbdee148fbad095a6d9d85aab67ececc",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "docker",
"com.docker.compose.project.config_files": "docker-compose.yml",
"com.docker.compose.project.working_dir": "/var/opt/docker",
"com.docker.compose.service": "jenkins",
"com.docker.compose.version": "1.25.0"
samuel#vmhost1:~$
I'm running docker-compose.yml configuration version 3.6
It is not currently possible.
As an alternative might find the following helpful:
Use docker ps -a | grep <certain_container>
Use locate docker-compose.yml and find the one that you want
Use docker-compose restart (do docker-compose to see option)
You can identify it using the inspect command, as follow:
docker inspect <container_id> | grep compose
Update: Since this was asked, docker compose v2 was released, which is written in Go and accessible from docker compose instead of docker-compose (there may also be a shim directing docker-compose to this new version depending on your install). This version now embeds the directory into the image labels that you can retrieve with:
docker container inspect ${container_name_or_id} \
--format '{{ index .Config.Labels "com.docker.compose.project.working_dir" }}'
This isn't perfect for the OP's request since there may be more than one compose file, the file could be located in a different directory from where compose was run, and it doesn't capture things like environment variables or profiles that may modify how compose starts the project. However I suspect it gets most people close enough to find the source.
If you're using an older version of compose, you can use one of the options in the original answer below:
As far as I can see, docker inspect CONTAINER_NAME does not provide
this information, nor does docker-compose provide a method to get
compose-related information from a running container.
From an already running container that you do not control, the information is not there. You can infer the location using bind mount directories if the container creates any host mounts to relative directories. Otherwise, it's possible to deploy containers without compose, and it's possible to use compose without a compose file on the filesystem (piped via stdin), and compose does not store these details on running containers for you.
What I'd like to do in a script:
list certain running containers on a docker host
get the corresponding docker-compose.yml file locations
use docker-compose to restart all containers of the corresponding docker-compose projects at once
If you just want to run a restart on all containers in the same project, you don't need the first two steps, or even docker-compose. Instead, you can run:
docker ps --filter "label=com.docker.compose.project=${your_compose_project}" -q \
| xargs docker restart
Which uses a label docker-compose adds to each project it deploys.
If you want to proactively store the compose file location for later use, you can inject that as a label in your compose file:
version: '2'
services:
test:
image: busybox
command: tail -f /dev/null
labels:
COMPOSE_PATH: ${PWD} # many Linux shells define the PWD variable
If your shell does not set a ${PWD} environment variable, you can start compose with:
PWD=$(pwd) docker-compose up -d
Then you can later inspect containers for this label's value with:
docker inspect --format '{{.Config.Labels.COMPOSE_PATH}}' ${your_container_id}
And you can chain a filter and inspect command together to find the path for a specific project:
docker ps --filter "label=com.docker.compose.project=${your_compose_project}" -q \
| xargs docker inspect --format '{{.Config.Labels.COMPOSE_PATH}}'
you know, your question turns to be a useful answer to the same issue I have.
I used docker inspect <containerID> and then it gave me the location that I should look into. specifically in these lines:
HostConfig": {
"Binds": [
....
...
],
If you mounted a local volume, e.g ./data then inspecting the container will give you the path, e.g docker inspect peertube_peertube_1 | jq .[0].HostConfig.Binds. It doesn't work for containers without volumes but it's rare enough.

Resources