EC2 on Docker-compose

This is an article to create a container for Web server and DB server by putting docker-compose in EC2 instance. [Almost plagiarism in this article. ](Https://qiita.com/yumatsud/items/33bc22f7d8f640a286f4#web%E3%82%B5%E3%83%BC%E3%83%90%E3%81%A8db%E3%82%B5%E3% 83% BC% E3% 83% 90% E3% 81% AE% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 83% 8A% E4% BD% 9C% E6% 88% 90)

Creating an EC2 instance

① Click "Create Instance" Screen Shot 2020-08-31 at 16.24.13.png

② Select Amazon Linux 2 with free tier

③ Select "t2.micro (subject to free usage tier)"

④ Click "Confirm and Create"

⑤ Save key If you do not save it here, you will not be able to see it again, so be sure to download it.

⑥ Add to security group Type: HTTP (80) Source: 0.0.0.0/0 Screen Shot 2020-08-31 at 16.34.17.png Screen Shot 2020-08-31 at 16.34.55.png

Log in to AWS EC2

(local)$ ssh -i key_portfolio_AWS.pem [email protected]

Change key_portfolio_AWS.pem and 18.181.96.192 to suit your situation.

Docker installation

(ec2-user|~)$ sudo yum update

(ec2-user|~)$ sudo yum install -y docker

(ec2-user|~)$ sudo service docker start
Redirecting to /bin/systemctl start docker.service

(ec2-user|~)$ sudo usermod -a -G docker ec2-user

(ec2-user|~)$ cat /etc/group |grep docker
docker:x:993:ec2-user

Log out and log in to EC2 again.

(ec2-user|~)$ exit  #Logout

(local)$ ssh -i key_portfolio_AWS.pem [email protected]  #Log in again

(ec2-user|~)$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
・ ・ ・

Install Docker-compose

(ec2-user|~)$ sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

(ec2-user|~)$ sudo chmod +x /usr/local/bin/docker-compose

(ec2-user|~)$ docker-compose --version
docker-compose version 1.16.1, build 6d1ac21

Creating a container for data storage

(ec2-user|~)$ vi Dockerfile

FROM busybox

#Creator information
MAINTAINER 0.1 [email protected]

#Data settings
VOLUME /var/lib/mysql

Creating a container

(ec2-user|~)$ docker build -t data-only .
Sending build context to Docker daemon  9.216kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
61c5ed1cbdf8: Pull complete 
Digest: sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977
Status: Downloaded newer image for busybox:latest
 ---> 018c9d7b792b
Step 2/3 : MAINTAINER 0.1 [email protected]
 ---> Running in 3b3dcfb4036b
Removing intermediate container 3b3dcfb4036b
 ---> 314a9cc31b09
Step 3/3 : VOLUME /var/lib/mysql
 ---> Running in 24ff3d39dd8a
Removing intermediate container 24ff3d39dd8a
 ---> 16e1b09a9faf
Successfully built 16e1b09a9faf
Successfully tagged data-only:latest

(ec2-user|~)$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
data-only           latest              16e1b09a9faf        7 seconds ago       1.22MB
busybox             latest              018c9d7b792b        4 weeks ago         1.22MB

Start container

(ec2-user|~)$ docker run -it --name data-only data-only

/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var

/ # exit

(ec2-user|~)$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
475afb3d38f8        data-only           "sh"                15 seconds ago      Exited (0) 6 seconds ago                       data-only

Web server and DB server container creation

(ec2-user|~)$ vi docker-compose.yml

docker-compose.ym


web-server:
  image: wordpress

  ports:
    - "80:80"

  #Container link specification
  links:
    - "db-server:mysql"

db-server:
  image: mysql

  #Specifying the data save destination
  volumes_from:
    - data-only

  #Specifying environment variables
  environment:
    MYSQL_ROOT_PASSWORD: password

Start container

(ec2-user|~)$ docker-compose up -d

(ec2-user|~)$ docker-compose ps
        Name                      Command               State          Ports       
-----------------------------------------------------------------------------------
ec2user_db-server_1    docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp
ec2user_web-server_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:80->80/tcp 

WordPress is running up to this point. Screen Shot 2020-08-31 at 17.13.52.png

Then Screen Shot 2020-08-31 at 17.15.11.png

Oh! If it is displayed like this, it is successful.

Check if it is mounted in a data-only container

Mount means "to make the computer recognize the devices and media connected to the computer and make them ready for use".

(ec2-user|~)$ docker start -ia data-only
/ # ls /var/lib/mysql
#ib_16384_0.dblwr   auto.cnf            binlog.index        client-cert.pem     ib_logfile0         ibtmp1              performance_schema  server-cert.pem     undo_001
#ib_16384_1.dblwr   binlog.000001       ca-key.pem          client-key.pem      ib_logfile1         mysql               private_key.pem     server-key.pem      undo_002
#innodb_temp        binlog.000002       ca.pem              ib_buffer_pool      ibdata1             mysql.ibd           public_key.pem      sys                 wordpress
/ # exit

Other

--Example of command execution in a container

(ec2-user|~)$ docker-compose run web-server /bin/bash
root@40e7f18520cb:/var/www/html# 

--How to backup and restore data-only container

#backup
$ docker export data-only > backup.tar

#restore
$ tar xvf backup.tar

from now on

This time, it was wordpress. Next, let's try the Rails app.

Reference article

・ [General](https://qiita.com/yumatsud/items/33bc22f7d8f640a286f4#web%E3%82%B5%E3%83%BC%E3%83%90%E3%81%A8db%E3%82%B5 % E3% 83% BC% E3% 83% 90% E3% 81% AE% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 83% 8A% E4% BD% 9C% E6 % 88% 90) -Create an EC2 instance -Install docker-compose ・ [Add Inbound Rule](https://qiita.com/naoki_mochizuki/items/f795fe3e661a3349a7ce#5%E3%82%BB%E3%82%AD%E3%83%A5%E3%83%AA%E3% 83% 86% E3% 82% A3% E3% 83% BC% E3% 82% B0% E3% 83% AB% E3% 83% BC% E3% 83% 97% E3% 81% AE% E4% BD% 9C% E6% 88% 90) ・ Rough mount meaning

Recommended Posts

EC2 on Docker-compose
Launch Rails on EC2
Deploy RAILS on EC2
Install docker-compose on a Graviton 2 instance of AWS EC2
Install docker on AWS EC2
Error when deploying EC2 on CircleCI
Launch Rails on EC2 (manual deployment)
Install docker and docker-compose on Alpine Linux
EFS mount on AWS Ubuntu EC2 (amazon-efs-utils)
Launch docker container on EC2 (personal memorandum)
try docker-compose
Command docker-compose
Error installing ruby execution environment on EC2 instance
I installed Docker on EC2 and started it
Install Java, Apache, Tomcat9 on EC2 (Amazon Linux2)
I tried installing docker on an EC2 instance
Recipe for deploying Rails apps on AWS EC2
EC2 ✕ Manual deployment with Docker / docker-compose / Asset compilation