To build a docker image you create a text file named Dockerfile. Example
FROM debian:jessie
RUN apt-get update && apt-get install -y git
Now lets build a docker image, with a tag (-t) mydebian and set version to 1.0.
$ docker build -t mydebian:1.0 .
Sending build context to Docker daemon 49.14MB
Step 1/2 : FROM debian:jessie
jessie: Pulling from library/debian
e7a7e6031030: Pull complete
Digest: sha256:a8ae3c5129fb2e10a62b5c059a24308831508c44018c24ccda2e4fc6fd7cdda7
Status: Downloaded newer image for debian:jessie
---> 652b7a59e393
Step 2/2 : RUN apt-get update && apt-get install -y git
---> Running in 407556d4679a
Get:1 http://security.debian.org jessie/updates InRelease [44.9 kB]
Ign http://deb.debian.org jessie InRelease
...
Running hooks in /etc/ca-certificates/update.d....done.
Removing intermediate container 407556d4679a
---> 74f740ed80d4
Successfully built 74f740ed80d4
Successfully tagged mydebian:1.0
Verify the new local docker image exists.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mydebian 1.0 74f740ed80d4 About a minute ago 224MB
Lets test it and verify that git is installed.
$ docker run -it --rm --name mydebian mydebian:1.0
root@4ebe6d58927a:/# git --help
usage: git [--version] [--help] [-C ] [-c name=value]
Now lets look at a more complex Dockerfile. We are going to docker containerized a simple python web - https://pythonspot.com/flask-web-app-with-python/.
# vi app/app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
We copied the getting started example, but modified app.run so it binds to all network interfaces - default is only 127.0.0.1. Now lets create our dockerfile.
$ vi Dockerfile
FROM python:3.5
RUN pip install Flask
RUN useradd --create-home --shell /bin/bash appuser
USER appuser
WORKDIR /app
COPY app /app
CMD ["python", "app.py"]
FROM, from which base image RUN, run a bash command USER, swich container user WORKDIR, switch default working directory for container user COPY, copy a file into container image CMD, start the container with following command
Now lets build our docker image, named myflask with version 1.0.
$ docker build -t myflask:1.0 .
Sending build context to Docker daemon 3.584kB
Step 1/7 : FROM python:3.5
3.5: Pulling from library/python
5ae19949497e: Pull complete
ed3d96a2798e: Pull complete
f12136850781: Pull complete
1a9ad5d5550b: Pull complete
6f18049a0455: Pull complete
8daf83b35d32: Pull complete
9e89e2e794a3: Pull complete
da6fafdc8b3e: Pull complete
a6fb723ae44c: Pull complete
Digest: sha256:4598d4365bb7a8628ba840f87406323e699c4da01ae6f926ff33787c63230779
Status: Downloaded newer image for python:3.5
---> 4ae385ba9dd2
Step 2/7 : RUN pip install Flask
---> Running in f8e6adfe71ac
Collecting Flask
Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting Jinja2>=2.10.1 (from Flask)
Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting Werkzeug>=0.15 (from Flask)
Downloading https://files.pythonhosted.org/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
Collecting itsdangerous>=0.24 (from Flask)
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from Flask)
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->Flask)
Downloading https://files.pythonhosted.org/packages/6e/57/d40124076756c19ff2269678de7ae25a14ebbb3f6314eb5ce9477f191350/MarkupSafe-1.1.1-cp35-cp35m-manylinux1_x86_64.whl
Installing collected packages: MarkupSafe, Jinja2, Werkzeug, itsdangerous, click, Flask
Successfully installed Flask-1.1.1 Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 itsdangerous-1.1.0
WARNING: You are using pip version 19.2.1, however version 19.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container f8e6adfe71ac
---> a44415ad05ad
Step 3/7 : RUN useradd --create-home --shell /bin/bash appuser
---> Running in 3cf59ce0bbfc
Removing intermediate container 3cf59ce0bbfc
---> d7751c403945
Step 4/7 : USER appuser
---> Running in 5cd1ce07c45b
Removing intermediate container 5cd1ce07c45b
---> 97d574a5c01e
Step 5/7 : WORKDIR /app
---> Running in 268051f212e0
Removing intermediate container 268051f212e0
---> 0289a837efa2
Step 6/7 : COPY app /app
---> 0d17013d0e9d
Step 7/7 : CMD ["python", "app.py"]
---> Running in 5d3194da4321
Removing intermediate container 5d3194da4321
---> e7cbc78bff46
Successfully built e7cbc78bff46
Successfully tagged myflask:1.0
Lets test it
$ docker run -d -p 5000:5000 --name myflask --rm myflask:1.0
$ curl 127.0.0.1:5000
Hello World!
No comments:
Post a Comment