Another way to use docker-compose is

 3r33333. 3r3-31.
In the wake of Article Docker + Laravel =? I want to talk about a rather unusual way of using the docker-compose utility.
 3r33333.
To begin with, for those who do not know why docker-compose is needed. This is a utility that allows you to run a set of related services packed in docker containers on a separate host. The original version was written in python and could be installed in two ways:
https://github.com/docker/compose/releases and download it
 3r33333.
$ sudo curl -L "https://github.com/docker/compose/releases/download/???/docker-compose-$(uname -s) - $ (uname -m)" -o /usr /local /bin /docker-compose
 3r33333. 3r33333.  3r33333.

We set the rights to run the application


 3r33333.
    $ sudo chmod + x /usr /local /bin /docker-compose    

 3r33333. 3r33333.  3r33333.

Additionally, you can install auto completion for bash and zsh command interpreters


 3r33333. 3r33333.  3r33333.

check the installation


 3r33333.
    $ docker-compose --version
docker-compose version ???.? build 1719ceb

 3r33333. 3r33333.  3r33333. 3r33939.
 3r33333.

I think that a single binary is very cool, because we don't need to pull python dependencies. Yes, and in general - maybe our python environment is completely broken on the target machine that we want to configure !!!


 3r33333.

3r3-300.
 3r33333. An example of confusion in the python environment


 3r33333.

But there is still the 4th path, which I wanted to tell about. This is the ability to run docker-compose through docker. Indeed, there are already collected official images on the Docker Hub ( [url]Https://hub.docker.com/r/docker/compose/[/url] ). Why can they be needed?


 3r33333.
  •  3r33333.
  • if we want to work with several versions of docker-compose at the same time (although usually fairly stable last) 3r33333.  3r33333.
  • if we don’t have python or we don’t want to use it (for example, we have a lightweight distribution package 3r3-33120. CoreOS Container Linux )  3r33333.
  • use in CI /CD pipelines. 3r33333.  3r33333. 3r33333.
     3r33333.

    Let's try!


     3r33333.

    As we did usually launch containers:


     3r33333.
        $ docker-compose up -d    

     3r33333.

    Through the utility packed in a docker-container:


     3r33333.
        $ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$ PWD: /rootfs /$ PWD" -w = "/rootfs /$ PWD" docker /compose : ??? up -d    

     3r33333.

    Too much word, huh? The brain can break down to memorize all these parameters. Therefore, we will try to make life easier for ourselves and write a wrapper in the shell language. But first, let's look at the parameters passed: 3r3280.
     3r33333.

    •  3r33333.
    • --rm - deletes the temporary container after stopping, i.e. we do not leave garbage in the system.  3r33333.
    • -v /var/run/docker.sock:/var/run/docker.sock - without this, docker-compose will not be able to connect to the docker-daemon on the host  3r33333.
    • -v "$ PWD: /rootfs /$ PWD" -w = "/rootfs /$ PWD" - allows you to forward the current directory inside the container so that the utility can see the docker-compose file  3r33333. 3r33333.
       3r33333.

    We still lack the ability to interpolate values ​​in the docker-compose file. This is the process by which the utility inserts environment variables into a YAML file. For example, in fragment


     3r33333.
    version: "2.1"
services:
pg:
image: postgres: ???r3r3346. environment:
POSTGRES_USER: $ {POSTGRES_DB_USER}
POSTGRES_PASSWORD: $ {POSTGRES_DB_PASSWORD}

 3r33333.

variables POSTGRES_DB_USER and POSTGRES_DB_PASSWORD will be read from the environment. This allows for a certain degree of convenience to templify docker-compose files. Those. we need to capture the environment from the host machine and pass it inside the container.


 3r33333.

Let's solve the problem by writing a bash script.


 3r33333.
    #! /bin /sh
# create a temporary file with a unique name 3r3333346. TMPFILE = $ (mktemp)
# capture the environment and write to the file 3r3333346. env> "$ {TMPFILE}"
# save the version to a separate variable for convenience
VERSION = "???"
# run docker-compose
docker run
--rm
-e PWD = "$ PWD"
--env-file "$ {TMPFILE}"
-v /var/run/docker.sock:/var/run/docker.sock
-v "$ pwd: /rootfs /$ pwd"
-w = "/rootfs /$ PWD"
docker /compose: "$ {VERSION}"
"$ @"
3r33333. # delete the temporary file with the captured list of environment variables
rm "{$ TMPFILE}"

 3r33333.

There are additional lines:


 3r33333.
  •  3r33333.
  • -e PWD = "$ PWD" - just in case, forward the current directory  3r33333.
  • --env-file "$ {TMPFILE}" - here all other environment variables are transferred from the host  3r33333.
  • docker /compose: "$ {VERSION}" - the name of the image, take the version of the variable  3r33333.
  • "$ @" - this construction allows you to use the script as if it is the docker-compose utility, i.e. "transparently" passes its arguments to the docker container. 3r33333.  3r33333. 3r33333.
     3r33333.

    The script can be saved, for example, in /usr /local /bin /docker-compose , set the eXecute flag on it and use. The above script does not claim to be 100% free of errors or flaws and is rather an illustration of the method.
     3r33333.
    We use this method in the CI /CD pipelines. It even saves traffic to some extent, since The target docker image is taken from the local cache.
    3r33333. 3r33333. 3r33333.
    3r33333. 3r33333. 3r33333. 3r33333.
    Only registered users can participate in the survey. Enter , you are welcome.
    3r33333. 3r33333.
    3r33333.
    3r33333.
    Was it interesting?
    3r33333. 3r33333. 3r33333. 3r33333.
    3r33333.
    3r33333.
    3r33333. 3r33232. 3r33333. 3r33333. 3r3302. 3r33333. 3r33333. 3r33333. 3r33333.
    3r33333.
    yes
    3r33333.
    3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333.
    3r33333.
    no
    3r33333.
    3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333.
    7 users have voted. Abstained 8 users. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333. 3r33333.

+ 0 -

Add comment