Category Archives: Docker

Docker

Docker Concepts

If you need to have multiple instances of different ROS or Ubuntu version, one solution is a virtual machine which puts lots of overhead on the hardware and makes it very slow. A good alternative for this problem is Docker. Docker is a tool that can package an application and its dependencies in a virtual container that can run on any Linux server [1].

Some analogy to OOP:

Images are similar to classes. Images are made of layers, conceptually stacked on top of each other, each layer can be added, changed, removed. Images can share layers. An image is read only.
Layers are similar to inheritance.
Containers are similar to instances. It is a copy of an image

Basically, you create images and spin of containers from images that you can work with.

Installation

Here I have installed the docker community edition that on Ubuntu 16.04 (Xenial Xerus) [2].

1) First, make sure you don’t have any old version of docker on your system:

2) Add required packages:

 

3) Add Docker’s official GPG key:

4) Add the repository:

 

5) Install the docker:

6) Now you ready to start, but first fix the permission problem:

Hands on docker

run command will start a container from a given image:

-i: means interactive
-t: means tty

If the image doesn’t exist it will pull it from hub.docker.com

Basically it runs the pull command beforehand:

To see available images on your machine:

You can search directly from the terminal for images:

Now run:

You can give it a name so when you want to connect to that via exec you can the name instead of container id

If you need to expose the ports, so your docker would be accessible from outside:

And you can type exit to leave.

To see the running containers:

To see all containers (running/ stopped):

The following command has the same output as docker ps, but it is a more update command to its better to use this one

To terminate a container:

“kill” will terminate the container immediately.

Next time you want to run your container you can use the name you gave to it :

Now if we install some packages inside our container, we can use docker diff to the changes we made to our container:

to get into a Docker container:

to find where the images and dockers are stored:

which on Ubuntu they are under:

to remove a container:

Remove all containers:

To remove all of unused containers at once:

to remove an image:

if you have a container which has been spun from an image, you need to delete it before removing an image, or you can use the force option:

to list dangling images:

to remove dangling images:

Creating Images

We can use the followings to build an image from our container and make a new image:

  • commit
  • build

 

Commit

commit will create an image from your container:

Build

What build does is pulling images, running an  intermediate container from them, run your commands on them and finally commit that and build and image, so it is basically an automated commit.

Example 1

First search in the hub.docker.com for an image i.e. “Ubuntu” to find the right name then create a file and rename it “Dockerfile” and add the followings to that:

Now you can build your custom image:

Example 2

First search in the hub.docker.com for PHP and from the list pick Apache, at the current time the latest stable Apache is 7.1.11-apache. Just like CMake, create a file and name it Dockerfile and add the following line into that:

create a src directory and add the index.php and simply put a hello world message in it:

now we ready to build it:

After a successful build, list all your images, and you should the image that you just created:

but in our case, since we need to expose the port 80 we need to add a line:

Now you should be able to see your running container:

to stop your container:

If you make changes to your project, you need to build it and create another image, which might be time-consuming. To avoid this, you can share directories which are called volume:

which in my case:

Now any changes to src directory in your docker project will be seen immediately in the corresponding image file and consequently in the running container.

Volume

We can map a directory on the host machine into a directory into the docker. This is called volume.