How do i create a docker container using a dockerfile to run a bash script?



  • Man page for docker build.
    Man page for Dockerfile.

    The main part here is the CMD command in the Dockerfile, take a look at the man page for Dockerfile for full details.

    Create a Dockerfile.

    FROM bash:latest
    
    COPY script.sh /
    
    CMD ["bash", "/script.sh"]
    

    Create a script.

    #!/bin/bash
    echo "Hello World!"
    

    Build the image.

    $ docker build -t mybash-script .
    Sending build context to Docker daemon  64.99MB
    Step 1/3 : FROM bash:latest
    latest: Pulling from library/bash
    cd784148e348: Already exists 
    f4b5283d91eb: Pull complete 
    d2127478c6ed: Pull complete 
    Digest: sha256:f633ed316cef627bcf3e12a6218199bdaea4675cb85902fed104214b2958a856
    Status: Downloaded newer image for bash:latest
     ---> ae95a7aa5142
    Step 2/3 : COPY script.sh /
     ---> fc83a375c657
    Step 3/3 : CMD ["bash", "/script.sh"]
     ---> Running in 6c1873400209
    Removing intermediate container 6c1873400209
     ---> 9e90bef7e1d4
    Successfully built 9e90bef7e1d4
    Successfully tagged mybash-script:latest
    

    Run a container using our image.

    $ docker run -it --rm --name bash-container mybash-script          
    Hello World!
    

Log in to reply
 

© Lightnetics 2024