How to deploy a docker container on a remote ubuntu server? How to deploy a docker container on a remote ubuntu server? docker docker

How to deploy a docker container on a remote ubuntu server?


Since the release of Docker 18.09.0 this has got a whole lot easier. This release added support for the ssh protocol to the DOCKER_HOST environment variable and the -H argument to docker ... commands respectively.

First of all, you'll need SSH access to the target machine (which you'll probably need with any approach).

Then, either:

# Re-direct to remote environment.export DOCKER_HOST="ssh://my-user@remote-host"# Run a container. To prove that we are on remote-host, this will print its hostname.docker run --rm --net host busybox hostname -f# All docker commands here will be run on remote-host.# Switch back to your local environment.unset DOCKER_HOST

Or, if you prefer, all in one go for one command only:

docker -H "ssh://my-user@remote-host" run --rm --net host busybox hostname -f

Note that this is not yet supported in docker-compose v.1.23.1 (latest version as of writing) and below. However, it will be part of the next release.


  1. Setup passwordless SSH on the target machine

  2. Run the following command to remotely manage Docker on the target VM (also installs Docker if needed):

docker-machine create --driver generic --generic-ip-address=10.123.2.74 --generic-ssh-user=docker --generic-ssh-key ~/.ssh/id_rsa some_name

You can find more information about the generic driver here.

  1. Set the needed environment variables for the newly configured docker machine:

eval $(docker-machine env some_name)

  1. Any Docker command run in this terminal/cmd window will be run on the remote machine. To test run:

docker ps

Now you can run your docker containers exactly as you would locally.

PS - If you need to remotely manage a docker instance running on Windows through the Docker Toolbox things get a little complicated. (you need to solve network access to your needed ports in the docker linux VM (ssh, docker engine, container ports) either through VirtualBox's bridged network adapter or through port forwarding; also solve windows firewall issues)


I would suggest installing docker-machine on your local development environment and use the generic driver to add the remote_server, you can than use eval $(docker-machine env remote_server) to connect to it and deploy your API.

The driver will perform a list of tasks on create:

  • If docker is not running on the host, it will be installed automatically.
  • It will update the host packages (apt-get update, yum update…).
  • It will generate certificates to secure the docker daemon.
  • The docker daemon will be restarted, thus all running containers will be stopped.
  • The hostname will be changed to fit the machine name.

Deploying local container to remote_server:

Upon adding the remote_server to docker-machine via the generic driver do the following to deploy your API.

  • Get envs for server: docker-machine env remote_server

  • Connect shell to server: eval $(docker-machine env remote_server)

  • Build API image: docker build -t api_image .. (Dockerfile DIR)

  • Run container: docker run -d -p 1111:1111 api_image

  • Use curl: curl $(docker-machine ip remote_server):1111

Hope you find this helpful.