[英]How to run command inside Docker container
I'm new to Docker and I'm trying to understand the following setup.我是 Docker 的新手,我正在尝试了解以下设置。
I want to debug my docker container to see if it is receiving AWS credentials when running as a task in Fargate
.我想调试我的 docker 容器,以查看它在Fargate
中作为任务运行时是否正在接收 AWS 凭证。 It is suggested that I run the command:建议我运行命令:
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
But I'm not sure how to do so.但我不知道该怎么做。
The setup uses Gitlab CI to build and push
the docker container to AWS ECR
.该设置使用 Gitlab CI 构建 docker 容器并将其push
送到AWS ECR
。
Here is the dockerfile:这是 dockerfile:
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
Here is the gitlab-ci.yml
file:这是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
Here is the build and push script:这是构建和推送脚本:
#!/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
I'd like to run this command on my docker container:我想在我的 docker 容器上运行这个命令:
curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
How I run this command on container startup in fargate?如何在 Fargate 的容器启动时运行此命令?
For running a command inside docker container you need to be inside the docker container.要在 docker 容器内运行命令,您需要在 docker 容器内。
Step 1: Find the container ID / Container Name that you want to debug第 1 步:找到您要调试的容器 ID/容器名称
docker ps
A list of containers will be displayed, pick one of them docker ps
将显示容器列表,选择其中一个
Step 2 run following command docker exec -it <containerName/ConatinerId> bash
and then enter wait for few seconds and you will be inside the docker container with interactive mode Bash
Step 2 run following command docker exec -it <containerName/ConatinerId> bash
and then enter wait for few seconds and you will be inside the docker container with interactive mode Bash
for more info read https://docs.docker.com/engine/reference/commandline/exec/欲了解更多信息,请阅读https://docs.docker.com/engine/reference/commandline/exec/
Short answer, just replace the CMD
简短的回答,只需更换CMD
CMD ["sh", "-c", " curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_UR && Rscript initiate.R"]
Long answer, You need to replace the CMD of the DockerFile, as currently running only Rscript
.长答案,您需要更换 DockerFile 的 CMD ,因为当前仅运行Rscript
。
you have two option add entrypoint
or change CMD
, for CMD
check above你有两个选项添加entrypoint
点或更改CMD
,对于CMD
检查上面
create entrypoint.sh
and run run only when you want to debug.创建entrypoint.sh
并仅在要调试时运行 run。
#!/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
Changes that will required on Dockerfile side 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.