Docker practical notes

It all comes from the content of other people's articles, but his blog is too stuck, and the whole computer is not good, so the content is copied out and the file header is explained.
Reference address:

Docker practical notes

One, Docker introduction

1. Download the environment that Dcoker relies on

To install Docker, you need to download all dependent environments first, just like Maven depends on JDK

yum -y install yum-utils device-mapper-persistent-data lvm2

2. Specify the Docker image source

The default download of Docker will go to a foreign server to download, the speed is slower, it can be set as the Aliyun mirror source, the speed is faster

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3. Install Docker

yum makecache fast
yum -y install docker-ce

4. Start Docker and test

After the installation is successful, you need to manually start it, set it to boot up, and test Docker

#启动docker服务
systemctl start docker
#设置开机自动启动
systemctl enable docker
#测试
docker run hello-world

Two, Docker's central warehouse

1. Docker official central warehouse: This warehouse has the most complete mirroring, but the download speed is slower.

https://hub.docker.com/

2. Domestic mirroring websites: NetEase Hive, daoCloud, etc., the download speed is fast, but the mirroring is relatively incomplete.

https://c.163yun.com/hub#/home 
http://hub.daocloud.io/ (推荐使用)

3. Inside the company, the mirror will be pulled by private server (add configuration)

#需要创建 /etc/docker/daemon.json,并添加如下内容
{
    
    
	"registry-mirrors":["https://registry.docker-cn.com"],
	"insecure-registries":["ip:port"]
}
#重启两个服务
systemctl daemon-reload
systemctl restart docker

Third, the operation of mirroring

1. Pull the mirror

Pull the image from the central warehouse to the local

docker pull 镜像名称[:tag]
#举个栗子:docker pull daocloud.io/library/tomcat:8.5.15-jre8

2. View all local mirrors

View the image information that has been installed locally, including the logo, name, version, update time, and size

docker images

3. Delete the local mirror

The image will take up disk space and can be directly deleted manually. The logo can be obtained by viewing

docker rmi 镜像的标识

4. Mirror import and export

If the image can be transferred through the hard disk due to network reasons, although it is not standardized, it is effective, but the name and version of the image exported in this way are both null and need to be modified manually

#将本地的镜像导出
docker save -o 导出的路径 镜像id
#加载本地的镜像文件
docker load -i 镜像文件
#修改镜像文件
docker tag 镜像id 新镜像名称:版本

Fourth, the operation of the container

1. Run the container

The specific image needs to be customized to run the container. If the image does not exist, it will be downloaded directly

#简单操作
docker run 镜像的标识|镜像的名称[:tag]
#常用的参数
docker run -d -p 宿主机端口:容器端口 --name 容器名称 镜像的标识|镜像名称[:tag]
#-d:代表后台运行容器
#-p 宿主机端口:容器端口:为了映射当前Linux的端口和容器的端口
#--name 容器名称:指定容器的名称

2. View the running container

View all running container information

docker ps [-qa]
#-a 查看全部的容器,包括没有运行
#-q 只查看容器的标识

3. View the container log

View the container log to view information about the operation of the container

docker logs -f 容器id
#-f:可以滚动查看日志的最后几行

4. Enter the inside of the container

Can enter the inside of the container for operation

docker exec -it 容器id bash

5. Copy the content to the container

Copy the files of the host machine to the specified directory inside the container, of course, it can also be copied in the reverse direction

#将宿主机的文件复制到容器内部的指定目录
docker cp 文件名称 容器id:容器内部路径
#将容器内的文件复制到外部位置
docker cp 容器id:容器内部路径 文件名称

6. Restart & start & stop & delete the container

Start, stop, delete and other operations of the container, which will be frequently used in the follow-up

#重新启动容器
docker restart 容器id
#启动停止运行的容器
docker start 容器id
 
#停止指定的容器(删除容器前,需要先停止容器)
docker stop 容器id
#停止全部容器
docker stop $(docker ps -qa)
#删除指定容器
docker rm 容器id
#删除全部容器
docker rm $(docker ps -qa)

