Docker use record (III)

Download image

Through the most simple image file "hello world", feel Docker.
It should be noted that the official repository of domestic Docker connection is very slow, but also broken, you need to change the default warehouse domestic mirror sites, the specific modification method in the first quarter. A friend in need, you can look at, you have configured remember to restart it docker.
First, run the following command to grab the image file from the warehouse to the local
docker image pull library/hello-world

  • Explanation:
    • docker image pullCommand files crawl image
    • library/hello-worldIs the location of the image file in the repository, which is a set of library files are located, hello-world image is the name of the file
      because the image file Docker official offer, are placed inside the library group, so it is the default group, can be omitted, as follows:
      docker image pull hello-world

After the crawl is successful, you can see the image files in the machine
docker image ls
running the image file
docker container run hello-world

  • Explanation:
    • docker container runCommand from the image file to generate a container instance running
      docker container runcommand has automatically grab image file functions, not specified if the local image file found, it will automatically grab from the warehouse,
      therefore, in front of the docker run pullcommand is not a necessary step

Run successfully, the following message appears

$ docker container run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

After the above information containers will stop running

Some containers are not automatically terminated because the service is provided. For example, running the Ubuntu installation image, you can experience the Ubuntu system on the command line
docker container run -it ubuntu bash

For the container does not automatically terminated, you must use the docker container killcommand to manually terminate
docker container kill [containID]

Container file

Examples of the image file generating container, is also a document, called the container file. In other words, once the container is generated, it will exist two files image files and container file
and close the container and container files are not deleted, but it stops running container

# 列出本机正在运行的容器
docker container ls

# 列出本机所有容器,包括终止运行的容器
docker container ls --all

Create a mirror

  • 1. Create a directory and a file Dockerfile
    I was in /homethe new
mkdir dockerProjects
cd dockerProjects
touch Dockerfile
  • 2.Dockerfile in each of these instructions to create a mirror layer, example:
# 该image文件继承官方的 python image,冒号表示标签,这里标签是3.7,即3.7版本的python
FROM python:3.7-slim
# 指定接下来的工作路径为/app
WORKDIR /app
# 将当前目录内容复制到/app的容器中
ADD . /app
# 在/app目录下,运行pip install命令安装requirements.txt中环境包。注意,安装后所有的环境包,都将打包进入image文件
RUN pip install -i https://pypi.douban.com/simple -r requirements.txt
# 将容器 80 端口暴露出来,允许外部连接这个端口
EXPOSE 80
# 定义环境变量
ENV NAME world
# 容器启动时运行app.py
CMD ["python","app.py"] 

Note that the above configuration file, do not use single quotes

  • 3. Create app.py
    vim app.py
    edit
# 导入flask
from flask import Flask

# 获取方式对象,(就随意)命名为app
app = Flask(__name__)
# 使用app绑定路由
@app.route('/')
def hello():
    # 页面内容
    return 'hello docker'

# 启动
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=80)
  • 4. Create a requirements.txtfile and add the required environment package (an example is: Flask)
  • The image generated image
    docker image build -t hello:v-1.0 .or a docker build -t hello:v-1.0
    -tparameter to specify the name of the image file, followed by a colon may also specify a label, if not specified, the default label is latest. The final point that represents the path Dockerfile file is located, the example is the current path, it is a point.

After a successful operation to generate the image file of the image
docker imagesto view

  • 6. The generating vessel
    docker container runcommand from the image file generating vessel
docker run -p 8100:80 hello:v-1.0
# 或
docker container run -p 8100:80 -it hello /bin/bash  # 用于没有设置tag时
# 或者
docker container run -p 8100:80 -it hello:v-1.0 /bin/bash
# 也可以这样
docker container run -d -p 8100:80 --rm --name hello

-pParameters: 80 container port mapping to port 8100 of the machine, the outside world by 8100 port access
-itparameters: Shell vessel mapped to the current Shell, then you are in the command window, enter the machine, it will pass the container
hello:v-1.0: the name of the image file (If you have labels also need to provide the label, the default is the latest label)
/bin/bash: after the container is started, the first internal command execution. Here is the start Bash, to ensure that users can use the Shell

-d: Represents a container running in the background (no output to the Shell) and displays the ID of the container
--rm: that is automatically deleted container after container stop
to see the running of container
docker ps

  • Access (via external ip: 8100 visit the above flask procedure)
14827704-44e6b9bfe2fe7aba.png
docker2.png

Reproduced in: https: //www.jianshu.com/p/83f6d5eb24af

Guess you like

Origin blog.csdn.net/weixin_34123613/article/details/91182304