rainit2006 / My_AWS-Cloud

0 stars 0 forks source link

Docker #2

Open rainit2006 opened 7 years ago

rainit2006 commented 7 years ago

Docker VS VirtualMachine https://www.youtube.com/watch?v=2xSwDPBtt3E

rainit2006 commented 7 years ago

■提问: 请问我输入的命令“sudo docker run -it -d --name opengrok -p 8080 -v /usr/lib/jvm -v /home/hemouse/opengrok krazakee/opengrok:1.1rc21”是不是有什么问题?好像在chrome中访问"http://localhost:8080"没反应

■回答: -p和-v是用来映射本机端口和本机储存路径的,也就是说,你要提供本地的参数。

比如说,你希望通过9091端口访问,你的linux jdk放在~/docker/jdk/jdk8目录下,你的源代码放在~/opengrok/javacode/src目录下。

那么命令就是

docker run -itd -name opengrok -p 9091:8080 -v ~/docker/jdk/jdk8:/usr/lib/jvm -v ~/opengrok/javacode:/var/opengrok krazakee/opengrok

然后你就可以通过localhost:9091来访问了。不过第一次访问是不能搜索的,需要先用文章中的命令建立索引。以后代码更新了也需要手动重建一下索引。出于性能考虑我没有配置自动重建。

另外,因为你已经试图run过容器,再用同样名称去run可能会冲突。可以先跑下面两个命令清理一下再run

docker stop opengrok docker rm opengrok

rainit2006 commented 7 years ago

Container和Image 根据Image生成Container, 搭配好Container后生成新的Image。

下载一个Image (一个ubuntu的 image) docker pull ubuntu

运行一个container

docker run -itd ubuntu /bin/bash # 后台执行 container
docker ps # 找到后台执行的 container id 或昵称
docker attach <container id> # 重新 attach 这个 container
rainit2006 commented 7 years ago

在测试电脑上(Windows8.1 不存在Hyper机能)安装dockToolbox后,需要最先点击Docker Quickstart Terminal图标让其创始化创建dock和相关资源。否则执行"docker run hello-world"时会抱错。

在Proxy环境的PC里,环境变量“system variables”里创建变量"HTTP_PROXY"其值等于“http://xxxx: 10080” 注意: 没有"http://"的话动作会失败。而且设置完环境变量后要reboot电脑

执行docker run -it ubuntu bash进入ubuntu bash后,发现里面连vim都没有。 As in the comments, there's no default editor set - strange - $EDITOR env variable is empty. You can login into container with: docker exec -it <container> bash and run:

apt-get update
apt-get install vim
rainit2006 commented 7 years ago

服务器搭建时用到的命令


基础命令: docker-machine ls docker ps docker stop "container ID"


http://www.runoob.com/docker/docker-container-usage.html Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。 输出Hello world docker run ubuntu:15.10 /bin/echo "Hello world" 各个参数解析: docker: Docker 的二进制执行文件。 run:与前面的 docker 组合来运行一个容器。 ubuntu:15.10指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。 /bin/echo "Hello world": 在启动的容器里执行的命令 以上命令完整的意思可以解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。

我们通过docker的两个参数 -i -t,让docker运行的容器实现"对话"的能力 runoob@runoob:~$ docker run -i -t ubuntu:15.10 /bin/bash 各个参数解析: -t:在新容器内指定一个伪终端或终端。 -i:允许你对容器内的标准输入 (STDIN) 进行交互。 此时我们已进入一个 ubuntu15.10系统的容器 我们尝试在容器中运行命令 cat /proc/version和ls分别查看当前系统的版本信息和当前目录下的文件列表. 我们可以通过运行exit命令或者使用CTRL+D来退出容器。

启动容器(后台模式) runoob@runoob:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done" (run -d 的意思: 等同 --detach。 Run container in background and print container ID) 在输出中,我们没有看到期望的"hello world",而是一串长字符: 2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63 这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。 在容器内使用docker logs命令,查看容器内的标准输出 runoob@runoob:~$ docker logs 2b1b7a428627

获取一个新的镜像: docker pull xxxxx 查找镜像: docker search xxxxx。 找到后使用命令 docker pull 来下载镜像。

rainit2006 commented 7 years ago

遭遇问题: ubuntu镜像里没有vim, sudo等许多常用package。 回答: 这个好像是式样。

执行“apt-get update”命令升级也失败。 解决: apt-get update升级失败还是因为proxy环境的问题。解决办法:执行下面命令 export http_proxy="http://xxxxxx:10080"

