系列文章目录
docker 门外初体验–docker container (二)
前言
本文主要讲述docker 镜像的编译,运行,及镜像推送,镜像拉取的过程,及谈一谈本人对docker container的理解. 重新查看docker官网的get-start主页,已经和之前内容有所不同.
一、docker container是什么?
程序的运行态是进程,docker image(镜像)的运行态是docker container (容器),程序是经过编译链接生成而成的,docker镜像也是需要build这样的一个过程。这是我对docker container的理解
二、编译
参考链接:https://docs.docker.com/get-started/#start-the-tutorial
docker 镜像的编译是通过Dockerfile编译的, 类如Makefile之于gcc. 从根本上来说,Dockerfile是包含一系统命令的文本,这些文本在创建docker镜像的过程中被docker客户端所调用.
# Use an official Python runtime as a parent image
FROM python:3
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install Flask Redis
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
使用如下命令编译docker 镜像
docker build -t friendlyhello .
root@iZuf6anc2b2vgfvms9d7elZ:~/docker/container# docker build -t friendlyhello .
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM python:3
---> e32be9a6f71f
Step 2/7 : WORKDIR /app
---> Using cache
---> 521688d41df3
Step 3/7 : ADD . /app
---> da5db64cb03b
Step 4/7 : RUN pip install Flash Redis
---> Running in 08740493a384
Collecting Flash
Downloading flash-1.0.3-py3-none-any.whl (2.7 kB)
Collecting Redis
Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
Installing collected packages: Redis, Flash
Successfully installed Flash-1.0.3 Redis-3.5.3
Removing intermediate container 08740493a384
---> 4abd2aab8498
Step 5/7 : EXPOSE 80
---> Running in f07f412154d7
Removing intermediate container f07f412154d7
---> 0e4d50b73a8d
Step 6/7 : ENV NAME World
---> Running in 4931d9708f25
Removing intermediate container 4931d9708f25
---> 775a3d58cc84
Step 7/7 : CMD ["python", "app.py"]
---> Running in 9e39e9d20d6d
Removing intermediate container 9e39e9d20d6d
---> 81112d93634f
Successfully built 81112d93634f
Successfully tagged friendlyhello:latest
使用docker image ls 可以查看前面生成的docker 镜像
三、推送
代码可以提交到git/svn仓库当中,docker image也是可以推送到docker 镜像仓库当中的,我们可以将编译生成docker image推送至docker hub上,以后需要使用时直拉位取下来。首先需要注册docker hub帐号, https://hub.docker.com/ ,其次使用docker login 登入 docker hub. 最后可以把docker image推送至docker hub上,犹如我们将代码提交至github帐号下的仓库一样,需要的时再拉取下来。
root@iZuf6anc2b2vgfvms9d7elZ:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: pan19881018
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
在docker hub 创建好docker image仓库,如下所示
root@iZuf6anc2b2vgfvms9d7elZ:~# docker tag friendlyhello pan19881018/friendlyhello:part2
root@iZuf6anc2b2vgfvms9d7elZ:~# docker push pan19881018/friendlyhello:part2
The push refers to repository [docker.io/pan19881018/friendlyhello]
d8499c1aa3a4: Pushed
997f79bcb7d5: Pushed
7b0f7f0f5ae2: Pushed
6145fd322249: Mounted from library/python
c5e393b8a19a: Mounted from library/python
b3f4557ae183: Mounted from library/python
9f5b4cdea532: Mounted from library/python
cd702377e4e5: Mounted from library/python
aa7af8a465c6: Mounted from library/python
ef9a7b8862f4: Mounted from library/python
a1f2f42922b1: Mounted from library/python
4762552ad7d8: Mounted from library/python
part2: digest: sha256:5f74871f505ee7970fa29a491bf8af54aedefe733438d1ee59d539e631dda95a size: 2842
docker tag friendlyhello pan19881018/friendlyhello:part2
这条指令主要是把本地的friendlyhello 与 远端的pan19881018/friendlyhello仓库绑定。
docker push pan19881018/friendlyhello:part2
这条指令则是将本地镜像真正推送至docker hub的仓库当中。
四、拉取
使用docker pull 可以把镜像拉至本地
root@iZuf6anc2b2vgfvms9d7elZ:~# docker pull pan19881018/friendlyhello:part2
part2: Pulling from pan19881018/friendlyhello
Digest: sha256:5f74871f505ee7970fa29a491bf8af54aedefe733438d1ee59d539e631dda95a
Status: Image is up to date for pan19881018/friendlyhello:part2
docker.io/pan19881018/friendlyhello:part2
使用docekr image ls 可以查看我们本地的docker镜像列表
root@iZuf6anc2b2vgfvms9d7elZ:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 81112d93634f 2 hours ago 891MB
pan19881018/friendlyhello part2 81112d93634f 2 hours ago 891MB
五、运行
使用该指令运行docker run -p 4888:80 pan19881018/friendlyhello:part2
root@iZuf6anc2b2vgfvms9d7elZ:~/docker/container# docker run -p 4888:80 pan19881018/friendlyhello:part2
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
可以通守docker container ls 查看运行的容器
root@iZuf6anc2b2vgfvms9d7elZ:~# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b3aa21798c pan19881018/friendlyhello:part2 "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:4888->80/tcp stupefied_heyrovsky
六、总结
本文主要是通过docker build/push/pull/run/container/tag/login 等基本指令演示docker 的基本操作过程,当我们把docker镜像运行起来之后,docker container (容器)就是我们系统当中的一个进程,只是容器所在的进程是具备完整的独立的namespace,如User, PID , IPC ,NET, UTS,MNT(挂载)。这些都需要底层技术的支持。