4 Docker Images

  Docker

< 3 Docker Run | 5 Docker Compose >

19: Docker Images

https://www.udemy.com/learn-docker/learn/lecture/7894020#overview

How to create images

Manually

  • Start with OS
  • Upgrade apt repo
  • Install dependencies
  • Install Python
  • Copy source code to /opt
  • Run application

Creating a Dockerfile

INSTRUCTION argument

FROM ubuntu
RUN apt-get updateRUN install python
RUN pip install flask
RUN pip install flask-mysql
COPY . /opt/source-code
ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run
  • All images must start with a Docker OS image
    • RUN Ubuntu
  • Install the dependencies
  • Copy the source code
  • Specify the entry point.

Layered Architecture

  • Each line of the file is a layer
  • Each line of instruction is just the changes from the previous layer

Docker Build

Use the docker build command to create the image from the dockerfile.

  • Each build step is cast (id’d?) so you can restart a build in the event a step failed or required modification.
  • Only the layers below the changed layer need to be rebuilt.

What can you containerize?

  • Everything
  • It is expected that one day, all applications will be containerized.
    • sure… riiigghhtt

20: Demo – Creating a new Docker Image

https://www.udemy.com/learn-docker/learn/lecture/7894022#overview

Start with the OS

docker run -it ubuntu bash

Install Python

apt-get update
apt-get install -y python

install Python pip

# ubuntu
apt-get install -y python-pip
# centos
yum install -y python2-pi

Install Flask

# ubuntu
pip install flask
# centos
pip2 install flas

Create the Python Flask file

/opt/app.py Code:

import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
   return "Welcome"
@app.route('/how are you')
def hello():
   return 'I am good. How about you?'

if __name__ == '__main__':
   app.run()

Run the script

FLASK_APP=/opt/app.py flask run --host=0.0.0.0

Access the website

Get the container IP

# Open a new shell
# get the container's id
docker ps
# Use inspect to get the IP
docker inspect <CONTAINER-ID>
#Open a browser on the docker host
http://IP.ADD.RE.SS:5000

Build the Image

This is done from the host machine.  You can view each step by running ‘history’

Create your source folder

mkdir -p ~/docker/src/my-simple-flaskapp
cd ~/docker/src/my-simple-flaskapp

Create the application ‘app.py’ file (^C to write/quit)

cat > app.py
import os from flask import Flask app = Flask(__name__) @app.route("/") def main(): return "Welcome" @app.route('/how are you') def hello(): return 'I am good. How about you?' if __name__ == '__main__': app.run()

Create the Docker build ‘Dockerfile’ file

Ubuntu

cat > Dockerfile
FROM ubuntu
RUN apt-get -y update
RUN apt-get install -y python python-pip
RUN pip install flask
COPY app.py /opt/app.py
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0

CentOS

cat > Dockerfile
FROM centos
RUN yum -y update
RUN yum install -y python2
RUN yum install -y python2-pip
RUN pip2 install flask
COPY app.py /opt/app.py
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.

Build the Image

  • -t = tag (aka image name)
  • Note it starts with your docker account

Ubuntu

# If you are only running from your local repository
docker build . -t easyflask-u
# Add your account id to push it to Docker Hub
docker build . -t account_id/easyflask-u

CentOs

# No Account ID required for local use only
docker build . -t easyflask-c
# Add your account id to push it to Docker Hub
docker build . -t account_id/easyflask-c

Push the image to docker

Log into your docker account

docker login

Push the image to your account

docker push accountid/easyflask-x

21 Online Lab

https://www.udemy.com/course/learn-docker/learn/lecture/15828598#content

22: Environmental Variables

https://www.udemy.com/learn-docker/learn/lecture/12240112#overview

Looks like these need to be set in Python code

Define a bash-style variable in the Dockerfile:

username = 'default_name'
if os.environ.get('USERNAME'):
   username = os.environ.get('USERNAME'

Run the image with the enviromnental variable

docker run -e USERNAME=Thomas sayhello
  • You can use the docker inspect command to see any environmental variables that had been set at run time.

23: Env Lab

 

24: CMD vs ENTRYPOINT

https://www.udemy.com/learn-docker/learn/lecture/12485580#overview

  • Using CMD, any commands following the `docker run IMAGE` will over write the specified CMD.
  • Using ENTRYPOINT, and command following `docker run IMAGE` will be appended to the entrypoint command.

CMD

  • New containers over-ride their Image’s CMD command by following it with the desired command
    • docker run ubuntu sleep 5
  • You can create new Images based on the ‘root’ image and set the desired command
    • Dockerfile:
      • If you want this: CMD sleep 5
      • Use this: CMD [‘sleep’,’5′]
        • When using JSON format, you must separate the command ‘sleep’ from the variable ‘5’
        • DO NOT use: CMD [‘sleep 5’]
    • docker run ubuntu-sleeper sleep 5

ENTRYPOINT

  • Using the ‘ENTRYPOINT’ command, when you suffix the image name with a variable, that variable is appended to the ENTRYPOINT command
    • Dockerfile: ENTRYPOINT [‘sleep’]
    • docker run ubuntu-sleeper 5
  • The issue with this format, is if you do not append the value for the sleep command, you’ll see an error.
    • docker run ubuntu-sleeper
      • Error!
  • To prevent this, use both the ENTRYPOINT CMD commands together
    • Dockerfile:
      • ENTRYPOINT [‘sleep’]
      • CMD [‘5’]
        • Both must be entered in JSON format!!
  • With this format, the default value will be ‘5’, but will be over-ridden if you include a value at the end of the run command
    • docker run ubuntu-sleeper
      • runs for 5 seconds
    • docker run ubuntu-sleeper 10
      • runs for 10 seconds

–entrypoint

  • To completely over ride the image’s ENTRYPOINT, you can use the –entrypoint run option.
    • docker run –entrypoint runthisinstead ubuntu-sleeper 10

 

 

LEAVE A COMMENT