First Sailing on Docker
Docker is a technology providing an additional layer of abstraction on your OS.
Install Docker CE (Community Edition)
For installing Docker please follow the [official guide] (https://docs.docker.com/install/)
Docker images
Docker introduce the concept of images
and containers
, thus the concept of containerization. Think of images
as blue prints of containers
.
Big names in technologies have their projects hosted inside (Docker Hub)[https//www.hub.docker.io]
Docker uses the resource isolation features of the linux kernel (cgroups), kernel namespaces and a union capable file system (overlayFS) to create independent containers. These containers run within a single Linux instance. Thus the overhead of duplicating a OS entirely to virtualize it is being avoided.
In order to check the available images on your local machine
docker images
Running it the first time you might get none. You can search then fetch images from hub:
docker search composer
docker pull composer
Tips.
Don’t create large images. Instead split into small containers and you deploy them using either docker deploy
or docker-compose
. Don’t create images from running containers (don’t use docker commit to create an image). Always use a Dockerfile (that is always reproducible so you can track changes) and store it into a source control repository (git).
Docker containers
Containers are immutable, lightweight and fast. Containers are “ephemeral”.
Following the above example: You can list the containers
docker ps
Then you can run the package, simply as that
docker run composer
docker -rm --interactive composer install
Don’t store data into containers. Don’t run more than one process into a single container. Don’t store credentials. Use environment variables instead Don’t run processes as a root user. Don’t rely on IP addresses
(a) Instead create a base image for your OS (1), another for the username definition (Dockerfile) (2) and another for the runtime installation, another later for configuration and finally another for your application.
Docker Hub
Get docker image [DOCKER_ACCOUNT]/[PACKAGE]
docker pull wadmiraal/drupal:7.41
then run it
mkdir -p modules/ themes/
docker run -d -p 8083:80 -p 8024:22 -v `pwd`/modules:/var/www/sites/all/modules/custom -v `pwd`/themes:/var/www/sites/all/themes wadmiraal/drupal
Run Existing Docker Containers
Use pull
command.
docker search drupal
docker pull wadmiraal/drupal
docker run -d -p 8083:80 -p 8024:22 -t wadmiraal/drupal
Port 8083
is pushed to 80
internal (belonging to container).
Remove or stop all containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi container-id
docker rm `docker ps -aq`
Manage and Build Your Own Custom Containers
You will do it by creating a filename called Dockerfile
. Then you will use build
command to build it.
Create a Dockerfile
to install nodejs
# Based on the Fedora image created by Matthew Miller.
FROM mattdm/fedora:f19
# Install nodejs and npm packages.
RUN yum update -y
RUN yum install -y --skip-broken nodejs npm
# Clean up
RUN yum clean all
# Start a server listening on 8080 using nodejs
ADD . /src
RUN cd /src; npm install
EXPOSE 8080
CMD ["node", "/src/server.js"]
As you can see you start by pulling from an existing image and get started customizing by running RUN
commands.
# ====================================================================================================
# This file contains all the commands a user could call on the command line to assemble an image.
# Based on this file and your OS context (local files PATH and URL) the command `docker build Dockerfile` will create a build.
# The build will be run by a component called docker daemon.
# ====================================================================================================
# Pulling repository docker.io/library/debian.
FROM debian:jessie
MAINTAINER Your Name <your.name@email.here>
ENV DEBIAN_FRONTEND noninteractive
# Update the repository sources list once more
RUN apt-get update
# Note: In order to effectively utilize the cache keep common instructions at the top of the Dockerfile and only add the alterations at the end.
RUN apt-get install -y \
vim \
git \
apache2 \
php5-cli \
php5-mysql \
php5-gd \
php5-curl \
php5-xdebug \
php5-sqlite \
libapache2-mod-php5 \
curl \
mysql-server \
mysql-client \
phpmyadmin \
wget \
RUN apt-get clean
# Setup PHP.
RUN sed -i 's/display_errors = Off/display_errors = On/' /etc/php5/apache2/php.ini
RUN sed -i 's/display_errors = Off/display_errors = On/' /etc/php5/cli/php.ini
RUN a2enmod rewrite
RUN /sbin/service mysqld start; /usr/bin/mysqladmin -u root password "password"; /usr/bin/mysql -uroot -ppassword -e "CREATE DATABASE dev; CREATE USER 'dev'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON dev.* TO 'dev'@'localhost'; FLUSH PRIVILEGES;"
The second step is to build the container by running the Dockerfile.
docker build --rm=true -t ${DOCKER_USER}/${DOCKER_IMAGE} . < Dockerfile
docker run -d -p 8080:80 -v ${PATH_TO_MOUNT}:/var/www:ro ${DOCKER_USER}/${DOCKER_IMAGE} /bin/bash -c '/sbin/service mysqld start; /sbin/service httpd start; tail -f /var/log/httpd/error_log &> /dev/null'
The -d
allow running the container as a service (daemon) instead of one time, then exiting.
Now in case you need to connect to bash inside the console all you need is to:
Find the name of the container
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7c780ac63f2 fpfis/php56-dev "/run.sh" 6 days ago Up 6 days 0.0.0.0:9000->8080/tcp compose_web_1
cf4f884d824f fix/fb:2 "/bin/bash /root/r..." 4 weeks ago Up 2 weeks compassionate_jepsen
Execute bash in the specify container. Specify the container id but only first 2-3 letters
docker exec -it b7 bash
Now you will be taken inside the inner bash.
You don’t want to connect inside the container’s bash you simply execute one command.
docker exec -it b7 bash composer
Shutdown the container
docker down
Or kill it
docker kill b7
References