Introduction to Docker

Docker is an open-source project that makes creating and managing Linux containers really easy. Containers are like extremely lightweight VMs – they allow code to run in isolation from other containers but safely share the machine’s resources, all without the overhead of a hypervisor.

First thing you need to do is installing Docker package for host OS.
Ubuntu 14.04 LTS

$ sudo apt-get update
$ sudo apt-get install docker.io

For other version of Ubuntu check below link,
https://docs.docker.com/installation/ubuntulinux/#ubuntu-trusty-1404-lts-64-bit
CentOS
https://docs.docker.com/installation/centos/

Powering up a new Docker instance

$ sudo docker run ubuntu /bin/echo hello world

If Ubuntu image does not exist it fetches from Docker repository.

Finding your favourite OS image

$ sudo docker search ubuntu
$ sudo docker search debian
$ sudo docker search centos

Running a Docker and login to bash

$ docker run –t –I [container name] [process/app]
$ docker run –t –I linux /bin/bash

Running a Docker to to listen on particular port

$ docker run –t –I –p [ host:guest ] [container name] [process/app]
$ docker run -t -i -p 8080:8080 linux /bin/bash

Commit the changes to Docker hard disk

  1. Find the related Docker process.
  2. $ sudo docker ps –a
  3. Find your container ID from the list
  4. Commit the changes to container
  5. sudo docker commit e907a8e56803 linux

Copy file from host over Docker
The path that Docker(s) located on host OS is

$ /var/lib/docker/aufs/mnt/
Example,
$ cp postgresql-9.2.4.tar.gz  /var/lib/docker/aufs/mnt/

Cloning Docker images/containers
Two ways possible,

  1. Export (export current container)
  2. Save (export image)

Export
Find docker process

$ sudo docker ps –a

Then save it to disk

$ sudo docker export  > /home/export.tar

How to import the file again?

$ cat /home/export.tar | sudo docker import - busybox-1-export:latest

Save

$ sudo docker save busybox-1 > /home/save.tar

How to import it again?

$ docker load < /home/save.tar

Show available images

$ sudo docker images

Show in tree format

$ sudo docker images –tree

Resolving busy/occupied port problem
Error :
Docker. Error: Cannot start container: port has already been allocated
How to resolve?
From the docker ps output, there is a container which is listening on port 5000 as you can see from the 0.0.0.0:5000->5000/tcp under the ports column.
You can kill this container with docker kill 3fdfc9ecf30f. At which point it will free up the port.

How to avoid losing data without commit
This part needs more investigation but for temporary solution,
First solution,
When you use docker run to start a container, it actually creates a new container based on the image you have specified.

Besides the other useful answers here, note that you can restart an existing container after it exited and your changes are still there.

docker start f357e2faab77 # restart it in the background

docker attach f357e2faab77 # reattach the terminal & stdin
Second solution,

Docker auto commit package

Third solution,

Keep important data such as database on host and just run your app on container. Hence, needs for commit would be occasionally.

References

https://github.com/rcmorano/docker-autocommit

http://stackoverflow.com/questions/19585028/i-lose-my-data-when-the-container-exits

http://stackoverflow.com/questions/24993704/docker-error-cannot-start-container-port-has-already-been-allocated

http://tuhrig.de/difference-between-save-and-export-in-docker/

http://www.jamescoyle.net/how-to/1503-create-your-first-docker-container

https://coreos.com/docs/launching-containers/building/getting-started-with-docker/

Send your idea and information to kasra@madadipouya.com

Leave a Reply