O. Shekriladze
O. Shekriladze

Reputation: 1536

Run docker command on host

Can I run docker command on host? I installed aws inside my docker container, now can I somehow use aws command on host (that under the hood will use docker container's aws)?

My situation is like that: I have database backups on production host. now I have Jenkins cron job that will take sql file from db container and take it into server folder. Now I also want jenkins to upload this backup file on AWS storage, but on host I have no aws installed, also I don't want to install anything except docker on my host, so I think aws should be installed inside container.

Upvotes: 1

Views: 1157

Answers (2)

Peter Thoeny
Peter Thoeny

Reputation: 7616

Since the docker host is a server with users you can execute commands remotely on the host via ssh from the container. Example:

docker_hostname="my-host"
docker_host_user="jimmyneutron"
ssh $docker_host_user@$docker_hostname 'echo $HOSTNAME' # returns: my-host

Now you can also execute a docker command from a container as if you are on the host. Example:

ssh $docker_host_user@$docker_hostname "docker exec myOtherContainer sh -c 'aws ...'

If needed you can setup public key based authentication for passwordless ssh access on the host. See https://linuxize.com/post/how-to-setup-passwordless-ssh-login/

Upvotes: 1

David Maze
David Maze

Reputation: 159781

You can't directly do this. Docker containers and images have isolated filesystems, and the host and containers can't directly access each others' filesystems and binaries.

In theory you could write a shell script that wrapped docker run, name it aws, and put it in your $PATH

#!/bin/sh
exec docker run --rm -it awscli aws "$@"

but this doesn't scale well, requires you to have root-level permissions on the host, and you won't be able to access files on the host (like ~/.aws/config) or environment variables (like $AWS_ACCESS_KEY_ID) with additional setup.

You can just install software on your host instead, and it will work normally. There's no requirement to use Docker for absolutely everything.

Upvotes: 2

Related Questions