Now that we have images ready, let’s start dockerizing some applications to play with. I will start with dockerizing nginx and serve some static html content from it.
To dockerize an application, you need 2 things:
- 1. The base image which will be used to build the new image from
- 2. A dockerfile
Now what’s a dockerfile?
Dockerfile is a text file that describes how and what will the new image do. It contains command that you would execute normally when installing and configuring applications.
Now the first thing is to choose which image you want to build nginx on. I’ll choose the previously created image from Debian Wheezy.
So let’s start writing our Dockerfile…
I always put some description, the author and the usage of the image on the top, so create a new file (nginx.docker) and put:
# nginx: Dockerfile for building nginx images
# Based on aossama/wheezy
# Version 0.1
# Author "Ahmed Ossama <firstname.lastname@example.org>
MAINTAINER Ahmed Ossama "email@example.com"
apt-get update && apt-get -y upgrade && \
apt-get install -y nginx-full && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
mkdir /data && chown -R www-data:www-data /data && \
echo 'root:0101848876' | chpasswd
# Expose ports
# Define mountable directories.
VOLUME [ "/data", "/etc/nginx/sites-enabled", "/var/log/nginx"]
# Define working directory
# Define default command
Now this is a simple docker file which builds layers of images in these steps:
- 1. A new image FROM aossama/wheezy
- 2. With a maintainer details
- 3. After than run these commands in a new container
- 4. Then expose port 80
- 5. And port 443
- 6. The image should have these mount points available
- 7. When the container starts, use this working directory by default
- 8. Finally run nginx
Now build the new image:
$ cat nginx.docker | docker build -t aossama/nginx -
And inspect the images, if the build finish successfully you should find the new image in the list.
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
aossama/nginx latest a1523342e224 7 minutes ago 336.7 MB
Your image is ready to run in a container, to run it:
$ docker run -d --name=nginx aossama/nginx
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4fa1e7cf65e6 aossama/nginx:latest nginx 4 seconds ago Up 3 seconds 443/tcp, 80/tcp nginx
Inspect the new image and grep the IP address:
$ docker inspect nginx | grep IPAddress
If you opened a browser and point to this IP address, you should see the welcome page of nginx.