Docker Push an Image to a Remote Server

Standard
Reading Time: < 1 minute

I needed to be able to push an image from my CI/CD server. Before I was using docker-hub to host my images and wanted to streamline the process. I would build my code >> build my image >> push to docker-hub >> have my server download the new image from docker-hub >> than update / restart container.

Now:

  1. CI/CD Build
  2. Push image to Remote Server
  3. Stop container / Restart

Things needed:

Docker
Python / PIP
SSH Key
docker-push-ssh (https://github.com/brthor/docker-push-ssh)

Assuming you have docker and python installed, and you have have setup ssh for key access.

Setup docker-push-ssh

sudo -H pip install docker-push-ssh

I followed the guide to add an insecure registry to docker for linux.

// /etc/docker/daemon.json

{
    "insecure-registries" : [ "localhost:5000" ]
}
sudo systemctl daemon-reload
sudo systemctl restart docker

Build a test image

Dockerfile

# vi Dockerfile

FROM alpine
RUN touch /etc/testimage
docker build -t testimage .

Push image to remote server

docker-push-ssh -i ~/.ssh/id_rsa username@host_ip_or_name testimage

Fin.

Useful Commands

# list images
docker images

# list all containers
docker ps -a

# force remove image
docker rmi -f image_id

# remove container
docker rm container_id