Run docker-compose without installation - docker

I try to run docker-compose without installation, so using docker:compose repository (with docker run).
So I tried this way :
docker run -ti --rm -v $PWD:/XXX docker/compose:1.24.1 up -d
The problem is that I don't know the container dir name of docker/compose (here XXX) to mount my current folder as volume.
Any ideas...?
Thanks !

You can bind mount your local docker-compose.yaml to any place just remember to tell docker-compose use -f, like next:
docker run -ti --rm -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/code docker/compose:1.24.1 -f /code/docker-compose.yaml up -d
Meanwhile, don't forget to add docker.sock of your host machine bind mount to the container.

That XXX folder can be anything inside the container. Basically in -v option of docker run. Its -v [host-directory]:[container-directory].
If you are trying to run a docker-compose up inside the container, then follow these steps:
Create a directory on host mkdir /root/test
Create docker-compose.yaml file with following contents:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: redis
Run docker run command to run docker-compose inside the container.
docker run -itd -v /var/run/docker.sock:/var/run/docker.sock -v /root/test/:/var/tmp/ docker/compose:1.24.1 -f /var/tmp/docker-compose.yaml up -d
NOTE: Here /var/tmp directory inside the container will contain docker-compose.yaml file so I have used -f option to specify complete path of the yaml file.
Hope this helps.

Related

How can you automatically run and remove a container in docker compose

I'm looking to forwarding my ssh-agent and found this
https://github.com/nardeas/ssh-agent
and the steps are the following
0. Build
Navigate to the project directory and launch the following command to build the image:
docker build -t docker-ssh-agent:latest -f Dockerfile .
1. Run a long-lived container
docker run -d --name=ssh-agent docker-ssh-agent:latest
2. Add your ssh keys
Run a temporary container with volume mounted from host that includes your SSH keys. SSH key id_rsa will be added to ssh-agent (you can replace id_rsa with your key name):
docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh -it docker-ssh-agent:latest ssh-add /root/.ssh/id_rsa
The ssh-agent container is now ready to use.
3. Add ssh-agent socket to other container:
If you're using docker-compose this is how you forward the socket to a container:
volumes_from:
- ssh-agent
environment:
- SSH_AUTH_SOCK=/.ssh-agent/socket
in a compose file, I add step 1 to it like so:
services:
ssh_agent:
image: nardeas/ssh-agent
However I do not what's the equivalent syntax in compose file for step 2
docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh -it docker-ssh-agent:latest ssh-add /root/.ssh/id_rsa
You can do it as below -
docker-compose -f my-docker-compose.yml run --rm ssh_agent bash -c "ssh-add /root/.ssh/id_rsa"
Ref - https://docs.docker.com/compose/reference/run/
docker-compose.yml file will be
services:
ssh_agent:
image: docker-ssh-agent:latest
command: ssh-add /root/.ssh/id_rsa
volumes_from:
- ssh-agent
environment:
- SSH_AUTH_SOCK=/.ssh-agent/socket
volumes:
- ~/.ssh:/.ssh
then run the docker-compose command as below
docker-compose -f docker-compose.yml run --rm ssh_agent

How to copy files to a Docker volume and use that volume with docker-compose

