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

Posted on Apr 28, 2022

Question

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.

Answer

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.