[Docker] Build an Apache container on EC2 using dockerfile


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


1. Start Docker daemon

** ① 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
 Debug Mode: false

 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
  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:
   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
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
 Live Restore Enabled: false

2. Create Docker image

** ① Create PHP file for container deployment ** Create a PHP file to deploy in the container you are creating.

vi test.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


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.

[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

3. Start Docker container

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>80/tcp   testweb

4. Connection verification

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

[Docker] Build an Apache container on EC2 using dockerfile
Launch docker container on EC2 (personal memorandum)
Quick build maven project using maven docker container
Build an authentication proxy server using Docker
Build an environment with Docker on AWS
Build an Ultra96v2 development environment on Docker 1
Build a Kotlin app using OpenJDK's Docker container
Send emails using Docker container on Raspberry Pi 3
I tried installing docker on an EC2 instance
Easily build Redmine on Windows using WSL2 and Docker
Deploy laravel using docker on EC2 on AWS ① (Create EC2 instance)
Run the Android emulator on Docker using Android Emulator Container Scripts
I tried to build an environment using Docker (beginner)
Install docker on AWS EC2
multi-project docker build using jib
[Amateur remarks] Build multiple WordPress on AWS using Docker Compose
Deploy laravel using docker on EC2 on AWS ② (Elastic IP acquisition-linking)
Deploy laravel using docker on EC2 on AWS ④ (git clone ~ deploy, migration)
Try Hello World using plain Java on a Docker container
Try using Redmine on Mac docker
Run React on a Docker container
Run GUI application on Docker container
Build an ASP.net Core Web API environment on Docker (VSCode) Part 1
Build Clang x VSCode on Docker (1)
Build an ASP.NET Core Web API environment on Docker (VSCode) Part 2
Run PureScript on a Docker container
Using Docker on Windows10 Home WSL2
[Linux] Start Apache container with Docker
Build Unity development environment on docker
How to build CloudStack using Docker
Build WebRTC Janus with Docker container
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Build an environment of "API development + API verification using Swagger UI" with Docker
Starting with installing Docker on EC2 and running Yellowfin in a container
[Docker] How to build when the source code is bind-mounted on the container
Summary of Docker understanding by beginners ⑤ ~ Until deploying docker container on EC2 instance ~
Created a Docker container image for an OpenLDAP server based on Fedora
Deployed using Docker + Rails + AWS (EC2 + RDS)
Run NordVPN on Docker (Windows) Ubuntu container
Docker container build fails to install php-radis
Build Redmine code reading environment on Docker
Send an email using JavaMail on AWS
Build Apache + Tomcat + Pebble locally on Mac
Until you run apache on ubuntu on docker
Building an environment for WordPress, MySQL and phpMyAdmin with Docker Compose on EC2
[App development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]
Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
Using templates on the classpath with Apache Velocity
Build a test flow on CircleCI using Jib
Run GUI application on Docker container (Japanese input)
[Java] Creating an Excel file using Apache POI
Extract key phrases using Text Analytics Docker container
Create an EC site using stripe! (Account creation)
Build Apache / Tomcat development environment on Cent OS 7
Build Java x Spring x VSCode x Gradle on Docker (1)
I installed Docker on EC2 and started it
Build a Laravel environment on an AWS instance
Build a container for Docker x Laravel phpMyAdmin
Update container image with KUSANAGI Runs on Docker
I tried running Ansible on a Docker container
Install Java, Apache, Tomcat9 on EC2 (Amazon Linux2)