Build an Apache container on your EC2 instance using dockerfile.
It is a memorandum memo of Docker learning. Basically, it is built with reference to the contents of the following site (*). The basic container and Docker concepts are also described in an easy-to-understand manner, so I think you should refer to this.
-EC2 instance (using Amazon Linux 2 AMI (HVM), SSD Volume Type) (*) has already been built.
Item number | title |
---|---|
1 | Start Docker daemon |
2 | Creating a Docker image |
3 | Start Docker container |
4 | Connection verification |
** ① Install Docker ** After logging in to the EC2 instance with the OS, install Docker.
sudo yum install -y docker
** ② Enable Docker daemon startup and automatic startup **
sudo systemctl start docker
sudo systemctl status docker
sudo systemctl enable docker
sudo systemctl is-enabled docker
** ③ Make ec2-user belong to docker group **
sudo usermod -a -G docker ec2-user
cat /etc/group | grep docker
After logging out (*), check that the Docker system information is displayed with docker info
.
[ec2-user@ip-172-31-32-204 ~]$ docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.13-ce
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: c623d1b36f09f8ef6536a057bd658b3aa8632828
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0 (expected: fec3683)
Security Options:
seccomp
Profile: default
Kernel Version: 4.14.209-160.339.amzn2.x86_64
Operating System: Amazon Linux 2
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 983.3MiB
Name: ip-172-31-32-204.ap-northeast-1.compute.internal
ID: RKGH:NETA:2FJW:FP7Q:X52F:F2DG:NSPR:YCQD:RYG2:XN54:ATEO:CLYY
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
** ① Create PHP file for container deployment ** Create a PHP file to deploy in the container you are creating.
vi test.php
test.php
<?php
echo "hoge";
?>
** ② Create docker file **
Based on the CentOS image obtained from Dockerhub (FROM
), install httpd and php (RUN
), and copy the test.php created earlier to/var/www/html (COPY
) and incorporate it. In addition, create a dockerfile
to execute the flow such as executing the httpd start command (CMD
) and creating a new Docker image when the container is started (*).
vi dockerfile
dockerfile
FROM centos:centos7
RUN yum -y install httpd php
COPY test.php /var/www/html/
CMD ["/usr/sbin/httpd","-DFOREGROUND"]
** ③ Create Docker image **
Use docker build
to create a docker image with the flow in the dockerfile applied.
This command is intended to create a docker image named testphp using the dockerfile that exists in the current directory.
# Successfully tagged testphp:OK if it is output to the latest and last line
docker build -t testphp .
Check the docker image list with docker images
.
You can check the creation of the base CentOS and the customized testphp (*) Docker image from it.
latest
.[ec2-user@ip-172-31-32-204 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testphp latest f5e7748e4f37 2 minutes ago 348MB
centos centos7 8652b9f0cb4c 6 weeks ago 204MB
From the created Docker image, start the docker container testweb with docker run
.
Start in the background with -d
(console operation is not possible after that when started in the foreground, so basically this option is added), and with -p 8080: 80
, local host port 8080 → container port 80 Set to forward to.
docker run -d -p 8080:80 --name testweb testphp:latest
You can check the container list with docker ps
and see the container named testweb.
[ec2-user@ip-172-31-32-204 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
216b216e14bf testphp:latest "/usr/sbin/httpd -DF…" 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp testweb
Access from a browser with <EC2 global IP address>: 8080/test.php
, and it is OK if the page displayed as hoge is displayed.
Recommended Posts