一,安裝docker
1,服務器安裝 docker
yum install docker
直接yum安裝版本太低
2,卸載:老版本的Docker在yum中名稱為docker或docker-engine,通過下面命令卸載docker和相關依賴
yum remove docker \
docker-common \ docker-selinux \ docker-engine
3,新的Docker CE在yum中被稱為docker-ce。(因為分化為docker-ce和docker-ee,后者為企業版)設置yum docker repository 指定版本安裝
(1),搭建Docker CE的REPOSITORY
# Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data and lvm2 are required by the devicemapper storage driver. yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
# Use the following command to set up the stable repository. You always need the stablerepository, even if you want to install builds from the edge or test repositories as well. yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
(2),下面這個例子是使用sort -r 命令來排序docker-ce的版本,從高到低。
yum list docker-ce --showduplicates | sort -r
(3) 安裝命令
# Install the latest version of Docker CE, or go to the next step to install a specific version. yum install docker-ce
或
yum install docker-ce-18.06.1.ce
3,使用Docker 中國加速器
vim /etc/docker/daemon.json
添加下面代碼
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"live-restore": true
}
(這個文件 初始狀態是空的 只有“{}”)
啟動服務
systemctl start docker systemctl restart docker systemctl stop docker
查看版本
docker version
查看狀態
service docker status
二,開啟docker遠程訪問
vim /etc/sysconfig/docker
在下面代碼中添加紅色部分
# /etc/sysconfig/docker # Modify these options if you want to change the way the docker daemon runs OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false' if [ -z "${DOCKER_CERT_PATH}" ]; then DOCKER_CERT_PATH=/etc/docker fi DOCKER_OPTS="-H unix:///var/run/docker.sock -H 0.0.0.0:2376" # Do not add registries in this file anymore. Use /etc/containers/registries.conf # instead. For more information reference the registries.conf(5) man page. # Location used for temporary files, such as those created by # docker load and build operations. Default is /var/lib/docker/tmp # Can be overriden by setting the following environment variable. # DOCKER_TMPDIR=/var/tmp # Controls the /etc/cron.daily/docker-logrotate cron job status. # To disable, uncomment the line below. # LOGROTATE=false # docker-latest daemon can be used by starting the docker-latest unitfile. # To use docker-latest client, uncomment below lines #DOCKERBINARY=/usr/bin/docker-latest #DOCKERDBINARY=/usr/bin/dockerd-latest #DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest #DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
修改服務配置:
vim /lib/systemd/system/docker.service
在下面代碼中添加紅色部分
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer
[Service]
Type=notify
NotifyAccess=main
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current \
--add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
--default-runtime=docker-runc \
--exec-opt native.cgroupdriver=systemd \
--userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
--init-path=/usr/libexec/docker/docker-init-current \
--seccomp-profile=/etc/docker/seccomp.json \
$OPTIONS \
$DOCKER_STORAGE_OPTIONS \
$DOCKER_NETWORK_OPTIONS \
$ADD_REGISTRY \
$BLOCK_REGISTRY \
$INSECURE_REGISTRY \
$REGISTRIES \
$DOCKER_OPTS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
KillMode=process
[Install]
WantedBy=multi-user.target
重啟docker網絡
systemctl daemon-reload
重啟docker服務
systemctl restart docker
在瀏覽器輸入:http://xxx.xxx.xxx:2376/info 或http://xxx.xxx.xxx:2376/version 顯示版本信息表示開啟成功
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.meylink.test</groupId> <artifactId>docker</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>docker</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <docker.image.prefix>springboot</docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- Docker maven plugin --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.0</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <baseImage>java</baseImage> <dockerHost>http://39.105.164.124:2376</dockerHost> <!--<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>--> <!-- copy the service's jar file from target into the root directory of the image --> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <!-- Docker maven plugin --> </plugins> </build> </project>
查看遠程服務器鏡像
docker images