Docker code release

Introduction to Docker:

  1. Compared with virtual machines, Docker has a small space occupation, a fast startup speed (in seconds), and a high degree of integration , which can effectively avoid version incompatibility issues.

  2. What is mirroring : Mirroring is the unification of a bunch of read-only layers. Except that the bottom layer does not point, each layer points to its parent layer. The unified file system (Union? File System) technology can integrate different layers into a file system, providing a unified perspective for these layers, thus hiding the existence of multiple layers. From the user's perspective, there is only one file system. Each layer of the mirror is not writable, and it is a read-only layer.
    What is a container : The definition of a container is almost the same as an image, and it is also a unified view of a stack of layers. The only difference is that the top layer of the container is readable and writable. Key points: container = mirror + read-write layer, and the definition of the container does not mention whether to run the container.
    It is recommended to come back after practice to understand the difference between docker containers and images

  3. Common operations
    docker images: view all images
    docker rmi image-id: delete images
    docker ps -a: view all containers
    docker rm container-id: delete containers
    docker attach container-id: enter the container (now basically use exec, exit without closing the container )
    Docker logs container-id View the internal output of the container
    docker tag imageid: tag name: tag Modify the image name
    docker commit: Convert the read-write layer of the container to a read-only layer, so that a container is converted into an immutable image .

  4. alpine

Text: Django project release

1. Create a file tree

compose
——downline (offline)
—Dockerfile (build image file)
—pip.conf (accelerated pip download speed)
—AIRS (project file)
—requirements.txt (installation directory)
—start.sh (startup file)
——upline (Online)
2. .git clone *** Store the project file under AIRS
3. .cp ./AIRS/requireme ./requirements Save the requirements file to be installed to the same directory as the Dockerfile
4. Write pip.conf file vim pip .conf


[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
~                                                        

5. Write start.sh vim start.sh

python manage.py runserver 0.0.0.0:8000

6. Write Dockerfile

#基础镜像 docker pull python:3.6
FROM python:3.6		
#定义环境变量
ENV PYTHONUNBUFFERED 1
#将pip.con转移到新镜像的 /root/.pip/pip.conf
COPY pip.conf /root/.pip/pip.conf
#将项目存放到/app文件夹下
COPY AIRS/ /app
#将需要下载的requirements.txt放入工作目录/app下
COPY requirements.txt /app/requirements.txt
#开始下载依赖包
RUN pip install -r /app/requirements.txt
#确定工作目录
WORKDIR /app

#将启动文件放入镜像
copy ./start.sh /app/start.sh
RUN chmod +x /app/start.sh
CMD ["sh", "start.sh"]

7. Create an image

#docker build -t="用户名/镜像名称:版本(tag) "   .(当前目录)
docker build -t="horn/airs:v1.0" .

Insert picture description here

8. Start the container

#-d 后台运行
#-i 打开STDIN,用于控制台交互
#-t 分配tty设备,该可以支持终端登录,默认为false
#-p  指定容器暴露的端口
#-u 指定容器的用户
#--name 给容器起别名
[root@horn downline]# docker run -d -p 1935:8000  horn/airs:v1.0  python manage.py runserver 0.0.0.0:8000
#显示所有容器
docker ps -a 

Insert picture description here

9. Successful startup, but externally inaccessible
Method 1: Modify files in the container by copying, disadvantages (trouble)

#将当前目录settings.py 复制到 containerid:/app/推荐系统/settings.py
docker cp ./settings.py 7ed:/app/推荐系统/settings.py

Method two: directly enter the container to modify the configuration file, the disadvantage is that vim needs to be installed, increasing the container footprint

docker run -i –t centos /bin/bash
or
docker exec  -it container-id /bin/bash  
#获取docker镜像root权限
sudo docker exec -ti -u root container-id  /bin/bash 

#退出后重启容器: docker restart container

10. Docker warehouse
(1) docker login
Insert picture description here

(2) Modify the image name according to the docker account-> username / image name

docker tag imageid:tag name:tag

Insert picture description here
(3) Store the image in the docker warehouse

docker push 用户名/镜像名

Insert picture description here

11. Modify the image

sudo docker commit -m 'modify settings.py' -a "docker horn1998" 9c54 horn1998/airs:v2.0
Among them, -m to specify the description information submitted, the same as the version control tool we use; -a can specify the updated user information; followed by the ID 9c54 of the container used to create the mirror ; finally specify the target mirror warehouse name and tag information . After successful creation, the ID information of this image will be returned . Insert picture description hereInsert picture description here
12. docker-compose
1. Quick installation

curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

2. Variable interpretation

  • image: Specify the image name or ID. If the image does not exist locally, Compose will try to pull the image
image: python:3.6-alpine
  • build: construct an image based on Dockerfile
    • args: Define the environment variables needed in the sh file during the build process, but will be canceled after the build is successful
e1:build: .	#当前目录下寻找Dockerfile,如果同时指定了build, image,则Compose会构建镜像并且把镜像命名为image对应的名字
e2:build:
		context:.
		args:
			build:1	
			password: secret		
  • command: can override the default command executed after the container is started
command: python manage.py runserver 0.0.0.0:8000
  • depends_on: The container startup sequence is required, control the container startup sequence to prevent startup failure due to container dependency issues
depends_on:
	-db #其它服务名称
  • CMD: After starting the container, provide the default commands and parameters (not necessarily will be executed, but the default) will be docker run behind the parameters to replace
    CMD to provide some commands and parameters when the container is running, the usage is as follows: The
    first usage: run an executable File and provide parameters.
    CMD ["sh", "start.sh"]
    The second usage: specify parameters for ENTRYPOINT.
    CMD ["param1", "param2"] The
    third usage (shell form): the command executed by the method of "/ bin / sh -c".
    CMD ["/ bin / echo", "this is a echo test"]
    A dockerfile can only have at most one cmd, if there are multiple, only the last one will take effect.

  • ENTRYPOINT: executed when the container is started (it will be executed)

  • The difference between entrypoint and cmd is
    generally the entry bracket in the form of square brackets as the default execution command after the docker container is started, which contains the unchanged part, the variable part such as the command parameter can use the form of cmd to provide the default version The default parameter used when there is no parameter in run. If we want to use the default parameters, we run directly, otherwise we want to use other parameters, add parameters to run.
    Original link: https://blog.csdn.net/u010900754/article/detaills/78526443
  • external_links: connect to containers no longer defined in docker-compose.yml
  • links: solve the connection problem between containers
  • ports: the label of the mapped port
ports:
		- "8000:8000"
  • The
    volume volume guarantees the persistence of Docker data, even if the data does not end with the end of the container, the data exists on the host machine-either exists in a specified directory of the host, or uses the volume managed by Docker itself.
volumes:	
		#此时docker将自动创建一个匿名的volume,并将其挂载到container中的/mysql目录。
		- /var/lib/mysql
		#如果文件不存在,则docker会自动创建
		- /opt/data:/var/lib/mysql

3. Sample code

version: '2'
services:
                ssm:
                        build: .
                        ports:
                                - "1935:8000"
                        image: horn1998/ssm:v2.0
                        command: sh start.sh
                        depends_on:
                               - mysql
                        volumes:
                               - /opt/data:/app/data
                        links:
                               - mysql
                mysql:
                        image: mysql:latest
                        ports:
                                - "3306:3306"
                        command: service mysql start

4. Start compose:

docker-compose up -d
Published 16 original articles · Like1 · Visits 383

Guess you like

Origin blog.csdn.net/qq_41174940/article/details/86107999