From Mageia wiki
Jump to: navigation, search

Foreword

This page is not aimed to replicate the docker documentation here, normally when someone is trying to use a docker image they already know how containers work and probably also know how to use them.

There are several ways to use a docker image depending on the platform you are running it, be it locally, or on a kubernetes cluster, on nomad, docker swarm, mesos, etc., it would be a pretty long documentation.

Getting started

But basically, what you need to do is to tell docker to create a volume where it will store the container data. There are plenty of ways to define a volume, handled by docker internally, in memory, using a storage plugin or with a bind volume, which is the one I'm going to use with this example as it is the one that shows more clearly the volume concept:

$ docker run -ti -v $PWD/data:/data mageia:9 bash
Unable to find image 'mageia:9' locally
9: Pulling from library/mageia
Digest: sha256:ee8deeb5ab22773a38ee147c98127b2faa5edc72272beef5d497db44c4fda658
Status: Downloaded newer image for mageia:9
[root@e42b3483add1 /]# cd data
[root@e42b3483add1 data]# ls
[root@e42b3483add1 data]# touch test 
[root@e42b3483add1 data]# exit
exit
$ ls data
test

With -v $PWD/data:/data we are telling docker to define a volume that will use a directory in the host system (your machine) called $PWD/data and will be mounted inside the /data directory inside the container.

But running a container like that is not usually the way of doing it, this would be more useful for tests. What is normally done is to define a Dockerfile that uses this image as a base, and then install some packages that will give the resulting image a more defined purpose. For example, install nginx to run a web server.

FROM mageia:9
label MAINTAINER="Mageia Community"
#RUN urpmi.addmedia --distrib --mirrorlist '$MIRRORLIST'
RUN urpmi.update -a && urpmi --auto-select --auto 
RUN urpmi --auto nginx
# Define volumes this container uses
VOLUME ["/usr/share/nginx/html"]
# Define init command
CMD ["nginx", "-g", "daemon off;"]
# Expose ports
EXPOSE 80
EXPOSE 443

Then, to run this image:

$ ls 
Dockerfile
$ docker build -t mageia:nginx .
[...]
$ echo 'Hello!!' > data/index.html
$ docker run -ti -v $PWD/data:/usr/share/nginx/html mageia:nginx -p 80:80 

Then open up http://localhost on a browser.

That is why these Linux distribution official images are minimal installs, so each user can customize them with a Dockerfile to do whatever they need, being very specific and light, compared with a traditional virtual machine.

There is another tool called docker-compose that is useful when you need to run multiple containers locally, for example, a web server for a WordPress install and a MySQL database, you should check that out too.

Links

Reference page on Docker hub: [1]

Tools used to generate Mageia images: [2]