Five, Docker application

1.Docker install tomcat

Run the Tomcat container to prepare for the deployment of the ssm project

docker run -d -p 8080:8080 --name tomcat  daocloud.io/library/tomcat:8.5.15-jre8
#或者已经下载了tomcat镜像
docker run -d -p 8080:8080 --name tomcat 镜像的标识

2. Run the MySQL container

docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root daocloud.io/library/mysql:5.7.4

3. Deploy the ssm project

Modify the SSM project environment and set it to the information of the Docker container in Linux.
Re-type the war package through the Maven
package. Copy the war package under Windows to Linux.
Use the docker command to copy the host’s war package to the inside of the container.

docker cp 文件名称 容器id:容器内部路径

Test access to SSM project

Six, data volume

In order to deploy the SSM project, you need to use the cp command to copy the ssm.war file in the host to the container.
Data volume: Map a directory of the host machine to a directory of the container.

You can manipulate the contents of the directory on the host, and the files mapped inside the container will also change along with it.

1. Create a data volume

#After creating the data volume, it will be stored in a directory by default /var/lib/docker/volumes/data volume name/_data

docker volume create 数据卷名称

2. View all data volumes

#查看全部数据卷信息
docker volume ls

3. View data volume details

#查看数据卷的详细信息,可以查询到存放的路径,创建时间等等
docker volume inspect 数据卷名称

4. Delete the data volume

#删除指定的数据卷
docker volume rm 数据卷名称

5. Container mapping data volume

#通过数据卷名称映射,如果数据卷不存在。Docker会帮你自动创建,会将容器内部自带的文件,存储在默认的存放路径中。
docker run -d -p 8080:8080 --name tomcat -v 数据卷名称:容器内部的路径 镜像id
 
#通过路径映射数据卷,直接指定一个路径作为数据卷的存放位置。但是这个路径下是空的。
docker run -d -p 8080:8080 --name tomcat -v 路径(/root/自己创建的文件夹):容器内部的路径 镜像id

Seven, Dockerfile custom image

1.Dockerfile

To create a custom image, you need to create a Dockerfiler, the following is the language of the Dockerfile

from: Specify the environment that the current custom mirror depends on
copy: copy the content under the relative path to the custom mirror
workdir: declare the default working directory of the mirror
run: execute commands, you can write multiple
cmd: commands that need to be executed (in Execute under workdir, cmd can write multiple, only the last one shall prevail)

#示例:
from daocloud.io/library/tomcat:8.5.15-jre8
copy ssm.war /usr/local/tomcat/webapps

2. Make image through Dockerfile

#编写完Dockerfile后需要通过命令将其制作为镜像,并且要在Dockerfile的当前目录下,之后即可在镜像中查看到指定的镜像信息,注意最后的 .
docker build -t 镜像名称[:tag] ./

八、Docker-Compose

1. Download and install Docker-Compose

1.1 Download Docker-Compose

#去github官网搜索docker-compose,下载1.24.1版本的Docker-Compose
下载路径:https://github.com/docker/compose/releases/download/1.24.1/docker-compose-Linux-x86_64

1.2 Set permissions

#需要将DockerCompose文件的名称修改一下,给予DockerCompose文件一个可执行的权限
mv docker-compose-Linux-x86_64 docker-compose
chmod 777 docker-compose

1.3 Configure environment variables

#方便后期操作,配置一个环境变量
#将docker-compose文件移动到了/usr/local/bin,修改了/etc/profile文件,给/usr/local/bin配置到了PATH中
 
mv docker-compose /usr/local/bin
vi /etc/profile
#添加内容:export PATH=$JAVA_HOME:/usr/local/bin:$PATH
source /etc/profile

1.4 test

Enter in any directory

docker-compose

2.Docker-Compose manages MySQL and Tomcat containers

The yml file uses key:value to specify configuration information.
Multiple configuration information is distinguished by line break + indentation
. Do not use tabs in the docker-compose.yml file.

