Recently I only studied python and Vue.js and did not use EC2 or docker, so I reviewed from image creation to container startup.
The image uses Amazon Linux2.
Copy and paste the official document as it is and install it.
$ sudo yum update -y
$ sudo amazon-linux-extras install docker
$ sudo service docker start
$ sudo usermod -a -G docker ec2-user
After logging in to the EC2 instance, create a Dockerfile with ~/my_dir
.
$ mkdir my_dir
$ cd my_dir
$ vi enigma.py
$ vi test.sh
$ vi Dockerfile
Build.
$ docker build -t original_image .
Create a directory mnt_dir
for mounting and move the files.
mkdir mnt_dir
$ ls
Dockerfile enigma.py mnt_dir test.sh
$ mv `ls | grep -v 'Docker' | grep -v 'mnt'` ./mnt_dir/
$ ls ./mnt_dir/
enigma.py test.sh
Start docker.
$ docker run -it -v `pwd`/mnt_dir:/mnt original_image /bin/bash
Confirm that it can be mounted.
root@67ac0dab37f2:/# ls /mnt/
enigma.py test.sh
python file execution.
root@67ac0dab37f2:/# python3 /mnt/enigma.py | tee /mnt/output.txt
FRZXI JFDRX. XV SYXU LV BZEXX. SJXO AG NLDE KEN.
HELLO WORLD. MY NAME IS ALICE. NICE TO MEET YOU.
root@67ac0dab37f2:/# cat /mnt/output.txt
FRZXI JFDRX. XV SYXU LV BZEXX. SJXO AG NLDE KEN.
HELLO WORLD. MY NAME IS ALICE. NICE TO MEET YOU.
Exit the container while keeping it running with Ctrl
+ P
+ Q
.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
67ac0dab37f2 original_image "/bin/bash" 3 minutes ago Up 3 minutes recursing_wozniak
There is no problem running python.
$ cat ./mnt_dir/output.txt
FRZXI JFDRX. XV SYXU LV BZEXX. SJXO AG NLDE KEN.
HELLO WORLD. MY NAME IS ALICE. NICE TO MEET YOU.
Add CMD
to your Dockerfile.
$ vi ./mnt_dir/test.sh
$ vi Dockerfile
Re-execute the build. After that, try starting the container several times.
$ docker build -t original_image .
$ docker run -v `pwd`/mnt_dir:/mnt original_image
$ docker run -v `pwd`/mnt_dir:/mnt original_image
$ docker run -v `pwd`/mnt_dir:/mnt original_image
$ docker run -v `pwd`/mnt_dir:/mnt original_image
Make sure the shell is running.
$ ls ./mnt_dir/
enigma.py mnt_file_20210116-05:25:01.txt mnt_file_20210116-05:29:38.txt test.sh
mnt_file_20210116-05:24:47.txt mnt_file_20210116-05:25:47.txt output.txt
Recommended Posts