| | |
| | | |
| | | import com.github.dockerjava.api.DockerClient; |
| | | import com.github.dockerjava.api.command.*; |
| | | import com.github.dockerjava.api.model.Container; |
| | | import com.github.dockerjava.api.model.PullResponseItem; |
| | | import com.github.dockerjava.core.DockerClientBuilder; |
| | | import com.se.common.core.domain.DockerContainer; |
| | | import com.se.docker.utils.DockerUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class SysDockerService { |
| | | //@Value("${docker.prefix}") |
| | | public String localFilePrefix; |
| | | @Value("${docker.host}") |
| | | private String host; |
| | | |
| | | @Value("${docker.apiVersion}") |
| | | private String apiVersion; |
| | | |
| | | private static DockerUtils dockerUtils; |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(SysDockerService.class); |
| | | |
| | | public DockerUtils getDockerUtils() { |
| | | if (null == dockerUtils) { |
| | | dockerUtils = new DockerUtils(host, apiVersion); |
| | | } |
| | | |
| | | return dockerUtils; |
| | | } |
| | | |
| | | public String test() throws Exception { |
| | | List<String> names = new ArrayList<>(Arrays.asList("se-redis", "se-mysql", "se-system", "se-wgcloud")); |
| | | List<DockerContainer> list = getContainers(names); |
| | | |
| | | String id = list.get(0).getId(); |
| | | start(id); |
| | | pause(id); |
| | | unpause(id); |
| | | stop(id); |
| | | restart(id); |
| | | |
| | | return "docker: " + System.currentTimeMillis(); |
| | | } |
| | | |
| | | public List<DockerContainer> getContainers(List<String> names) { |
| | | DockerUtils util = getDockerUtils(); |
| | | List<Container> containers = util.listContainers(); |
| | | |
| | | List<DockerContainer> list = new ArrayList<>(); |
| | | for (Container container : containers) { |
| | | if (null == container.getNames() || 0 == container.getNames().length) continue; |
| | | |
| | | String containerName = container.getNames()[0]; |
| | | for (String name : names) { |
| | | if (containerName.contains(name)) { |
| | | list.add(new DockerContainer(container.getId(), containerName, container.getState(), container.getImage())); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | public void start(String id) { |
| | | getDockerUtils().start(id); |
| | | } |
| | | |
| | | public void restart(String id) { |
| | | getDockerUtils().restart(id); |
| | | } |
| | | |
| | | public void stop(String id) { |
| | | getDockerUtils().stop(id); |
| | | } |
| | | |
| | | public void pause(String id) { |
| | | getDockerUtils().pause(id); |
| | | } |
| | | |
| | | public void unpause(String id) { |
| | | getDockerUtils().unpause(id); |
| | | } |
| | | |
| | | /** |
| | | * java DockerClient操作 |
| | | * https://blog.csdn.net/weixin_45198228/article/details/130060333 |