Moving docker containers manually and automated.

Started by burnyd, June 09, 2018, 06:02:53 PM

Previous topic - Next topic

burnyd

Moving docker containers manually and automated.

This is a blog post that will cover part of my container obsession just the basics.  Ill go over how to manually create a docker container.  Moving a container anywhere.  Publishing the container to docker hub then finally I wrote a quick python script that will take and automate the creation of as many containers […]

This is a blog post that will cover part of my container obsession just the basics.  Ill go over how to manually create a docker container.  Moving a container anywhere.  Publishing the container to docker hub then finally I wrote a quick python script that will take and automate the creation of as many containers as a user wants.


This is all within my home test lab which was using the following

-Ubuntu 14.04 LTS

-Docker 1.11.0

-Python 2.7 (yah yah I know)

-Assuming the docker client and daemon are on the same host.


First things first in typical Debian we will want to go ahead and install docker.  The funny thing about Ubuntu is that there was a process called docker previously. So in the ubuntu world we want to run the following commands


#sudo apt-get update

#sudo apt-get install docker.io

Screenshot from 2016-05-16 13:02:53


With the 14.04 repository 1.6 is the current docker version which will most likely work for this exercise.  I upgraded to 1.11 due to the ip mac/vlan project.  So we will upgrade to the latest and greatest straight from docker source.


#sudo wget -qO- https://get.docker.com/ | sh

This command should grab the latest version off of the docker repo

Screenshot from 2016-05-16 13:05:56


Alright.  So we are about to deploy our very first container within ubuntu 14.04.  The process is really easy.  I love what docker has done with containers to package everything up so it is so simple that it can be ran from a simple one liner.  So lets get into it.


#sudo docker run -dit ubuntu:14.04.1 /bin/bash

Docker run is command to start a container the argument -dit is disconnect and interactive ubuntu 14.04.1 is the container image and /bin/bash is the run command.

Screenshot from 2016-05-16 13:09:01

It is important to note that this ubuntu image is directly from the docker hub.  Within Dockerhub anyone can pull any public image down from anywhere running docker.  So After docker realizes that ubuntu image is not local it will pull the image down.  Now there are 4 pulls.  This is what makes docker really significant.  There are 4 levels of this image.  What is even more awesome is that if I tied something to the ubuntu image I would only have to ever update the level I tied to it.  So I would never have to keep downloading over and over again the ubuntu image.


So the container is running within this system.

#sudo docker ps -a

Screenshot from 2016-05-16 13:14:28

We can see this containers unique ID which I will get into in a bit.  The image it uses.  The starting bash how long it has ran for.  Ports are something I am not going to get into but basically we can expose network tcp/udp ports to each one of these containers.


We can easily kill the container with the following commands

#sudo docker stop CONTAINERID

#sudo docker rm CONTAINERID

The container needs to be stopped and deleted.  Just as a FYI the container can be brought back up at any point.  The files live within /var/lib/docker/aufs/diff using their container ID.  So they will need to be deleted as well.


So lets go ahead and create out very own docker container unique to lets say a project and try to move it from this VM I am using and off to another docker host.


First thing is create a file in whichever directory called Dockerfile it has to be called exactly that.

#sudo vim Dockerfile

Screenshot from 2016-05-16 16:20:01

This file is where docker will build the image from.  So for example the first line is a simple comment.  The from Ubuntu is saying pull down the ubuntu image.  I am the maintainer.  The next two commands tell the container to first update your respo then go ahead and download iperf. The last is a simple write the output to testfile so later we know it all worked.


We are ready to create our docker image.

#docker build -t firstimage:0.1 Dockerfile

#docker run -it firstimage:0.1 /bin/bash

Screenshot from 2016-05-16 16:27:45

We can see that this has iperf installed from the apt-get we did told it to in the Dockerfile.


Now the first option here we can do to get this container off of this host is to manually move it.  Its possible once the docker container is up and running to save it and move it to any docker platform and run it there!


Saving the docker image

#docker save -o firstimage.tar firstimage:0.1


The image is now saved we need to SCP it to another host

#scp firstimage user@machine:/pathtoscpfile

Screenshot from 2016-05-16 16:45:34

The part that always gets me is that this file is only 156MB!


Next lets go ahead and jump on the machine we moved the file to and run the file


#docker load -i  /pathtofolder

#docker run -it


Screenshot from 2016-05-16 16:45:34

So in that tutorial we have successfully moved a container the manual way.


Next we will walk through how to push this to docker hub.  The first thing to do is sign up for docker hub.


So here is my docker hub repository

Screenshot from 2016-05-16 16:53:08

The repository we are going to move firstimage to is going to be called burnyd/iperf-test


We first want to create a docker tag we need to find the unique docker ID

#docker images | grep firstimage

Screenshot from 2016-05-16 17:08:54

We have found the unique image file.  Now we need to create the tag

#docker tag 4ea31abc539d burnyd/iperf-test:1.0

If we check the images again we can see there is a new image created.






So lets go ahead and push this to dockerhub.

#docker push burny/iperf-test:1.0

Screenshot from 2016-05-16 17:11:01

Now if we check our docker hub we should see this image.

Screenshot from 2016-05-16 17:12:19

It is that simple.  I should be able to pull this container and run it on any docker running host lets try a new host.


#docker pull burny/iperf-test:1.0

Screenshot from 2016-05-16 17:15:45.png

So now we should be able to simply run the image.

#docker run -it burnyd/iperf-test:1.0 /bin/bash

Screenshot from 2016-05-16 17:16:49

So there we have it.  We can grab this image from anywhere in the world!


The last section of this blog post is a automated script I created to run docker images. Schedulers are really the way to go like Kubernetes and Swarm.  I simply created a python script that will prompt the user and ask them how many containers and what commands they would like to run I posted it on Github here.


On this test we will create 20 containers.  Let them run then delete them and tear it all down.  We could easily create 20 iperf client streams to a server if we wanted to for example.


Screenshot from 2016-05-16 17:35:33


Okay now lets go ahead and tear it all down!

Screenshot from 2016-05-16 17:36:20

Here is the code for anyone interested without going to git hub.


Screenshot from 2016-05-16 17:37:30


 


Source: Moving docker containers manually and automated.