Introduction to Docker

Default featured post

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 this link.

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 favorite 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

  • Find the related Docker process
$ sudo docker ps –a
  • Find your container ID from the list
  • Commit the changes to container
$ 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,

  • Export (export current container)
$ sudo docker ps –a 
  • Save (export image)
$ 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