Search code examples
amazon-web-servicesdockeramazon-ecsamazon-ecr

How to run command inside Docker container


I'm new to Docker and I'm trying to understand the following setup.

I want to debug my docker container to see if it is receiving AWS credentials when running as a task in Fargate. 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.

Here is the 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:

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:

curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI

How I run this command on container startup in fargate?


Solution

  • Short answer, just replace the 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.

    you have two option add entrypoint or change CMD, for CMD check above

    create entrypoint.sh and run run only when you want to debug.

    #!/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

    WORKDIR /home/project/
    ENV IS_DEBUG=true
    COPY entrypoint.sh /entrypoint.sh
    RUN chmod +x /entrypoint.sh
    entrypoint ["/entrypoint.sh"]
    CMD Rscript initiate.R