skip to Main Content

I have a Dockerfile as follow:

FROM centos
RUN mkdir work
RUN yum install -y python3 java-1.8.0-openjdk java-1.8.0-openjdk-devel tar git wget zip
RUN pip install pandas
RUN pip install boto3
RUN pip install pynt
WORKDIR ./work
CMD ["bash"]

where i am installing some basic dependencies.
Now when I run

docker run imagename

it does nothing but when I run

docker run -it imageName

I lands into the bash shell. But I want to get into the bash shell as soon as I trigger the run command without any extra parameters.

I am using this docker container in AWS codebuild and there I can’t specify any parameters like -it but I want to execute my code in the docker container itself.
Is it possible to modify CMD/ENTRYPOINT in such a way that when running the docker image I land right inside the container?

2

Answers


  1. Chosen as BEST ANSWER

    We cannot login to the docker container directly. If you want to run any specific commands when the container start in detach mode than either you can give it in CMD and ENTRYPOINT command of the Dockerfile.

    If you want to get into the shell directly, you can run

    docker -it run imageName

    or

    docker run imageName bash -c "ls -ltr;pwd"

    and it will return the output. If you have triggered the run command without -it param then you can get into the container using:

    docker exec -it imageName

    and you will land up into the shell.

    Now, if you are using AWS codebuild custom images and concerned about how the commands can be submitted to the container than you have to put your commands into the build_spec.yaml file and put your commands either in pre_build, build or post_build parameter and those commands will be submitted to the docker container.

    -build_spec.yml

    version: 0.2
    phases:
      pre_build:
        commands:
          - pip install boto3 #or any prebuild configuration
    
      build:
        commands:
          - spark-submit job.py
      post_build:
        commands:
          - rm -rf /tmp/*
    

    More about build_spec here


  2. I checked your container, it will not even build due to missing pip. So I modified it a bit so that it at least builds:

    FROM centos
    RUN mkdir glue
    RUN yum install -y python3 java-1.8.0-openjdk java-1.8.0-openjdk-devel tar git wget zip python3-pip
    RUN pip3 install pandas
    RUN pip3 install boto3
    RUN pip3 install pynt
    WORKDIR ./glue
    

    Build it using, e.g.:

    docker build . -t glue
    

    Then you can run command in it using for example the following syntax:

    docker run --rm  glue bash -c "mkdir a; ls -a; pwd"
    

    I use --rm as I don’t want to keep the container.

    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search