问题: 执行who命令没有反应

rainit2006 commented 7 years ago

执行: root# docker run -t -i ubuntu:14.04 /bin/bash 后, Docker 中整个容器是一个 Linux 环境,ubuntu 镜像的默认用户为 root,默认工作目录为根目录。

root@33d90ffaf1ac:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18172  3104 ?        Ss   08:50   0:00 /bin/bash
root        16  0.0  0.0  15572  2200 ?        R+   09:04   0:00 ps aux

容器中的进程相当简洁,只有正在运行的两个程序没有其他任何进程。

rainit2006 commented 7 years ago

生成Dockerfile https://www.howtoforge.com/tutorial/how-to-create-docker-images-with-dockerfile/

Dockerfile的命令 http://aspec7.hateblo.jp/entry/2015/06/27/184819

FROM: コンテナのベースイメージを指定します。

MAINTAINER コンテナの管理者を明示するためのものです。 主に担当者の名前やメールアドレスを書きます。

RUN コンテナを作成する際にコマンドを実行し、コンテナイメージにコミットします。

CMD コンテナ起動時にデフォルトで実行するコマンドを指定します。

ENTRYPOINT docker run実行時、オプションを指定しなくても自動的に実行するコマンドを指定します。

EXPOSE コンテナで解放するポート番号を指定します。

ENV 環境変数を指定します。

VOLUME コンテナ内のホストや他のコンテナからのマウントポイントを明示します。

USER RUN,CMD,ENTRYPOINTを実行するユーザを指定します。

rainit2006 commented 7 years ago

docker exec Run a command in a running container. docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

execはdocker run と似てるけど、こちらは起動中のコンテナでコマンドを実施できる。デバックに使える。

Docker run Run a command in a new container

run命令的常用参数: -p : Publish a container’s port(s) to the host -v : Bind mount a volume -d: コンテナをバックグラウンドで動かす --rm=true コンテナをexitした時に自動的にコンテナを削除 -m メモリ制限値を指定する(割り当てではない)

-network参数      -network=bridge  默认选项,表示连接到默认的网桥。     -network=host    容器使用宿主机的网络     -network=container :NAME_or_ID :告诉Docker让新建的容器使用已有容器的网络配置    -network  = none  不配置该容器的网络,用户可自定义网络配置

rainit2006 commented 7 years ago

docker容器默认的空间是10G,如果想指定默认容器的大小(在启动容器的时候指定),可以在docker配置文件里通过dm.basesize参数指定,比如 docker -d --storage-opt dm.basesize=20G

ホスト側のマウント先を指定する場合 docker run -v [ホストディレクトリの絶対パス]:[コンテナの絶対パス] [イメージ名] [コマンド] docker run -v /lib/modules:/lib/modules dummy-img /bin/bash

これでホスト側のディレクトリがコンテナにマウントされる。コンテナ上で作ったファイルがホストの方に残る。

rainit2006 commented 7 years ago

在某个Linux OS上跑不同的Linux OS image,这些OS有什么区别吗? The crux of the matter is that if the Host OS is RedHat then it is the RedHat kernel which will be used by whatever build of Linux you run in your Docker container ie. Ubuntu in your example.

This comes down to understanding what the difference is between a Linux OS and a Linux Image. You will not be running a full Ubuntu OS inside the Docker Container but an image of Ubuntu.

For the purpose of your question think:-

OS = kernel + filesystem/libraries Image = filesystem/libraries

The Ubuntu image running inside your Docker container is just the Ubuntu filesystem/libraries (not the kernel shipped with the Ubuntu OS). The Ubuntu image running inside the container will be using the Redhat kernel. This partly explains the efficiencies you get from a Docker container which is leveraging the Kernel (among other things) of the underlying Host .

很重要的一点: Docker uses host OS kernel, there is no custom or additional kernel inside container. All containers which run on a machine are sharing this "host" kernel.

rainit2006 commented 7 years ago

WindowsでDockerが使える? http://qiita.com/NewGyu/items/d9e6bddb85e2bf9afd15

rainit2006 commented 6 years ago

Dockfile的实例 注意: dockerfile的脚本是为了创建同样的docker环境,而不会去执行里面的脚本。 所以会看到很多 “ADD 脚本文件”和“chmod +x 脚本文件”的命令,但是不会去执行该脚本文件。

baseimage-docker では /etc/service/<サービス名> に run という名前をつけたシェルスクリプトをおくだけで、デーモンとして起動してくれるようになる。

