Launch docker container on EC2 (personal memorandum)

Create an instance of ubuntu on EC2 and create a key pair

Instance creation is omitted

-#Change key pair permissions
%chmod 400 key pair.pem
-#Ssh connect to ubuntu instance
% ssh -i key pair.pem ubuntu@EC2 public DNS
yes

The purpose of changing the authority of the key pair is to use "400" and ** read-only ** key pair because ssh connection cannot be made if it is rewritten by mistake. Note that EC2 public DNS changes by ** stopping ** and ** starting ** the instance!

Install docker on EC2

-#I don't have access privileges, so I use sudo to update.
$ sudo apt-get update
-#install docker
$ sudo apt-get install docker.io
-#Press Y
-#Check docker version
$ docker -—version

At this rate, ubuntu doesn't have permission for docker, so you have to prefix the command with ** "sudo" ** every time. So create a group called "docker" and put "ubuntu" in it. Then, since "ubuntu" belongs to the "docker" group, docker can be used and commands can be used without "sudo".

-#Put "ubuntu" in a group called "docker"
$ sudo glassed -a ubuntu docker
Adding user ubuntu to group docker
-#It will not be reflected unless it comes out once
$ exit
-#ssh connect
% ssh -i key pair.pem ubuntu@EC2 public DNS
-#Check docker command
$ docker images

You can confirm that the docker command can be used.

Compress Docker image to tar and send to EC2

-#Creating a Docker image to send to EC2
% mkdir temp_folder
% cd temp_folder
% vim Dockerfile

Dockerfile


FROM alpine
RUN touch test
% docker build .

Create a lightweight alpine and create a file called "test" in it.

Convert to tar file

-#docker save image name>New name.tar
% docker save b3fc1a6f13e6 > myimage.tar
-#Check tar file
% ls

Access EC2 using SFTP.

The command to use when transferring files.

-#Enter sftp(The state where the host and the instance are connected)
% sftp -I key pair.pem ubuntu@EC2 public DNS
-#put File path to send File path to destination
sftp> put temp_folder /home/ubuntu 
-#Open another terminal, change to the directory where the key pair is, and ssh it.
% ssh -i mydocker.pem [email protected]
-#Check if there is a file
$ ls

You can see that the file has been transferred from the host to EC2.

Transfer files from EC2 to host

-#Creating a file on EC2
$ touch test
-#Open another terminal, go to the host side, enter sftp
% sftp -I key pair.pem ubuntu@EC2 public DNS
-#Make sure you have the test file
sftp> ls
-#Get the test file
sftp> get test
-#Exit sftp
sftp> exit

Make sure you have a "test" file on your desktop

Return tat file to docker image

#ssh connect
% ssh -i key pair.pem ubuntu@EC2 public DNS
# docker load <Return to image with tar faril
$ docker load < my image.tar
#Confirm image
$ docker images
# docker run(alpine doesn't have bash so sh)
$ docker run -it image ID sh
#Make sure you have the test file with the ls command
ls

Send the Dockerfile directly to EC2 and launch it.

-#Enter sftp
% sftp -I key pair.pem ubuntu@EC2 public DNS
-#Send Dockerfile with put, if you do not specify the destination, it will be put to ubuntu's home directory
%Absolute path of put Dockerfile
-#ssh connect
% ssh -i mydocker.pem [email protected]
-#Check the Dockerfile
$ ls
-#Creating a build context
$ mkdir dsenv_build
-#Move Dockerfile
$ mv Dockerfile dsenv_build/
$ cd dsenv_build
-#Launch the container
$ docker build .

However, ** storage (8GB) ** is not enough to build.

Check your computer's disk space

# -h at M,Gbyte display
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            476M     0  476M   0% /dev
tmpfs            98M  768K   98M   1% /run
/dev/xvda1      7.7G  5.1G  2.7G  66% /
tmpfs           490M     0  490M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           490M     0  490M   0% /sys/fs/cgroup
/dev/loop0       97M   97M     0 100% /snap/core/9804
/dev/loop1       29M   29M     0 100% /snap/amazon-ssm-agent/2012
tmpfs            98M     0   98M   0% /run/user/1000

