如何在 Docker 容器中 运行 命令
How to run command inside Docker container
我是 Docker 的新手,我正在尝试了解以下设置。
我想调试我的 docker 容器以查看它在 运行 宁作为 Fargate
中的任务时是否正在接收 AWS 凭据。建议我运行命令:
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
但我不知道该怎么做。
安装程序使用 Gitlab CI 构建 push
docker 容器到 AWS ECR
。
这是docker文件:
FROM rocker/tidyverse:3.6.3
RUN apt-get update && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y liblzma-dev && \
apt-get install -y libbz2-dev && \
apt-get install -y libnetcdf-dev
COPY ./packrat/packrat.lock /home/project/packrat/
COPY initiate.R /home/project/
COPY hello.Rmd /home/project/
RUN install2.r packrat
RUN which nc-config
RUN Rscript -e 'packrat::restore(project = "/home/project/")'
RUN echo '.libPaths("/home/project/packrat/lib/x86_64-pc-linux-gnu/3.6.3")' >> /usr/local/lib/R/etc/Rprofile.site
WORKDIR /home/project/
CMD Rscript initiate.R
这是 gitlab-ci.yml
文件:
image: docker:stable
variables:
ECR_PATH: XXXXX.dkr.ecr.eu-west-2.amazonaws.com/
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
stages:
- build
- deploy
before_script:
- docker info
- apk add --no-cache curl jq py-pip
- pip install awscli
- chmod +x ./build_and_push.sh
build-rmarkdown-task:
stage: build
script:
- export REPO_NAME=edelta/rmarkdown_report
- export BUILD_DIR=rmarkdown_report
- export REPOSITORY_URL=$ECR_PATH$REPO_NAME
- ./build_and_push.sh
when: manual
这是构建和推送脚本:
#!/bin/sh
$(aws ecr get-login --no-include-email --region eu-west-2)
docker pull $REPOSITORY_URL || true
docker build --cache-from $REPOSITORY_URL -t $REPOSITORY_URL ./$BUILD_DIR/
docker push $REPOSITORY_URL
我想 运行 在我的 docker 容器上执行此命令:
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
我如何 运行 在 fargate 中启动容器时执行此命令?
对于 运行 在 docker 容器内执行命令,您需要在 docker 容器内。
第 1 步:找到您要调试的容器 ID/容器名称
docker ps
将显示容器列表,选择其中一个
步骤 2 运行 以下命令
docker exec -it <containerName/ConatinerId> bash
然后输入 wait 几秒,你就会进入 docker 容器,交互模式 Bash
有关详细信息,请阅读 https://docs.docker.com/engine/reference/commandline/exec/
简答,只需替换 CMD
CMD ["sh", "-c", " curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_UR && Rscript initiate.R"]
长答案,您需要替换 DockerFile 的 CMD,因为目前 运行仅 Rscript
。
你有两个选项添加 entrypoint
或更改 CMD
,对于 CMD
检查上面的
仅在您要调试时创建 entrypoint.sh
和 运行 运行。
#!/bin/sh
if [ "${IS_DEBUG}" == true ];then
echo "Container running in debug mode"
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
# uncomment below section if you still want to execute R script.
# exec "$@"
else
exec "$@"
fi
Dockerfile 端所需的更改
WORKDIR /home/project/
ENV IS_DEBUG=true
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
entrypoint ["/entrypoint.sh"]
CMD Rscript initiate.R
我是 Docker 的新手,我正在尝试了解以下设置。
我想调试我的 docker 容器以查看它在 运行 宁作为 Fargate
中的任务时是否正在接收 AWS 凭据。建议我运行命令:
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
但我不知道该怎么做。
安装程序使用 Gitlab CI 构建 push
docker 容器到 AWS ECR
。
这是docker文件:
FROM rocker/tidyverse:3.6.3
RUN apt-get update && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y liblzma-dev && \
apt-get install -y libbz2-dev && \
apt-get install -y libnetcdf-dev
COPY ./packrat/packrat.lock /home/project/packrat/
COPY initiate.R /home/project/
COPY hello.Rmd /home/project/
RUN install2.r packrat
RUN which nc-config
RUN Rscript -e 'packrat::restore(project = "/home/project/")'
RUN echo '.libPaths("/home/project/packrat/lib/x86_64-pc-linux-gnu/3.6.3")' >> /usr/local/lib/R/etc/Rprofile.site
WORKDIR /home/project/
CMD Rscript initiate.R
这是 gitlab-ci.yml
文件:
image: docker:stable
variables:
ECR_PATH: XXXXX.dkr.ecr.eu-west-2.amazonaws.com/
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
stages:
- build
- deploy
before_script:
- docker info
- apk add --no-cache curl jq py-pip
- pip install awscli
- chmod +x ./build_and_push.sh
build-rmarkdown-task:
stage: build
script:
- export REPO_NAME=edelta/rmarkdown_report
- export BUILD_DIR=rmarkdown_report
- export REPOSITORY_URL=$ECR_PATH$REPO_NAME
- ./build_and_push.sh
when: manual
这是构建和推送脚本:
#!/bin/sh
$(aws ecr get-login --no-include-email --region eu-west-2)
docker pull $REPOSITORY_URL || true
docker build --cache-from $REPOSITORY_URL -t $REPOSITORY_URL ./$BUILD_DIR/
docker push $REPOSITORY_URL
我想 运行 在我的 docker 容器上执行此命令:
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
我如何 运行 在 fargate 中启动容器时执行此命令?
对于 运行 在 docker 容器内执行命令,您需要在 docker 容器内。
第 1 步:找到您要调试的容器 ID/容器名称
docker ps
将显示容器列表,选择其中一个
步骤 2 运行 以下命令
docker exec -it <containerName/ConatinerId> bash
然后输入 wait 几秒,你就会进入 docker 容器,交互模式 Bash
有关详细信息,请阅读 https://docs.docker.com/engine/reference/commandline/exec/
简答,只需替换 CMD
CMD ["sh", "-c", " curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_UR && Rscript initiate.R"]
长答案,您需要替换 DockerFile 的 CMD,因为目前 运行仅 Rscript
。
你有两个选项添加 entrypoint
或更改 CMD
,对于 CMD
检查上面的
仅在您要调试时创建 entrypoint.sh
和 运行 运行。
#!/bin/sh
if [ "${IS_DEBUG}" == true ];then
echo "Container running in debug mode"
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
# uncomment below section if you still want to execute R script.
# exec "$@"
else
exec "$@"
fi
Dockerfile 端所需的更改
WORKDIR /home/project/
ENV IS_DEBUG=true
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
entrypoint ["/entrypoint.sh"]
CMD Rscript initiate.R