起動時にスクリプトを実行する ba­seim­age-docker では /etc/my_init.d/ にシェルスクリプトをおくだけで、起動時に実行してくれる。

ENV HOME /root

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

#RUN apt-get install -y wget

# install aws
RUN wget "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -O "awscli-bundle.zip"
RUN unzip awscli-bundle.zip
RUN ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

# install boto for hive_daily_files script
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python get-pip.py
RUN pip install boto

#Add activemq.xml for activemq
ADD activemq.xml /var/lib/activemq/conf/
#RUN ln -sf /etc/activemq/instances-available/main/ /etc/activemq/instances-enabled/main

创建docker file: Step 1 : Create a file named Dockerfile touch Dockerfile Step 2 : Add instructions in Dockerfile Step 3 : Build dockerfile to create image docker build . #如果dockerfile在同目录下docker build -t <ImageName>:<TagName> . #如果dockerfile在同目录下

Step 4 : Run image to create container

rainit2006 commented 5 years ago

安装Docker-ce 在ubuntu 参考官网:https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce-1

依次执行命令:

$ sudo apt-get update
$ 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 -

确认生成了fingerprint “9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88” $ sudo apt-key fingerprint 0EBFCD88

执行lsb_release -cs 确认当前ubunt的版本号:比如xenial。把xenial带入下面的命令里 $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xennial stable" 然后执行 $ sudo apt-get update 注意: 此时如果报错 E: Malformed entry 54 in list file /etc/apt/sources.list (url parse) E: The list of sources could not be read. 则执行sudo nano /etc/apt/sources.list 打开文本内容,并修改第54行的文字格式(此行的url格式肯定存在问题,比如少空格或多了其他符号),然后Ctrl+x退出,选择y保存修改内容。既可以解决。 接着执行

$ sudo apt-get install docker-ce

$ apt-cache madison docker-ce 取得版本一览

$ sudo apt-get install docker-ce=<VERSION>    比如:docker-ce=18.03.0~ce-0~ubuntu
rainit2006 commented 5 years ago

■保存对容器的修改:docker commit Docker Commitする場合は docker commit <コンテナ名/ID> <イメージ名>:<タグ名> で行う。 http://www.docker.org.cn/book/docker/docer-save-changes-10.html <步骤:run docker image, 安装软件或修改什么, 然后执行exit退出,执行“ps -a”确认docker image已经不再运行了,然后在“docker images”命令里可以看到image列表。然后执行commit命令>

  1. 运行docker commit,可以查看该命令的参数列表。
  2. 你需要指定要提交保存容器的ID。(译者按:通过docker ps -l 命令获得)
  3. 无需拷贝完整的id,通常来讲最开始的三至四个字母即可区分。(译者按:非常类似git里面的版本号) $ docker commit 698(id的头三位数字) learn_ping(新名称) 4, docker images查看有没有保存成功

参考视频: https://www.youtube.com/watch?v=svs-_9SAYz0

■docker rmiコマンドでイメージを削除する

rainit2006 commented 5 years ago

Docker ROS構成 https://qiita.com/Leonardo-mbc/items/cfd38a4fae8667593cf1

涉及到下面用法 Docker Compose Docker network

rainit2006 commented 5 years ago

Docker pull下来的镜像文件、创建出来的容器文件都存在本地的什么地方了? 参考: http://dockone.io/question/70

rainit2006 commented 5 years ago

docker-compose : 複数コンテナの管理が便利に https://qiita.com/y_hokkey/items/d51e69c6ff4015e85fce

docker-compose.ymlと同じ階層に移動してdocker-compose up -dをすると、ビルド・プルを自動で行ってから、linksの依存関係に沿った順番でコンテナを起動してくれる。

docker-compose.yml 例子

# mysql
mysql:
  image: mysql
  volumes_from:
    - data-mysql
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: password
# redmine
redmine:
  image: sameersbn/redmine:2.6.3
  volumes_from:
    - data-gitbucket
    - data-redmine
  links:
    - mysql:mysql
  environment:
    REDMINE_RELATIVE_URL_ROOT: /redmine
    DB_USER: redmine
    DB_PASS: password
    SMTP_USER: address@hoge.com
    SMTP_PASS: password
# gitbucket
gitbucket:
  image: f99aq8ove/gitbucket
  volumes_from:
    - data-gitbucket
  ports:
    - "29418:29418"
  command: java -jar /opt/gitbucket.war --prefix=/gitbucket
