Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker问题集 #31

Open
lqshow opened this issue May 4, 2018 · 0 comments
Open

docker问题集 #31

lqshow opened this issue May 4, 2018 · 0 comments

Comments

@lqshow
Copy link
Owner

lqshow commented May 4, 2018

如何在docker run中运行多条指令

docker run -d  \
 --restart always \
 -p 13200:13200 \
 --volume /root/.m2:/root/.m2  \
 --env NODE_ENV=test  \
 enigma:0.0.1 \
 /bin/sh -c 'cd /data/project/service; npm run build-xfs; ./bin/start.sh'

Docker环境变量如何设置

1. 通过Dockerfile设置

FROM debian:wheezy
ENV NODE_ENV test

查看容器中的环境变量

➜  /Users/linqiong/workspace/docker/example docker run -it --rm example env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=45804cf45f6f
TERM=xterm
NODE_ENV=test
HOME=/root

2. 通过docker run命令中的-e或--env值来设置环境变量

这里传递的环境变量值会覆盖掉Dockerfile中设置的环境变量值

docker run \
	-it \
	--rm \
	-e NODE_ENV=bb \
	-e HADOOP_HOME=/opt/hadoop/current \
	example env

查看执行结果

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2f2a77b3dfea
TERM=xterm
NODE_ENV=bb
HADOOP_HOME=/opt/hadoop/current
HOME=/root

3.配置环境变量文件, 再通过docker run命令中的--env-file参数传入

该方法的好处是可以不将环境变量的值暴露出去

  1. 创建.env配置文件
echo "NODE_ENV=production\nKAFKA_HOME=/opt/kafka/current" > .env
  1. 执行docker run命令
docker run -it --rm --env-file ./.env example env
  1. 查看执行结果
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=17ec8d22fad8
TERM=xterm
NODE_ENV=production
KAFKA_HOME=/opt/kafka/current
HOME=/root

同主机下容器间通信

默认情况下主机和容器是可以相互通信的
docker提供了--link 容器名:别名,运行容器启动的时候通过--link来指定要和哪个容器通信。

容器 IP
workspace 172.17.0.8
dashboard-app-runner 172.17.0.15

如果xdp-workspace-api容器需要访问dashboard-app-runner,创建workspace的容器方式如下

docker run -d \
 --name workspace \
 --link dashboard-app-runner:dashboard \
 --restart always \
 -p 3201:3201  enigma:0.0.1

workspcae hosts如下
image
workspace访问dashboard调用方式:http://dashboard:14200/api/v2

容器内运行spark应用

以下需要指定配置

Property Name desc
spark.driver.bindAddress 容器host名称或者ip地址
spark.driver.host 主机名或者IP地址
spark.driver. port 驱动器监听端口号
spark.ui.port 应用程序dashboard的端口
spark.blockManager.port 块管理器监听的端口

References

如何设置容器默认时区

在Dockerfile中配置

CentOS

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Ubuntu

RUN echo "Asia/Shanghai" > /etc/timezone && \
    dpkg-reconfigure -f noninteractive tzdata

如何在构建镜像时使用SSH

相关问题:GitLab CI/CD中使用SSH私钥

resource与Dockerfile为同级目录,id_rsa为本机的私钥

CentOs

# Add credentials on build
RUN mkdir -p /root/.ssh
Add resource/id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa && \
      echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config


# Remove SSH keys
RUN rm -rf /root/.ssh/

Ubuntu

# Add credentials on build
RUN mkdir -p /root/.ssh
Add resource/id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa && \
      echo "StrictHostKeyChecking no\nUserKnownHostsFile /dev/null" >> /root/.ssh/config


# Remove SSH keys
RUN rm -rf /root/.ssh/

Reference

在docker容器中使用SSH密钥

docker-compose使用过程中的网络通信问题

如果docker-compose.yml没有配置networks,在运行docker-compose up时它会默认创建一个default的network,这里需要注意的是,如果默认创建的network的subnet正好是项目中用到的subnet,这里就会有问题。比如说项目中连的redis为172.18.0.105:6379,而default network如下所示

➜  /Users/linqiong/workspace/basebit/gitlab/dockerfile/centos_enigma git:(master) ✗ docker network inspect enigma_backend
[
    {
        "Name": "enigma_backend",
        "Id": "1a10dd8b06a6e639e1ba3dc1ddfba899b3868b226f840765382f4e86a7db7192",
        "Created": "2018-05-06T14:12:11.576372418Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                   "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

解决方案:可以手动设置一个network

docker network create --subnet=172.19.0.0/16 enigma_backend

验证码乱码

容器环境缺少字体引起

RUN yum install -y curl cabextract xorg-x11-font-utils fontconfig
RUN mkdir -p /root/fonts
COPY resource/msttcore-fonts-installer-2.6-1.noarch.rpm /root/fonts/msttcore-fonts-installer-2.6-1.noarch.rpm
RUN rpm -i /root/fonts/msttcore-fonts-installer-2.6-1.noarch.rpm

Reference

Get Microsoft's Core Fonts for the Web and Cleartype Fonts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant