Learn How To Use Docker Images Part 2

0
1243
use-docker-images

use-docker-images

In part 1 of Docker images tutorial, we learned how to search for images on Docker hub and download them to a local machine. We demonstrated how to run downloaded images in part 1 of the tutorial. In that tutorial we noted that although images downloaded from Docker hub are a good starting point we often require customized images. An image can be modified in two ways which are listed below:

  • By using Dockerfile that contains instructions that will be used to create an image
  • By updating a container that was created from the image you intend to modify and committing to the image

In this tutorial we will focus on how to create custom images using Dockerfiles.

Basically a Dockerfile is a script containing operating system and Dockerfile commands. Before we can begin creating dockerfiles you need to understand the structure of a dockerfile. There are key dockerfile commands that you need to understand before you are able to create Dockerfiles. The FROM command is placed at the top of the Dockerfile and it specifies the base image you will use to create your new image. The optional MAINTAINER command specifies the name of the developer who maintains the image. The RUN command is used to specify any commands that you would like to run in the process of building your image. To copy resources like files from your local machine file system to the Docker image you are building you use the ADD command. When using the ADD command you can also specify a URL from which resources will be downloaded and copied to your Docker image. To specify an environment variable you use the ENV command. Execution of commands is possible through the CMD command. A default command that executes when a container is running is specified using the ENTRYPOINT command. A user id for a container that we will create from our image is specified using USER command. When you would like to have access between your docker directory and your local machine you use the VOLUME command to enable that. To add comments you add a hashtag (#) before anything you would like considered a comment. With a good background on commands that are useful in creating Dockerfiles we can go ahead and demonstrate how to create a Dockerfile.

The first step in creating a Dockerfile is to create a directory and add an empty dockerfile in our created directory. The commands below will do that

file-directory

After creating the empty Dockerfile, you edit it using your favorite editor to add commands that will be used in creating your image. Open the Docker file in an editor using the command below and add your commands.

add-dockerfile

Before you begin building your image it is important to identify the components that will make up your image. In the Dockerfile below, we are creating an image with a base ubuntu image containing Nginx and PHP-FPM 7. Add the commands highlighted in yellow to your Docker file, save and close.
#Download base image ubuntu 16.04
FROM ubuntu:16.04
# Update Software repository
RUN apt-get update
# Install nginx, php-fpm and supervisord from ubuntu repository
RUN apt-get install -y nginx php7.0-fpm supervisor && \
rm -rf /var/lib/apt/lists/*
#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.0/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf
# Enable php-fpm on nginx virtualhost configuration
COPY default ${nginx_vhost}
RUN sed -i -e ‘s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g’ ${php_conf} && \
echo “\ndaemon off;” >> ${nginx_conf}
#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}
RUN mkdir -p /run/php && \
chown -R www-data:www-data /var/www/html && \
chown -R www-data:www-data /run/php
# Volume configuration
VOLUME [“/etc/nginx/sites-enabled”, “/etc/nginx/certs”, “/etc/nginx/conf.d”, “/var/log/nginx”, “/var/www/html”]
# Configure Services and Port
COPY start.sh /start.sh
CMD [“./start.sh”]
EXPOSE 80 443

After creating a Dockerfile the next step is to create a configuration file. In the case of the Docker file we created, we need to create three configuration files. The first configuration file named default is for the virtual host. The second configuration file named supervisord.conf is for the supervisord. The third configuration file named start.sh is for a service.

In the directory where we created the dockerfile, create a default file and add the content highlighted in yellow below, save and close the file.

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

default-conf

Create a supervisord file and add the contents highlighted in yellow below.

[unix_http_server]
file=/dev/shm/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
user=root ;
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the “files” setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
[program:php-fpm7.0]
command=/usr/sbin/php-fpm7.0 -F
numprocs=1
autostart=true
autorestart=true
[program:nginx]
command=/usr/sbin/nginx
numprocs=1
autostart=true
autorestart=true

supervisor

Create a start.sh file and add the content highlighted in yellow, save and close.

#!/bin/sh
/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

start

We need to make our start.sh executable by running the command below:

Once we have created our Dockerfile and associated configuration files, our image is ready to be used to create containers. To build an image using all files in our directory we issue the command below

build-image

Once the build process is complete, we can use sudo docker images to list images on our local machine.

In this tutorial we reviewed material covered in part 1. We discussed commands that are important in creating dockerfiles. We created a dockerfile that has an ubuntu image and PHP with its associated configuration files. Finally, we built an image using the dockerfile we created.

LEAVE A REPLY

Please enter your comment!
Please enter your name here