# reverse-proxy
proxy:
  build: myproxy
  links:
    - gitbucket:gitbucket
    - redmine:redmine
  ports:
    - "80:80"

常用命令:

YAMLに「build:」があれば、そのイメージをまとめてビルド

docker-compose build

YAMLに「image:」があれば、そのイメージをまとめてプル

docker-compose pull

docker-compose build, docker-compose pullをした後にdocker run

docker-compose up -d

個別のサービスを指定することもできる。依存関係がある場合は関係するコンテナすべてが起動するので、この場合は redmine と mysql が両方起動する

docker-compose up -d redmine

関係するコンテナすべての出力を表示

docker-compose logs

関係するコンテナをまとめて終了

docker-compose stop

関係するコンテナをまとめて削除

docker-compose rm

rainit2006 commented 5 years ago

Docker http://paiza.hatenablog.com/entry/docker_intro コンテナと仮想マシンの違い: コンテナはOSレベルの仮想化を行いその上でプロセスを動かしますが、仮想マシンはマシンレベルの仮想化を行いその上でゲストOSを動かします。コンテナ(Docker)と仮想マシンを比較すると、コンテナ(Docker)は、早い、リソース消費が少ない、OSはLinuxのみ、という特徴があります。

Dockerの構成(5つの要素) コンテナ: Dockerイメージから作られ、実行される仮想環境です。 Dockerイメージ: コンテナのファイルシステム、設定をひとまとめに保存しています。 Dockerサーバ: Docker本体ともいえる、コンテナ・イメージの管理を行うサービスです。 Dockerクライアント: ユーザが実際にDockerを操作すル時に使うコマンド、GUIツールです。Dockerを利用する周辺ツールも含まれます。 Docker Hub(レジストリ): Dockerイメージを集めたサイトです。OS、アプリケーションのイメージが多く公開されており、誰でも自由に利用できます。

http://bg.biedalian.com/2014/11/20/docker-start.html 什么是 docker? 为了更好理解, 你可以直接和别人说它是虚拟机, 实际上 docker 并不是虚拟机, 它做的是 linux 的隔离, 但它的隔离做的如此之真实以至于让人觉得自己拥有可以一台完整的 linux 系统.

那 docker 和 虚拟机具体是什么区别呢, 虚拟机在底层模拟出各种硬件, cpu, 硬盘之类的, 而 docker 是在软件层面给资源分组, docker 性能无限接近原生, 因为 docker 用的就是系统自己的进程, 而虚拟机做的再好, 也做不出原生的感觉.

docker 的隔离技术源自于 Linux 容器 LXC(linux container), 听起名字就知道, 这和沙箱应该差不多, 可以把东西分开放, 也就是隔离的意思, 甚至可以在某些仓库中看到 docker 的名字叫 lxc-docker. LXC 又是基于 cgroup 的 namespace, chroot 等. cgroup 对于 docker 是至关重要的, 了解它才会觉得 docker 不神秘, cgroup 全称为 control group, 是 linux 内核提供的功能, 简单的说, 它的作用就是把系统运行的进程按用户自定义的群组区分, 也就是说 一个 docker, 一个 group. cgroup 有限制使用资源的能力: •blkio -- 这个子系统为块设备设定输入/输出限制,比如物理设备(磁盘,固态硬盘,USB 等等) •cpu -- 这个子系统使用调度程序提供对 CPU 的 cgroup 任务访问 •cpuacct -- 这个子系统自动生成 cgroup 中任务所使用的 CPU 报告 •cpuset -- 这个子系统为 cgroup 中的任务分配独立 CPU(在多核系统)和内存节点 •devices -- 这个子系统可允许或者拒绝 cgroup 中的任务访问设备 •freezer -- 这个子系统挂起或者恢复 cgroup 中的任务 •memory -- 这个子系统设定 cgroup 中任务使用的内存限制,并自动生成由那些任务使用的内存资源报告 •net_cls -- 这个子系统使用等级识别符(classid)标记网络数据包,可允许 Linux 流量控制程序(tc)识别从具体 cgroup 中生成的数据包 •ns -- 名称空间子系统

rainit2006 commented 4 years ago

- Docker cp <Host的文件>  <ContainerID:/ファイルパス>

コンテナ内にコピーします。
docker cp ./sample.html aabade1747be:/usr/local/apache2/htdocs/
コンテナからホストへ
docker cp aabade1747be:/usr/local/apache2/conf/httpd.conf ./