I've previously looked at Design Patterns for Running CLI on Docker Container, but this time IDCF Cloud CLI Let's make an image of .jp / cloud / spec / api.html). You can prepare a virtual environment with virtuanemv, but if you distribute commands with a Docker image, you can try it without polluting the environment of the host machine. You can do it. The Docker image can also be conveniently used as a CLI distribution format.
The repository created this time is here.
Dockerfile
The base image uses 2.7 ON BUILD of the official Python. The Dockerfile is very easy because ONBUILD does the necessary processing for the build.
Dockerfile
FROM python:2-onbuild
requirements.txt
The IDCF Repository does not include requirements.txt. Define the addition of dependent packages and cloudstack-api to be installed via GitHub as well.
requirements.txt
httplib2
simplejson
argparse
prettytable==0.5
parsedatetime==0.8.7
lxml
-e git+https://github.com/idcf/cloudstack-api#egg=cloudstack-api
The ONBUILD base image will COPY and pip install
the requirements.txt file.
Dockerfile
FROM python:2.7
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ONBUILD COPY requirements.txt /usr/src/app/
ONBUILD RUN pip install --no-cache-dir -r requirements.txt
ONBUILD COPY . /usr/src/app
docker-compose.yml
Set environment variables from the IDCF Cloud Console API Key (https://console.idcfcloud.com/user/apikey).
docker-compose.yml
idcfcli:
build: .
volumes:
- /etc/localtime:/etc/localtime:ro
environment:
- IDCF_COMPUTE_HOST=
- IDCF_COMPUTE_API_KEY=
- IDCF_COMPUTE_SECRET_KEY=
command: ["/usr/local/bin/cloudstack-api","listVirtualMachines","-t=id,name,state"]
git clone
from the repository. Rename docker-compose.yml.default to docker-compose.yml and set the environment variables.
$ git clone https://github.com/masato/docker-idcfcli.git idcfcli
$ cd idcfcli
$ mv docker-compose.yml.default docker-compose.yml
$ vi docker-compose.yml
Start the container from Docker Compose. The default command is running listVirtualMachines.
$ docker-compose build
$ docker-compose run --rm idcfcli
+--------------------------------------+------+---------+
| id | name | state |
+--------------------------------------+------+---------+
| xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | seed | Running |
+--------------------------------------+------+---------+
Execute any command as follows.
$ docker-compose run --rm idcfcli cloudstack-api listVirtualMachines -t=id,name,state
+--------------------------------------+------+---------+
| id | name | state |
+--------------------------------------+------+---------+
| xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | seed | Running |
+--------------------------------------+------+---------+
Define an alias in ~ / .bashrc
etc.
~/.bashrc
alias idcf-cli='docker-compose run --rm idcfcli cloudstack-api'
Using aliases makes it more CLI-like.
$ idcf-cli listVirtualMachines -t=id,name,state
+--------------------------------------+------+---------+
| id | name | state |
+--------------------------------------+------+---------+
| xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | seed | Running |
+--------------------------------------+------+---------+
Recommended Posts