With 8GB, "/" is already using 5.1G, and the rest is only 2.7GB, so when building, there is not enough storage to build. By the way, in case of Linux, the storage location of Docker object is "/ var / lib / docker /".

Change EC2 storage.

  1. Click the Elastic Block Store volume on the EC2 instance page.
  2. Check any instance and change the volume with "Action".
  3. Set the size to 20GB.
  4. Refresh the screen and check that the size is 20GB.
  5. It may not be reflected in the instance, so restart the instance.

Build again.

-#ssh connection
% ssh -i mydocker.pem [email protected]
-#Go to the location of the Dockerfile
$ cd dsenv_build
-#Build again
$ docker build .
-#Now that the storage is 20GB, you can build without any problems.
$ docker run -v~:/work -p 8888:8888 Image ID

You can confirm that you can connect with EC2 public DNS: 8888.

Container permissions

-# $sudo adduser —uid any ID username(Note that users cannot be created without sudo privileges)
-#I'm asked a lot, but this time I omitted everything with enter
$ sudo adduser --uid 1111 aaa
-# /home/Moved because a directory called aaa has been created
$ cd /home/aaa
-#Create another user
$ sudo adduser --uid 2222 bbb
-#Return to home directory
$ cd /home
-#Check file permissions.
$ ls -la
-#You can check the permissions of the aaa and bbb files, and you can see that aaa cannot write to the bbb directory.
-#Enter as a aaa user
docker run -u 1111 -v /home/aaa:/home/aaa -v /home/bbb:/home/bbb -it ubuntu bash
-#Confirm id
1111
It can be confirmed that it is entered as a user of aaa.
-#Move to bbb directory
$ cd /home/bbb
-#Create file
$ touch test

However, it can be confirmed that it cannot be created because it does not have write permission.

Recommended Posts

Launch docker container on EC2 (personal memorandum)
Launch Rails on EC2
Docker Machine (personal memorandum)
Docker network (personal memorandum)
Install docker on AWS EC2
[Docker] Build an Apache container on EC2 using dockerfile
Creating a docker host on AWS using Docker Machine (personal memorandum)
Run React on a Docker container
Run GUI application on Docker container
Launch Rails on EC2 (manual deployment)
Run PureScript on a Docker container
Docker memorandum
Docker memorandum
Run NordVPN on Docker (Windows) Ubuntu container
Starting with installing Docker on EC2 and running Yellowfin in a container
Summary of Docker understanding by beginners ⑤ ~ Until deploying docker container on EC2 instance ~
Technical memorandum (Docker)
Run GUI application on Docker container (Japanese input)
Liberty on Docker
Building a CICD pipeline using Docker (personal memorandum)
Data management using volume in Docker (personal memorandum)
I installed Docker on EC2 and started it
Send emails using Docker container on Raspberry Pi 3
Small Docker container
Update container image with KUSANAGI Runs on Docker
I tried running Ansible on a Docker container
Docker command memorandum
I tried installing docker on an EC2 instance
EC2 on Docker-compose
Redmine on Docker
Systemctl cannot be used on Ubuntu inside Docker container
Deploy laravel using docker on EC2 on AWS ① (Create EC2 instance)
Data management using Docker bindmount and tmpfs (personal memorandum)
Run the Android emulator on Docker using Android Emulator Container Scripts
GitHub Actions/Cache: Cache bundle installed gems on Docker container
A memorandum when installing Docker and building a Linux container
Construction of data analysis environment using Docker (personal memorandum)
Docker installation on CentOS 6
Lambda on Terraform Container
python notes on docker
Launch MariaDB with Docker
Install Docker on Manjaro
Deploy RAILS on EC2
docker command personal summary
Docker container usage scene
M.S. docker on Windows
Docker installation on WSL2
Run phpunit on Docker
Deploy laravel using docker on EC2 on AWS ② (Elastic IP acquisition-linking)
Launch Nuxt.js + Rails API on Docker and try CRUD operation
Deploy laravel using docker on EC2 on AWS ④ (git clone ~ deploy, migration)
I tried running a Docker container on AWS IoT Greengrass 2.0
Time is wrong with the application launched on the Docker container
Try Hello World using plain Java on a Docker container