36

I have implemented an API inside a docker container and I want to deploy this container to a remote ubuntu server. How can I do that exactly? My API uses many resources and I have used MLDB framework to implement it. I want to deploy the container containing the API on this remote ubuntu server. So far, I have found many guides to deploy the API on AWS and DigitalOcean, but since I already have access to a remote ubuntu server than I don't need those right? So how can I deploy my container in order for someone else to be able to test my API? If there is a better way to deploy my API (hopefully for free or with cheap cost) then please let me know.

Thanks in advance.

sniper71
  • 479
  • 1
  • 4
  • 16

3 Answers3

34

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.

Dirk
  • 9,381
  • 17
  • 70
  • 98
13
  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)

Sergiu Indrie
  • 559
  • 5
  • 12
4

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.

thoba
  • 61
  • 3
  • This seems like the solution I need. Let's say I was able to connect to the remote host, how do I deploy the API then? – sniper71 Apr 20 '17 at 08:29
  • 1
    Upon `eval $(docker-machine env remote_server)`, you'd use the usual `docker` or `docker-compose` commands used to `build` and `run` containers. – thoba Apr 20 '17 at 09:47