4. Let's use Dockerfile
You can build Docker Images using writing the instructions in Dockerfile.
We are going to use some basic dockerfile instructions to get the idea.
4-1. Dockerfile 101
Create workspace directory on you Mac
$ mkdir -p ~/workspace/docker/dockerfiles
$ cd ~/workspace/docker/dockerfiles
Create Dockerfile
$ vi df-apache-01
FROM ubuntu:14.04 RUN apt-get -y install apache2 CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
We use ubuntu 14.04 as a base image.
Install apache2 on the new image.
When you run container from the new image, execute the apache2ctlBuild docker image using Dockerfile
$ docker build -f df-apache-01 -t apache-01 .
Don't forget . (current directory) at the end.
[AKIRA@^_^:dockerfiles]$ docker build -f df-apache-01 -t apache-01 . Sending build context to Docker daemon 6.144 kB Step 0 : FROM ubuntu:14.04 14.04: Pulling from library/ubuntu
You just built apache-01 docker image.
$ docker images
Run container from newly built image binding port 80
$ docker run -itd -p 80:80 apache-01
-d flag means detached mode
[AKIRA@^_^:dockerfiles]$ docker run -itd -p 80:80 apache-01 8522deea82e2ec44aedbf4617bf32387ed9917b070a9e6993f4dca42c06fd5ba
This docker run command binds port 80 on the container to port 80 on the host with -p flag.
NOTE:
-p [host port no] : [container port no]Get the IP Address of your Docker Host (vm-appcat01)
$ docker-machine ls
[AKIRA@^_^:dockerfiles]$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM vm-appcat01 * vmwareappcatalyst Running tcp://172.16.194.149:2376
Check to see if your web server is up and running.
Apache2 default page is shown.
Stop the container
$ docker stop $(docker ps -p)
4-2. Use ADD instruction to add files to docker image
Create a Web page(index.html)
$ vi index.html
<h1>Who's your Daddy?</h1>
Create Dockerfile
$ vi df-apache-02
FROM ubuntu:14.04 RUN apt-get update && apt-get -y install apache2 ADD index.html /var/www/html/index.html EXPOSE 80 CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND" ]
Look at the "ADD" line.
This instruction is to ADD index.html on your current directory to the path in docker image(/var/www/html/index.html is the default web page)
Build
$ docker build -f df-apache-02 -t apache-02 .
[AKIRA@^_^:dockerfiles]$ docker build -f df-apache-02 -t apache-02 . Sending build context to Docker daemon 6.144 kB Step 0 : FROM ubuntu:14.04 ---> 91e54dfb1179 Step 1 : RUN apt-get update && apt-get -y install apache2 ---> Running in 12f7c312923a
You created the image called apache-02
$ docker images
Run container from apache-02
$ docker run -itd -p 80:80 apache-02
[AKIRA@^_^:dockerfiles]$ docker run -itd -p 80:80 apache-02 df0b304667534562e7219e7be11192f80f2621d5417c76d79d6cd0ad3a75b0ae
Check the IP Address
$ docker-machine ls
[AKIRA@^_^:dockerfiles]$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM vm-appcat01 * vmwareappcatalyst Running tcp://172.16.194.149:2376
Access the Web Site using browser.
You can see that index.html you specified was ADDed to new image and container.
Stop the container.
$ docker stop $(docker ps -q)
4-3. Volume
Create Dockerfile
$ vi df-apache-03
FROM ubuntu:14.04 VOLUME [ "/var/www/html" ] ADD index.html /var/www/html/index.html RUN apt-get update && apt-get -y install apache2 EXPOSE 80 CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Build
$ docker build -f df-apache-03 -t apache-03 .
[AKIRA@^_^:dockerfiles]$ docker build -f df-apache-03 -t apache-03 . Sending build context to Docker daemon 6.144 kB Step 0 : FROM ubuntu:14.04 ---> 91e54dfb1179 Step 1 : VOLUME /var/www/html ---> Running in 899612eb6fd4 ---> e718ae690d57 Removing intermediate container 899612eb6fd4 Step 2 : ADD index.html /var/www/html/index.html ---> 62b0180d278e Removing intermediate container 56dee1e0b6ff Step 3 : RUN apt-get update && apt-get -y install apache2 ---> Running in c69ae7b84d75
Run
$ docker run -itd -v ~/workspace/docker/dockerfiles/index.html:/var/www/html/index.html -p 80:80 apache-03
[AKIRA@^_^:dockerfiles]$ docker run -itd -v ~/workspace/docker/dockerfiles/index.html:/var/www/html/index.html -p 80:80 apache-03 6f580d359ea82ace71469b13a5f43bc5a7bf130401f1454fa98929417560e9e6
-v flag
mounting local index.html to /var/www/html/index.html in the containerIP Address of vm-appcat01
$ docker-machine ls
[AKIRA@^_^:dockerfiles]$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM vm-appcat01 * vmwareappcatalyst Running tcp://172.16.194.149:2376
Access the web site with browser
Edit your local index.html
$vi index.html
<h1>Who's got the last laugh now?</h1>
Reload your browser
You didn't have to re-build the image after editing index.html.
Stop the container
$docker stop $(docker ps -q)