docker review image creation-container start

at first

Recently I only studied python and Vue.js and did not use EC2 or docker, so I reviewed from image creation to container startup.

EC2 instance launch

The image uses Amazon Linux2.

image.png

image.png

Install docker command

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

Build-start container

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

image.png

image.png

image.png

Build.

$ docker build -t original_image .

image.png

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.

Shell execution when container is executed

Add CMD to your Dockerfile.

$ vi ./mnt_dir/test.sh 
$ vi Dockerfile

image.png

image.png

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

docker review image creation-container start
[Docker] Start container, start bash in container, delete image
Docker + Spring-boot start
Delete unused docker image
Install yarn in docker image
Extract files from Docker Image
Image Spring Boot app using jib-maven-plugin and start it with Docker
Install openjdk8 on Docker image (Debian)
Docker test DB does not start
Run lambda with custom docker image
Create a lightweight STNS Docker image
Migrate Docker image to another server
Deploy Flask's Docker image on Heroku
[SRE / Docker] Start control with Dockerize
2. Create Docker image and register Registry
mysql doesn't start up with docker.
Restart apache with docker php-apache image
[Linux] Start Apache container with Docker
How to start Camunda with Docker
[Splunk] Build Splunk Enterprise (use docker image)