Engineering - Docker

Docker安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Download packages
$ wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
$ wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-cli-18.09.7-3.el7.x86_64.rpm
$ wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-18.09.0-3.el7.x86_64.rpm

## Install required packages
$ yum install container-selinux libseccomp
$ rpm -ivh containerd.io-1.2.6-3.3.el7.x86_64.rpm
$ rpm -ivh docker-ce-cli-18.09.7-3.el7.x86_64.rpm

## Install docker
$ rpm -ivh docker-ce-18.09.0-3.el7.x86_64.rpm

## Uninstall docker
## $ yum remove docker-ce

## Start docker
$ systemctl start docker

## Verify docker
$ docker run hello-world

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

## List images
$ docker images

Docker配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## 开启远程访问Docker

### 方式一
$ vi /usr/lib/systemd/system/docker.service

#ExecStart=/usr/bin/dockerd -H unix://
ExecStart=/usr/bin/dockerd

$ vi /etc/docker/daemon.json

{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}

### 方式二
$ vi /usr/lib/systemd/system/docker.service

#ExecStart=/usr/bin/dockerd -H unix://
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375

### 方式三
$ dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375

## 重新加载配置
$ systemctl daemon-reload
## 重启docker
$ systemctl restart docker

## 测试
$ curl 127.0.0.1:2375/info

Docker部署服务

构建Docker镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!-- pom.xml -->
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>${project.artifactId}:${project.version}</imageName>
<dockerHost>http://192.168.8.129:2375</dockerHost>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<!-- Package as an executable jar -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1
2
3
4
5
6
## src/main/docker/Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8080

启动容器实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
## 将本机7070端口映射到容器的8080端口
$ docker run -d -p 7070:8080 reliablemq

## -d|--detach: Run container in background and print container ID
79fbc1e40a2aa3e5ef16823102a41218

## 查看容器日志
$ docker logs 79fbc1e40a2aa3e5ef16823102a41218

## 列出本地容器
$ docker ps
## 列出本地镜像
$ docker images
## 关闭容器
$ docker stop facf8533780a
## 删除容器
$ docker rm facf8533780a
## 删除镜像
$ docker rmi 3d9019901c03

Docker可视化

1
2
3
4
5
6
7
8
9
10
11
## 从DockerHub查找Portainer镜像
$ docker search portainer

## 下载Portainer镜像
$ docker pull portainer/portainer

## 单机运行Portainer
$ docker run -d -p 9000:9000 portainer/portainer

## 测试
$ curl http://127.0.0.1:9000

Docker私服

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## Install Docker Compose
$ wget https://github.com/docker/compose/releases/download/1.24.1/docker-compose-Linux-x86_64
$ mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
## Test the installation
$ docker-compose --version

## Download the harbor installer
$ wget https://storage.googleapis.com/harbor-releases/release-1.8.0/harbor-offline-installer-v1.8.1.tgz
$ tar -zxvf harbor-offline-installer-v1.8.1.tgz
$ cd harbor

## Configure harbor.yml
$ vi harbor.yml

# Configuration file of Harbor

# The IP address or hostname to access admin UI and registry service.
#hostname: reg.mydomain.com
hostname: 192.168.8.129

# http related config
http:
port: 80

# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345

# Harbor DB configuration
database:
# The password for the root user of Harbor DB. Change this before any production use.
password: root123

# The default data volume
data_volume: /data

## Install and start Harbor
$ ./install.sh
## Test the installation
$ curl http://192.168.8.129

## Deploy a plain HTTP registry
$ vi /etc/docker/daemon.json

{
"insecure-registries": ["192.168.8.129"]
}

## Restart Docker for the changes to take effect
$ systemctl daemon-reload && systemctl restart docker
$ docker-compose -f /opt/harbor/docker-compose.yml up -d
$ docker info

## Login to a self-hosted registry
$ docker login -u admin -p Harbor12345 http://192.168.8.129
$ docker tag hello-world:latest 192.168.8.129/library/hello-world:latest
## To push an image to a private registry, Docker requires that the image tag being pushed is prefixed with the hostname and port of the registry.
$ docker push 192.168.8.129/library/hello-world:latest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!-- pom.xml -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<!-- The credentials for the registry is set in the settings.xml -->
<serverId>docker-hub</serverId>
<pushImage>true</pushImage>
<imageName>
192.168.8.129/library/${project.artifactId}:${project.version}
</imageName>
<!--<imageName>${project.artifactId}:${project.version}</imageName>-->
<dockerHost>http://192.168.8.129:2375</dockerHost>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

<!-- ${MAVEN_HOME}/conf/settings.xml -->
<servers>
<server>
<id>docker-hub</id>
<username>admin</username>
<password>Harbor12345</password>
<configuration>
<email>lugavin@outlook.com</email>
</configuration>
</server>
</servers>