AlexZ33 / lessions

自己练习的各种demo和课程
12 stars 2 forks source link

About docker install #2

Open AlexZ33 opened 5 years ago

AlexZ33 commented 5 years ago

关于docker基础知识:

这就催生出了Docker。Docker使用了相同的Linux底层功能(namespaces 和 cgroups )来创建轻量级容器。

Docker只正式支持64位系统,对于遗留系统来说,lxc是一种更好的选择。

Docker容器和lxc容器的主要区别在于:

Docker的设计哲学使得我们很容易从小的构建块(building block)入手来构造系统,但也增加了开发构建块的难度,因为在完整的Linux系统中(包括 crontab 表项),需要运行大量的工具来执行清理、日志回卷等操作。

创建好Docker容器之后,其行为在其他Docker服务器上也不会出现变化。这使得在云端或远程站点上部署Docker容器变得非常简单。

here is the sh script for install docker

reference:基于 Docker 打造前端持续集成开发环境 install-docker.sh Install docker CE on Linux Mint 18.3 or ubuntu

#!/usr/bin/env bash

# https://docs.docker.com/install/linux/docker-ce/ubuntu/
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
sudo apt-get update
sudo apt-get install docker-ce

# https://docs.docker.com/compose/install/
sudo curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# https://docs.docker.com/install/linux/linux-postinstall/
sudo groupadd docker
sudo usermod -aG docker $USER

image

AlexZ33 commented 5 years ago
sh install-docker.sh
AlexZ33 commented 5 years ago

docker -v

AlexZ33 commented 5 years ago

Dockerize Vue Js app or any other Node app

cd myapp/ code .

AlexZ33 commented 5 years ago

Create Dockerfile and fill with below to build image node - Docker Hub nginx - Docker Hub

# build stage
FROM node:10.15.3-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# production stage
FROM nginx:1.16.0-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
AlexZ33 commented 5 years ago

Run docker build -t d3-vue:v1.0 . in myapp terminal

AlexZ33 commented 5 years ago

Dockerfile

Dockerfile是用来描述文件的构成的文本文档,其中包含了用户可以在使用组合Image的所有命令,用户还可以使用Docker build实现连续执行多个命令指今行的自动构建。

通过编写Dockerfile生磁镜像,可以为开发、测试团队提供基本一致的环境,从而提升开发、测试团队的效率,不用再为环境不统一而发愁,同时运维也能更加方便地管理我们的镜像。

Dockerfile的语法非常简单,常用的只有11个:

命令 说明
FROM 基于哪个镜像来实现
MAINTAINER 镜像的创建者
ENV 声明环境变量
RUN 执行的命令
ADD 添加宿主机文件到容器里,有需要解压的文件会自动解压
COPY 添加宿主机文件到容器里
WORKDIR 工作目录
EXPOSE 容器内应用可使用的端口
CMD 容器启动后所执行的程序,如果执行docker run 后面跟启动命令会被覆盖掉
ENTRYPOINT 与CMD功能相同,但需docker run 不会覆盖, 如果需要覆盖可增加参数 -entrypoint 来覆盖
VOLUMNE 将宿主机的目录挂载到容器里

创建镜像

commit命令创建镜像

$ docker commit containerId/Name respositoryPath

Dockerfile创建镜像

$ mkdir buildEnv
$ cd buildEnv
$ touch Dockerfile
$ vim Dockerfile
# Dockerfile
# 指定基础镜像(base image)
FROM ubuntu:latest
# 申明作者以及邮箱
MAINTAINER Name "Email"
# 执行RUN指令
RUN apt-get update && apt-get install -y nginx 
RUN echo 'hello world' >/usr/share/nginx/html/index.html
# 暴露端口
EXPOSE 80

$ docker build -t="kzq/nginx" .

启动镜像
$ docker run -d -p 80 "kzq/nginx" nginx -g "daemon off;"
// -d以分离(detached)的方式在后台运行 -p公开端口给宿主机
AlexZ33 commented 5 years ago

对比下面的两种写法:

FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install -y apt-utils libjpeg-dev \     
python-pip
RUN pip install --upgrade pip
RUN easy_install -U setuptools
RUN apt-get clean
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y apt-utils \
  libjpeg-dev python-pip \
           && pip install --upgrade pip \
      && easy_install -U setuptools \
    && apt-get clean

