简体   繁体   English

如何在 Docker 容器内运行命令

[英]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.

相关问题 如何在 docker 容器内运行 AWS CW 代理? - How to run AWS CW Agent inside docker container? 如何在EC2容器中运行Docker镜像? - How to run a Docker image inside your EC2 container? 如何将ECS Docker Container ID放入Container内部的环境变量中 - How to put ECS Docker Container ID into a Environmental Variable inside the Container 如何在Elastic Beanstalk上自定义docker run命令? - How to customize the docker run command on Elastic Beanstalk? 当我们在没有 -it 的情况下触发 docker 运行命令时,如何运行 bash? - How to run the bash when we trigger docker run command without -it? 如何承担在Elasticbeanstalk环境中运行的Docker容器中的AWS角色? - How to assume AWS role in docker container run on Elasticbeanstalk environment? 如何读取docker容器内的系统环境变量 - How to read system environment variables inside docker container 如何在AWS的Docker容器中查找正在运行的进程的状态? - How to find the status of a running process inside docker container in AWS? 如何从 docker 容器内的主机工作站获取 aws 凭据 - how to get aws credentials from host workstation, inside docker container 在 Lambda 中运行任意 Docker 容器? - Run an arbitrary Docker container in Lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM