Quick start with Docker

Tác Giả Thuy Dang | Ngày 22 tháng 12 năm 2017 | Category Devops , guide | Tags




In this tutorial, we will create a docker container image from scratch. The image is built with vim, python packages, etc. One sample application of such container is to provide a Linux bash shell in Windows and mount some folders allowing access by contained program. Let's start!

Installation

Detailed guides to installation of docker are available here for Linux and here for Windows platforms. We only give some commonly encountered issues during installation and theirs solutions in this section.

Creating Docker Images

Docker builds images containing a bunch of programs for certain functionalities. The instructions for docker build is contained in a Dockerfile.

The Dockerfile

A sample Dockerfile looks like following. The image to be build is based on latest ubuntu version. Some environment variables are set for locale, a user is created and vim, python packages are installed and their configuration files are placed in the user's home folder.

FROM ubuntu:latest

MAINTAINER Thuy Dang

# environment variables
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && \
    apt-get install -y \
                tzdata \
                build-essential \
                python3 \
                python \
                python-dev \
                libxml2-dev \
                libxslt-dev \
                libssl-dev \
                zlib1g-dev \
                libyaml-dev \
                libffi-dev \
                python-pip \
                git \
                vim git wget fontconfig

# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
#ENV TZ=America/Los_Angeles
ENV TZ=Europe/Berlin

RUN pip install --upgrade pip \

                          virtualenv \
                          requests
RUN useradd dev && \
    echo "ALL            ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    dpkg-reconfigure locales && \
    locale-gen en_US.UTF-8 && \
    /usr/sbin/update-locale LANG=en_US.UTF-8

WORKDIR /home/dev
ENV HOME /home/dev
ENV LC_ALL en_US.UTF-8

RUN chown -R dev:dev $HOME
USER dev

# setup pathogen vim plugin manager
RUN mkdir -p $HOME/.vim/autoload $HOME/.vim/bundle && \
    wget -P $HOME/.vim/autoload https://tpo.pe/pathogen.vim && \
    echo "execute pathogen#infect()" >> $HOME/.vimrc && \
    echo "syntax on"                 >> $HOME/.vimrc && \
    echo "filetype plugin indent on" >> $HOME/.vimrc && \

# Powerline fonts
    mkdir -p $HOME/.fonts $HOME/.config/fontconfig/conf.d && \
    wget -P $HOME/.fonts                     https://github.com/Lokaltog/powerline/raw/develop/font/PowerlineSymbols.otf && \

    wget -P $HOME/.config/fontconfig/conf.d/ https://github.com/Lokaltog/powerline/raw/develop/font/10-powerline-symbols.conf && \
    fc-cache -vf $HOME/.fonts/ && \
    echo "set guifont=Droid\\ Sans\\ Mono\\ 10"
# echo "set guifont=Liberation\\ Mono\\ for\\ Powerline\\ 10"

CMD bash

Building images

Assume we are in a folder containing the Dockerfile.

cd ubuntu_cmd

docker build -t ubuntu_cmd_image:latest .

Note the dot at the end of the command.

Creating Containers

We create the container from the previous image by starting it.

docker run -it ubuntu_cmd_image bash

The option i tells docker to run the container in interactvie mode, otherwise it will execute the command then terminates.

The option t tells docker to open a pseudo tty (console) to run bash shell.

Extra Steps

Rebuilding images

When building images, we often want to add small adjustments to the Dockerfile then rebuild. To avoid a complete download and rebuilding process, the follow trick will help.

We will take the created image as a base with preinstlled packages and install additional packages or configurations. Create new Dockerfile.rebuild:

FROM ubuntu_cmd_image:latest

MAINTAINER Thuy Dang <write.thuy@gmail.com>

# environment variables
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=Europe/Berlin

RUN apt-get update && \
    apt-get install -y \
    sudo

RUN useradd -m dev && echo "dev:dev" | chpasswd && adduser dev sudo

RUN echo "ALL            ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers

WORKDIR /home/dev
ENV HOME /home/dev
ENV LC_ALL en_US.UTF-8

RUN chown -R dev:dev $HOME
USER dev

CMD bash

Here we only add -m option to adduser command, which will create user's home directory. Further we enable sudo to user dev. Rebuild the image:

docker build -f Dockerfile.rebuild -t ubuntu_cmd_image:new .

Create and run new container:

docker run -it vsrccom/ubuntu_cmd:new bash

The rebuild is very fast, but the resulting image is larger. A more complex multi-stage build is referred to in the last section.

Frequently Used Commands

Show available local images

docker image ls

REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
vsrccom/ubuntu_cmd   latest              595ce183954c        11 hours ago        630MB
                           16e3689cb294        12 hours ago        628MB
ubuntu               latest              00fd29ccc6f1        7 days ago          111MB
debian               latest              da653cee0545        10 days ago         100MB
d4w/nsenter          latest              9e4f13a0901e        15 months ago       83.8kB

Show all runing and terminated containers

 docker ps -a


CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                    PORTS               NAMES
1e08f4db8aa9        vsrccom/ubuntu_cmd   "bash"                   11 hours ago        Exited (0) 11 hours ago                       zealous_lumiere
5219f40e430a        595ce183954c         "/bin/sh -c 'usera..."   12 hours ago        Exited (1) 12 hours ago                       compassionate_murdock
023aa0ac12a3        16e3689cb294         "/bin/sh -c 'usera..."   12 hours ago        Exited (1) 12 hours ago                       eloquent_meitner
86b26c2e81e5        debian               "bash"                   12 hours ago        Exited (0) 12 hours ago                       tender_turing
29f50fcaf817        debian               "bash"                   12 hours ago        Exited (0) 12 hours ago                       focused_fermi
112c0e66bb8a        debian               "bash"                   12 hours ago        Exited (0) 12 hours ago                       hardcore_bassi

Cleanup images and containers

Further Readings

  • Multi-stage build: https://docs.docker.com/engine/userguide/eng-image/multistage-build/#name-your-build-stages

Share Article:

Real Time Web Analytics