Environment Setup
A single CentOS 6.5 virtual machine is required, along with febootstrap and docker intsalled.
Installing febootstrap
yum install -y yum-priorities && rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
yum -y install febootstrap
Installing Docker
yum -y install docker-io
service docker start
The image creation process must be performed under the root account; using a regular user may lead to various issues.
Creating the Base Image
febootstrap -i bash -i wget -i yum -i iputils -i iproute -i man -i vim -i openssh-server -i openssh-clients -i tar -i gzip centos6 centos6-image http://mirrors.aliyun.com/centos/6/os/x86_64/
Upon copmletion, a directory named centos6-image will be created.
cd centos6-image
cp etc/skel/.bash* root/
tar -c . | docker import - centos6-base
Verifying the Image
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos6-base latest 8f94e0bebd19 6 hours ago 399.4 MB
Starting a Container
docker run -itd 8f94e0bebd19 bash
2e943abb130a32830dd8580ab9345dd514b7b39597559cd08a790a8754ef56f1
docker exec -it 84f48b314bfa bash
[root@2e943abb130a /]#
Once inside the container, applications such as PHP and Nginx can be installed. Init scripts for Nginx and PHP-FPM are added to /etc/init.d/ (optional).
Enabling Application Startup on Container Boot
Multiple methods exist to manage startup services with in containers, including Supervisord and shell scripts. Here we use a shell script approach:
mkdir /usr/local/scripts
Create /usr/local/scripts/init-bashrc.sh:
#!/bin/sh
function service_start()
{
for SERVICE in nginx php-fpm sshd
do
if ! (ps ax | grep -v grep | grep $SERVICE > /dev/null)
then
/etc/init.d/$SERVICE start;
fi
done
}
service_start
Modify ~/.bashrc:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -l'
alias vi='vim'
/usr/local/scripts/init-bashrc.sh
# User specific aliases and functions
Remember to source the file: source ~/.bashrc. Exit the container with Ctrl+D.
Committing the Container
docker commit 2e943abb130a centos6-nginx-php56:v1
d85f477d9e00d12c380ebc442d544e0d8c9dd8ee86441e0885bb54f636c18ec1
After committing, a new image appears in docker images.
Launching the New Image
docker run -it centos6-nginx-php56:v1
Starting nginx: [ OK ]
Starting php-fpm: [ OK ]
Starting sshd: [ OK ]
[root@36abc5805a89 /]#
Ports can also be exposed during launch, and the root password modified. This can be achieved via a Dockerfile.
Example Dockerfile:
FROM centos6-nginx-php56:v1
MAINTAINER Tempted
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN echo "root:123456" | chpasswd
EXPOSE 22 80
CMD ["bash"]
Building the Final Image
docker build -t centos6-lnp:v1 .
Sending build context to Docker daemon 15.87 kB
Step 0 : FROM centos6-nginx-php56:v1
---> d85f477d9e00
Step 1 : MAINTAINER Tempted
---> Running in 46ec3f0fab12
---> 4ed3d31476d0
Removing intermediate container 46ec3f0fab12
Step 2 : RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
---> Running in 48ece83ad435
---> c02f08cf9b19
Removing intermediate container 48ece83ad435
Step 3 : RUN echo "root:123456" | chpasswd
---> Running in 22d1086800ca
---> 18db13e52113
Removing intermediate container 22d1086800ca
Step 4 : EXPOSE 22 80
---> Running in 8a65e01a0e84
---> 63a5bd61ba88
Removing intermediate container 8a65e01a0e84
Step 5 : CMD bash
---> Running in 671572d70abf
---> d0c9c51584ee
Removing intermediate container 671572d70abf
Successfully built d0c9c51584ee
Three images should now appear when listing docker images.
Starting the New Image
docker run -itd d0c9c51584ee bash
bc10a9e77a8c8b9eb12a7da16d6513ffa11a686d29dc86e050e702fac96a6109
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc10a9e77a8c d0c9c51584ee "bash" 20 seconds ago Up 17 seconds 22/tcp, 80/tcp cocky_morse
Check network interfaces:
docker exec -it bc10a9e77a8c ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:51
inet addr:172.17.0.81 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:51/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:41 errors:0 dropped:0 overruns:0 frame:0
TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5060 (4.9 KiB) TX bytes:5179 (5.0 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Connecting via SSH
ssh 172.17.0.81
root@172.17.0.81's password:
Last login: Fri Nov 3 17:23:16 2017 from 172.17.42.1
[root@bc10a9e77a8c ~]#
Exporting the Image
docker save centos6-lnp:v1 > centos6-lnp.tar