Skip to content

A simple guide to get you started on docker. (Might be outdated but it should still give you a basic overview.)

License

Notifications You must be signed in to change notification settings

vasuadari/docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting started with docker

Contents

  1. Introduction
  2. Installation
  3. Basics of docker
  4. Dockerize Applications

Introduction

Using docker you can wrap your application with all its dependencies into a container by having your host machine clean.

Installation

Follow below links to setup docker on your machine.

Ubuntu

Mac OS X

Basics of docker

Before getting started with docker, you need to understand below terminologies.

docker-machine

Docker follows client-server architecture. docker-machine is a command to create Virtual machine with a minimal linux os running on it. All the images that you build will be hosted on this server.

docker

docker is a command to interact with your docker-machine. Think of it as a client which talks to a server(docker-machine) to get the work done.

Image

It is a pre-built image like the ones you download for OSes which can be ran on the container.

Container

Light weight virtual machine which runs your image.

Dockerfile

It is a manifest file which is used for building your own images. This manifest file contains set of instructions which guides docker to run list of commands to setup your application. Docker rebuilds the image only if it finds any new instruction after the first build.

Below is the sample Dockerfile

# Filename: Dockerfile
# Pull image from the docker hub
FROM ruby

RUN which ruby

On mac, you may need to start docker Quickstart Terminal before trying out docker commands.

Now, run docker build -t myruby . within the same directory. Your output will look like the below. Use -t option to name your image.

Sending build context to Docker daemon 197.7 MB
Step 1 : FROM ruby
 ---> c6ebc3270d1c
Step 2 : RUN which ruby
 ---> Running in 425293ec8f98
/usr/local/bin/ruby
 ---> bc3c6f463349
Removing intermediate container 425293ec8f98
Successfully built bc3c6f463349

If you try to rerun the same command, you will find that docker says that it is Using cache.

Sending build context to Docker daemon 197.7 MB
Step 1 : FROM ruby
 ---> c6ebc3270d1c
Step 2 : RUN which ruby
 ---> Using cache
 ---> bc3c6f463349
Successfully built bc3c6f463349

Run docker images to find your images.

To start a new container, run docker run -t myruby which will start the irb console. To enter into the container with new TTY, run docker run -it myruby /bin/bash.

Docker hub

It is a public repository which hosts all the docker images.

Dockerize Applications

Step by step guide to move things to docker.

Rails on docker

Requirements:

  • ruby
  • build-essential
  • nokogiri dependencies
  • nodejs

Dockerfile:

# Pull image from the docker hub
FROM ruby

RUN apt-get update -qq && apt-get install -y build-essential

# Nokogiri dependencies
RUN apt-get install -y libxml2-dev libxslt1-dev

# JS runtime dependencies
RUN apt-get install -y nodejs

RUN gem install rails

Run docker build -t rails . to create a new rails image.

Rails application

Lets take a sample rails application which uses MySQL database with redis and a sidekiq for running background jobs.

Docker compose

It's a tool provided by docker(You may need to install this separately from here) which takes a YAML file with all the components, links and the ports to be exposed. And it will take care of linking, exposing ports, mounting volumes etc. This command will look for docker-compose.yml file or you can also pass the custom YAML file to it.

Sample docker-compose.yml may look like this:

redis:
  image: redis
  ports:
    - '6379'

Run docker-compose build inside the directory which contains docker-compose file to build all the images and then do docker-compose up to start the services. When you run build, it will fetch latest redis image from the docker hub since we are using image option. When do docker-compose up, redis container will be started in its default port which we are exposing it to our host machine by setting it in the ports options.

Creating Dockerfile

Lets create a Dockerfile with ruby and install bundler.

# Pulls latest ruby version from the docker hub
FROM ruby

RUN apt-get update -qq && apt-get install -y build-essential

# Nokogiri dependencies
RUN apt-get install -y libxml2-dev libxslt1-dev

# JS runtime dependencies
RUN apt-get install -y nodejs

RUN apt-get install -y mysql-client

RUN gem install bundler

ENV BUNDLE_PATH /bundle

Creating docker-compose file

Create a docker-compose file within the same directory where you have created Dockerfile. Typically you will have these files inside the root of your rails application.

MySQL:

For MySQL server you would need a persistent storage meaning you wouldn't want to have data inside the same container. For these reasons, docker provides you with data volume containers or you could use data volume. Lets create a data volume container for MySQL. Data volume container is a container which will save your data but use a light weight and existing image for running these containers.

Now, lets create a docker-compose file with mysql data volume.

Filename: docker-compose.yml

mysql_data_volume:
  image: redis
  volumes:
    - /var/lib/mysql
  command: echo 'Data container for mysql'

We have named the image as mysql_data_volume. Here we are using redis as a light weight image and /var/lib/mysql is a path that is created in this container. Whenever we start the services, docker runs a startup command. For data containers, you don't want your containers to be running all the time. So, we just run a command which will create a container and exit. But, it will not be removed.