version: '3.1'
services:
  mysql:           # 服务的名称
    restart: always   # 代表只要docker启动,那么这个容器就跟着一起启动
    image: daocloud.io/library/mysql:5.7.4  # 指定镜像路径
    container_name: mysql  # 指定容器名称
    ports:
      - 3306:3306   #  指定端口号的映射
    environment:
      MYSQL_ROOT_PASSWORD: root   # 指定MySQL的ROOT用户登录密码
      TZ: Asia/Shanghai        # 指定时区
    volumes:
     - /opt/docker_mysql_tomcat/mysql_data:/var/lib/mysql   # 映射数据卷
  tomcat:
    restart: always
    image: daocloud.io/library/tomcat:8.5.15-jre8
    container_name: tomcat
    ports:
      - 8080:8080
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /opt/docker_mysql_tomcat/tomcat_webapps:/usr/local/tomcat/webapps
      - /opt/docker_mysql_tomcat/tomcat_logs:/usr/local/tomcat/logs

3. Use the docker-compose command to manage the container

When using the docker-compose command, the docker-compose.yml file will be found in the current directory by default

#1.基于docker-compose.yml启动管理的容器
docker-compose up -d
 
#2.关闭并删除容器
docker-compose down
 
#3.开启|关闭|重启已经存在的由docker-compose维护的容器
docker-compose start|stop|restart
 
#4.查看由docker-compose管理的容器
docker-compose ps
 
#5.查看日志
docker-compose logs -f

4. docker-compose used with Dockerfile

Use the docker-compose.yml file and the Dockerfile file to start the current image while generating a custom image, and docker-compose to manage the container

4.1docker-compose file

Write docker-compose file

# yml文件
version: '3.1'
services:
  ssm:
    restart: always
    build:            # 构建自定义镜像
      context: ../      # 指定dockerfile文件的所在路径
      dockerfile: Dockerfile   # 指定Dockerfile文件名称
    image: ssm:1.0.1
    container_name: ssm
    ports:
      - 8081:8080
    environment:
      TZ: Asia/Shanghai

4.2 Dockerfile

Write Dockerfile

from daocloud.io/library/tomcat:8.5.15-jre8
copy ssm.war /usr/local/tomcat/webapps

4.3 Operation

#可以直接基于docker-compose.yml以及Dockerfile文件构建的自定义镜像
docker-compose up -d
# 如果自定义镜像不存在,会帮助我们构建出自定义镜像,如果自定义镜像已经存在,会直接运行这个自定义镜像
#重新构建自定义镜像
docker-compose build
#运行当前内容,并重新构建
docker-compose up -d --build

9. Introduction and preparation of CI and CD

1. Introduction to CI and CD

Project deployment
1. Compile and package the project through maven
2. Upload the file to the specified server
3. Put the war package in the tomcat directory
4. Use the Dockerfile to convert the Tomcat and war package into a mirror image, which will be run by DockerCompose After the container project is updated, the above process needs to be executed again from beginning to end. If the above operations are executed once for each update, it is time-consuming and laborious. We can help us achieve continuous integration, continuous delivery and deployment through CI and CD

2.CI introduction

CI (continuous intergration) Continuous Integration
: When writing code, after completing a function, immediately submit the code to the Git repository, and rebuild and test the project.
1. Find errors quickly.
2. Prevent the code from deviating from the main branch.

3. Build Gitlab server

3.1. Prepare the environment

To implement CI, you need to use the Gitlab remote warehouse. First, build a Gitlab through Docker to
create a brand new virtual machine, and specify at least 4G of running memory, which is the recommended memory size for Gitlab.
And install Docker and Docker-Compose

3.2 Modify port 22 of ssh

#将ssh的默认22端口,修改为60022端口,因为Gitlab需要占用22端口
 
vi /etc/ssh/sshd_config
  PORT 22 -> 60022
systemctl restart sshd

3.3 编写docker-compose.yml

docker-compose.yml文件去安装gitlab(下载和运行的时间比较长的)
 
