docker笔记 · AoiNeko

你。 提交于 2019-12-01 16:51:27

docker简介

镜像(image):一个轻量级,独立的可执行的软件包。其中包含了运行一个软件的所有,软件代码,运行软件的环境运行时(runtime),软件运行依赖的代码库,环境变量,以及配置文件。
容器(container):容器即镜像运行的一个实例。镜像实际执行时以容器的形式存在内存中,默认情况下容器完全独立于host机的环境,仅在配置后可以读写host机的文件和端口。

与虚拟机的区别

虚拟机

安装虚拟机需要安装虚拟机监视器 hypervisor ,给每台虚拟机分配计算机资源内存 cpu 网络与磁盘,而且每个OS层里面都有各自的系统依赖安装,系统安全补丁等容易丢失而不易复制的资源

docker

docker容器之间共同使用host机的内核,已进程的形式运行在host机上。因为镜像包含了所有的依赖,不需要安装在host机器上。一个与host机器没有任何配置纠葛的‘容器化’app就诞生了

如何创建镜像并运行

  1. 编写dockerfile
    将编译成app.jar的spring-boot项目放在项目目录下。并编写启动脚本

    FROM java
    ENV JAVA_HOME /usr
    COPY app.jar /usr/local/app/
    COPY start.sh /usr/local/app/
    WORKDIR /usr/local/app
    EXPOSE 8111
    RUN echo $JAVA_HOME
    ENTRYPOINT ["sh", "start.sh"]
    

    dockerfile命令文档: https://docs.docker.com/engine/reference/builder/

  2. 使用doc 大专栏  docker笔记 · AoiNekoker build命令创建镜像 docker build -t myapp:1.0.0 .

  3. 执行镜像,生成容器。docker run -d -p 8111:8111 -v /data:/data -v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime -v /home/apms/docker/timezone:/etc/timezone --name myapp myapp:1.0.0

docker CLI文档 https://docs.docker.com/engine/reference/run/

迁离docker镜像容器安装目录

You can change Docker's storage base directory (where container and images go) using the -g option when starting the Docker daemon.

Ubuntu/Debian: edit your /etc/default/docker file with the -g option: DOCKER_OPTS="-dns 8.8.8.8 -dns 8.8.4.4 -g /mnt"

Fedora/Centos: edit /etc/sysconfig/docker, and add the -g option in the other_args variable: ex. other_args="-g /var/lib/testdir". If there's more than one option, make sure you enclose them in " ". After a restart, (service docker restart) Docker should use the new directory.

Using a symlink is another method to change image storage.

Caution - These steps depend on your current /var/lib/docker being an actual directory (not a symlink to another location).

1) Stop docker: service docker stop. Verify no docker process is running ps faux
2) Double check docker really isn't running. Take a look at the current docker directory: ls /var/lib/docker/
2b) Make a backup - tar -zcC /var/lib docker > /mnt/pd0/var_lib_docker-backup-$(date +%s).tar.gz
3) Move the /var/lib/docker directory to your new partition: mv /var/lib/docker /mnt/pd0/docker
4) Make a symlink: ln -s /mnt/pd0/docker /var/lib/docker
5) Take a peek at the directory structure to make sure it looks like it did before the mv: ls /var/lib/docker/ (note the trailing slash to resolve the symlink)
6) Start docker back up service docker start
7) restart your containers

一.启动docker服务的时候添加-g option
二.停止docker服务 将源目录复制到大空间的硬盘挂载目录后,建立软连接。重启服务。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!