Use bash command to wrap Docker command to build , run and stop docker

Use bash command to wrap Docker command to build , run and stop docker

People often say that docker is for DevOps, well, I don't have DevOps background, however, docker is so funny that I have to dig in just a little bit deeper to see what's there ...

well, when I first started using docker, I always find it difficult to memorize all the docker command correctly, therefore, when I did succeed, I want to make sure I could reproduct the same thing next time, which is why I am eager to see whether I can wrap docker command into bash command and create .sh scripts from this exercise ...

Below is such an experiment to achieve the following ---

(1) building a docker image from Dockerfile using bash command wrap the orignal docker command which take in user input

(2) running a docker image given user input with bash command wrapping the original docker command

(3) bind mounting a host directory to docker local directory and see the magic of creating a new file resulting in the same change from inside docker

(4) stop docker using bash command with user specified docker friendly name

Let's get started, here is my very simple Dockerfile

No alt text provided for this image

Note: if you dont have ; sleep infinity there in the end , as soon as the CMD command is run, it will exit the docker container, which is not what we want, therefore, make sure ;sleep infinity there to ensure it will still run

now let's build this file with bash command to complete task (1)

No alt text provided for this image

so what's this bash build_docker.sh is about ?

sudo docker build --build-arg build_env=$1 . -t ztest

now we are going to run this docker we just built , that's task (2)

the run.sh command is as following

sudo docker container run -d -it --name $1 -p 8080:8080 -v $(pwd):/usr/share2host ztest:latest 

notice that not only did I need a user input friendly docker name ( I called it zenotest1 ) , I also mount host current directory onto /usr/share2host which is default WORKDIR when I created the Dockerfile

No alt text provided for this image

that conclude task (2)

but here is a funny thing about docker , for task (3)

we first need a bash command that allow us to do ''ls'' inside docker's current working directory ( = /usr/share2host ) , now if we use sudo docker exec -it zenotest1 bash -c ls (=> bash inside_docker_print.sh ) command , it will print all files in the current working dir which pointing to the exact same directly in my host machine

so now if i create a new file using touch command as below shown

well, it should and it certainly did show up inside docker directory as well ! it's like magic Y^__^Y

No alt text provided for this image

oki, now we are done with the experiments for the day, we want to clear things up , finish things up with task (4)

No alt text provided for this image

there, it's all clean and tidy up !

Happy docker experimenting !!!



To view or add a comment, sign in

More articles by Zenodia Charpy

Others also viewed

Explore content categories