version: '3.1'
services:
 gitlab:
  image: 'twang2218/gitlab-ce-zh:11.1.4'
  container_name: "gitlab"
  restart: always
  privileged: true
  hostname: 'gitlab'
  environment:
   TZ: 'Asia/Shanghai'
   GITLAB_OMNIBUS_CONFIG: |
    external_url 'http://192.168.199.110'
    gitlab_rails['time_zone'] = 'Asia/Shanghai'
    gitlab_rails['smtp_enable'] = true
    gitlab_rails['gitlab_shell_ssh_port'] = 22
  ports:
   - '80:80'
   - '443:443'
   - '22:22'
  volumes:
   - /opt/docker_gitlab/config:/etc/gitlab
   - /opt/docker_gitlab/data:/var/opt/gitlab
   - /opt/docker_gitlab/logs:/var/log/gitlab

Ten, build GitlabRunner

1. Prepare documents

Prepare the maven installation package, jdk1.8 installation package, Dockerfile, daemon.json and docker-compose in the folder environment

daemon.json
{
    
    
“registry-mirrors”: [“https://registry.docker-cn.com”],
“insecure-registries”: [ip:ports]
}

2. Start building

Create the working directory /usr/local/docker_gitlab-runner
and copy the docker-compose.yml file and environment directory to the above directory. After the host starts the docker program, execute sudo
chown root: root /var/run/docker.sock ( If you have restarted docker, execute again)
Execute docker-compose up -d --build in the /usr/local/docker_gitlab-runner directory to start the container and
add container permissions to ensure that the container can use the host's docker exec -it gitlab-runner usermod- aG
root gitlab-Runner Runner registration information to gitlab

3. Go to the next steps

docker exec -it gitlab-runner gitlab-runner register


 
# 输入 GitLab 地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.199.109/
 
# 输入 GitLab Token
Please enter the gitlab-ci token for this runner:
1Lxq_f1NRfCfeNbE5WRh
 
# 输入 Runner 的说明
Please enter the gitlab-ci description for this runner:
可以为空
 
# 设置 Tag,可以用于指定在构建规定的 tag 时触发 ci
Please enter the gitlab-ci tags for this runner (comma separated):
deploy
 
# 这里选择 true ,可以用于代码上传后直接执行(根据版本,也会没有次选项)
Whether to run untagged builds [true/false]:
true
 
# 这里选择 false,可以直接回车,默认为 false(根据版本,也会没有次选项)
Whether to lock Runner to current project [true/false]:
false
 
# 选择 runner 执行器,这里我们选择的是 shell
Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh:
shell

Eleven, integration project introductory test

1. Create a project

Create maven project, add web.xml file, write HTML page

2. Write the .gitlab-ci.yml file

stages:
  - test
 
test:
  stage: test
  script:
    - echo first test ci   # 输入的命令

3. Push the maven project to gitlab

Execute git command to push to Gitlab

git push origin master

4. View the effect

You can view the content written by gitlab-ci.yml in gitlab

12. Improve project configuration

Add Dockerfile and docker-compose.yml, and modify the .gitlab-ci.yml file

1. Create a Dockerfile

# Dockerfile
FROM daocloud.io/library/tomcat:8.5.15-jre8
COPY testci.war /usr/local/tomcat/webapps

2. Create docker-compose.yml

# docker-compose.yml
version: "3.1"
services:
  testci:
    build: docker
    restart: always
    container_name: testci
    ports:
      - 8080:8080

3. 修改 .gitlab-ci.yml

# ci.yml
stages:
  - test
 
test:
  stage: test
  script:
    - echo first test ci
    - /usr/local/maven/apache-maven-3.6.3/bin/mvn package
    - cp target/testci-1.0-SNAPSHOT.war docker/testci.war
    - docker-compose down
    - docker-compose up -d --build
    - docker rmi $(docker images -qf dangling=true)

Guess you like

Origin blog.csdn.net/qq_15915293/article/details/108099451