There is a webservice running in a Docker container.
This webservice relies on big json files to boot.
I create a Docker volume to store the json files with docker volume create my-api-files.
Here is the docker-compose file for the webservice:
version: '3'
services:
my-api:
image: node:alpine
expose:
- ${NODE_PORT}
volumes:
- ./:/api
- my-api-files:/files
working_dir: /api
command: npm run start
volumes:
my-api-files:
external: true
Now, how can I copy the json files to the my-api-files docker volume before to start the the webservice with docker-compose up?
You could run a temporary container with that volume and a bind mount to your host files and run a copy from there:
docker run --rm -it -v my-api-files:/temporary -v $PWD/jsonFileLocation:/big-data alpine cp /big-data/*.json /temporary
docker run --rm -it -v my-api-files:/test alpine ls /test
You should see your JSON files in there.
EDIT: Of course, replace $PWD/jsonFileLocation with your JSON file location and your operating system's syntax.

Launching docker command from docker-compose v.3 file

I'm learning about Docker and I'm at first steps.
I've to 'refresh' postgres image from compose file to initialize db scripts as YOSIFKIT here do through shell (https://github.com/docker-library/postgres/issues/193).
here is my Docker file:
FROM postgres:9.6.7
COPY docker-postgresql-9.6.7/prova.sql /docker-entrypoint-initdb.d/
and here is my compose file:
version: '3'
services:
postgresql_rdbms:
restart: always
image: postgres-prova
build:
context: ../
dockerfile: docker-postgresql-9.6.7/Dockerfile
command: bash -c "docker run -it --rm postgres-prova ls -ln /docker-entrypoint-initdb.d && docker run -it --rm postgres-prova && postgres"
environment:
PG_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- /srv/docker/postgresql:/var/lib/postgresql
HOW can I insert a command in a compose-file to do "docker run -it --rm imageToReload" ???
Because I've seen that "command:" in compose file works inside the container, but I want operate ON the container, on a upper level (=manage the container from the compose file, after the container creation)
Thank you very much
From what I understand you want docker-compose to delete/remove the container after every run so that the build is run each time and a fresh prova.sql file can be copied into the image each time the service is brought up. The --force-recreate flag is probably what you need.
The command directive within the yaml file provides the command that is run inside the container.

Docker run command, volumes error when using relative paths

I'm trying to make a docker run and build command out of the compose file below.
So far I have come up with this:
docker build --tag testenvironment/nodejs ./node_js
docker run -p 8080:8080 -v ./node_js:/home/app/chat -v /home/app/chat/node_modules --name nodejs testenvironment/nodejs
I'm stuck here because it gives the following error:
C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: create ./node_js: "./node_js" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intented to pass a host directory, use absolute path. See 'C:\Program Files\Docker Toolbox\docker.exe run --help'.
Compose file:
node:
build: ./node_js
command: node server.js
depends_on:
- mongo
links:
- mongo
environment:
NODE_ENV: development
ports:
- '8080:8080'
volumes:
- ./node_js:/home/app/chat
- /home/app/chat/node_modules
Can anybody tell me how to convert the volumes from the compose file to a docker run command? Thanks in advance.
I am using the Docker Toolbox for Windows 10.
This part:
docker run -p 8080:8080 -v ./node_js:/home/app/chat ....
Should be:
docker run -p 8080:8080 -v $(pwd)/node_js:/home/app/chat
docker run requires an absolute path for volumes (as a difference from compose)
You have to use an absolute path when adding a volume in Windows, which can be in the following formats:
docker run -p 8080:8080 -v //c/users/desktop/path/to/node_js:/home/app/chat
docker run -p 8080:8080 -v C://users/desktop/path/to/node_js:/home/app/chat

Execute command in linked docker container

Is there any way posible to exec command from inside one docker container in the linked docker container?
I don't want to exec command from the host.
As long as you have access to something like the docker socket within your container, you can run any command inside any docker container, doesn't matter whether or not it is linked. For example:
# run a container and link it to `other`
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock \
--link other:other myimage bash -l
bash$ docker exec --it other echo hello
This works even if the link was not specified.
With docker-compose:
version: '2.1'
services:
site:
image: ubuntu
container_name: test-site
command: sleep 999999
dkr:
image: docker
privileged: true
working_dir: "/dkr"
volumes:
- ".:/dkr"
- "/var/run/docker.sock:/var/run/docker.sock"
command: docker ps -a
Then try:
docker-compose up -d site
docker-compose up dkr
result:
Attaching to tmp_dkr_1
dkr_1 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dkr_1 | 25e382142b2e docker "docker-entrypoint..." Less than a second ago Up Less than a second tmp_dkr_1
Example Project
https://github.com/reduardo7/docker-container-access
As "Abdullah Jibaly" said you can do that but there is some security issues you have to consider, also there is sdk docker to use, and for python applications can use Docker SDK for Python

Resources