Penflip

Penflip - Write better with others

  • Loading...
  • Discover Projects
  • Help
  • Signup
  • Login
  • Welcome back!
    No account? Signup Close
    Ready to write better?
    Have an account? Login Close

akira.ohio · AppCatalyst Hands-on Lab en

Make Changes
5

4. Let's use Dockerfile

You can build Docker Images using writing the instructions in Dockerfile.
docker-ppt-dockerfile-4.png

We are going to use some basic dockerfile instructions to get the idea.

4-1. Dockerfile 101

  1. Create workspace directory on you Mac
    $ mkdir -p ~/workspace/docker/dockerfiles
    $ cd ~/workspace/docker/dockerfiles

  2. 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 apache2ctl

  3. Build 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

  4. 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]

  5. 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
    
  6. Check to see if your web server is up and running.
    docker-dockerfile-apache01.png

    Apache2 default page is shown.

  7. Stop the container
    $ docker stop $(docker ps -p)


4-2. Use ADD instruction to add files to docker image

  1. Create a Web page(index.html)
    $ vi index.html

    <h1>Who's your Daddy?</h1>
    
  2. 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)

  3. 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

  4. Run container from apache-02
    $ docker run -itd -p 80:80 apache-02

    [AKIRA@^_^:dockerfiles]$ docker run -itd -p 80:80 apache-02
    df0b304667534562e7219e7be11192f80f2621d5417c76d79d6cd0ad3a75b0ae
    
  5. 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
    
  6. Access the Web Site using browser.

    docker-dockerfile-apache02.png

    You can see that index.html you specified was ADDed to new image and container.

  7. Stop the container.
    $ docker stop $(docker ps -q)


4-3. Volume

  1. 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"]
    
  2. 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
    
  3. 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 container

  4. IP 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
    
  5. Access the web site with browser
    docker-dockerfile-apache021.png

  6. Edit your local index.html
    $vi index.html

    <h1>Who's got the last laugh now?</h1>
    
  7. Reload your browser
    docker-dockerfile-apache03.png

    You didn't have to re-build the image after editing index.html.

  8. Stop the container
    $docker stop $(docker ps -q)

Updated by akira.ohio about 4 years (view history)
3. Let's Get Used to Docker 5. Using Docker Compose

Contents

    1. AppCatalyst & Docker Lab 2. Docker & AppCatalyst Setup 3. Let's Get Used to Docker 4. Let's use Dockerfile 5. Using Docker Compose 6. Using Docker Swarm
    Discussions 0 Pending changes 0 Contributors
    Download Share

    Download

    Working...

    Downloading...

    Downloaded!

    Download more

    Error!

    Your download couldn't be processed. Check for abnormalities and incorrect syntax. We've been notified of the issue.

    Back

    Download PDF Download ePub Download HTML Download Word doc Download text Download source (archive)

    Close
615 Words
5,790 Characters

Share

Collaborators make changes on their own versions, and all changes will be approved before merged into the main version.

Close

Penflip is made by Loren Burton
in Los Angeles, California

Tweet

    About

  • Team
  • Pricing
  • Our Story

    Quick Start

  • Markdown
  • Penflip Basics
  • Working Offline

    Support

  • Help
  • Feedback
  • Terms & Privacy

    Connect

  • Email
  • Twitter