1、 第一个Dockerfile的好处是:当正在执行的过程某一层出错,对其进行修正后再次Build,前面已经执行完成的层不会再次执行。这样能大大减少下次Build的时间,而它的问题就是会因层级变多了而使镜像占用的空间也变大。 2、 第二个Dockerfile把所有的组件全部在一层解决,这样做能一定程度上减少镜像的占用空间,但在制作基础镜像的时候若其中某个组编译出错,修正后再次Build就相当于重头再来了,前面编译好的组件在一个层里,得全部都重新编译一遍,比较消耗时间。

看两个Dockerfile所编译出来的镜像大小:

$ docker images | grep ubuntu      
REPOSITORY      TAG     IMAGE ID    CREATED     SIZE                                                                                                                                   
ubuntu                   16.04       9361ce633ff1  1 days ago 422MB
ubuntu                   16.04-1   3f5b979df1a9  1 days ago  412MB

若Dockerfile非常长的话可以考虑减少层次,因为Dockerfile最高只能有127层。

AlexZ33 commented 5 years ago

多阶构建

如何编写优雅的Dockerfile

AlexZ33 commented 5 years ago

Docker Compose With Node & MongoDB

项目示例:https://github.com/AlexZ33/todo_app 教程:https://www.youtube.com/watch?v=hP77Rua1E0c reference: https://github.com/cnodejs/egg-cnode/blob/master/tutorials/Docker.md

AlexZ33 commented 4 years ago

聊聊docker容器安装nodejs环境遇到的坑

https://blog.csdn.net/weixin_45123659/article/details/102819484

AlexZ33 commented 4 years ago

实际案例


FROM centos
CMD /bin/init.py
ADD id_rsa known_hosts /root/.ssh/
ENV LD_LIBRARY_PATH='/opt/soft/thrift/lib:/usr/lib:/usr/local/lib:/home/work/php/lib/:' PATH='/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/work/opshell/:/home/work/bin:/opt/soft/maven/bin:/opt/soft/jdk/bin:/home/xbox/aesir/frigga/bin:/home/xbox/aesir/ruby/bin' IS_OCEAN_CONTAINER=1 LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LANGUAGE=en_US.UTF-8

RUN mkdir -p /tmp/nodejs && \
        echo version 1 && \
        wget /require_packages/nodejs/node-v10.13.0-linux-x64.tar.gz -O /tmp/nodejs/node-v10.13.0-linux-x64.tar.gz && \
        tar -xf /tmp/nodejs/node-v10.13.0-linux-x64.tar.gz -C /usr/local/ && \
        rm -rf /tmp/nodejs
ENV PATH=/usr/local/node-v10.13.0-linux-x64/bin:$PATH
ENV DOCKER_USER=root DOCKER_MYSQL_ON=No DOCKER_INSTANCE=7001:::  NODE_ENV="staging" EGG_SERVER_ENV="staging"

ADD / /home/work/webapps/website
RUN . /etc/profile && cd /usr/local/ && bash -x /usr/local/seal/build-release.sh -u git@v9.git.n.xiaomi.com:HY-FE/migc-new-website.git -j job.website-nodejs_service.website-nodejs_cluster.preview-c3_pdl.gameunion_owt.game_cop.xiaomi -b staging -v 97d465e5-webyml20191112184711 -s docker -n zhaokang1 -a No -w https://ocean.pt.xiaomi.com/api/deploy/v1/jobs/job.website-nodejs_service.website-nodejs_cluster.preview-c3_pdl.gameunion_owt.game_cop.xiaomi/jobWebYml?token=due1z63P6X -d 
AlexZ33 commented 3 years ago

centos 安装 docker & docker-compose

https://www.cnblogs.com/ruanqin/p/11082506.html

image

AlexZ33 commented 1 year ago

docker-compose

Compose is a tool for defining and running multi-container Docker applications Compose是一个用于定义和运行多容器Docker应用程序的工具

安装 话不多说,官网文档大法好 https://docs.docker.com/compose/install/

三步走 (1)创建Dockerfile搭建环境 (2)创建docker-compose.yml配置服务 (3)运行docker-compose up

docker-compose.yml格式(注意空格)

version: '3'
  services:
      web:
          ports:
          - "5000:5000"
          volumes:
          - .:/code
          - logvolume01:/var/log
          links:
          - redis
      redis:
          image: redis
AlexZ33 commented 1 year ago

将镜像推送到Docker Hub

image

AlexZ33 commented 1 year ago

镜像

image

AlexZ33 commented 1 year ago

删除容器

image