Create Docker Image with Nginx on AWS EC2 Instance

A.T.M Ruhul Amin
3 min readFeb 18, 2023

--

Here are a few steps we can follow to create a docker image with Nginx :

Install Nginx into EC2 instance :

  1. Connect to our EC2 instance via SSH.
  2. Update the package list and upgrade the system:
sudo apt update
sudo apt upgrade

3. Install Nginx using the following command:

sudo apt install nginx

4. Once the installation is complete, start the Nginx service using the following command:

sudo systemctl start nginx

5. Verify that Nginx is running by accessing the default Nginx page using the public IP address of our EC2 instance in our web browser. The default Nginx page should be displayed.

6. To ensure that Nginx starts automatically when the EC2 instance is rebooted, enable the Nginx service with the following command:

sudo systemctl enable nginx

That’s it! Nginx is now installed and running on the EC2 instance.

Now install Install Docker on the EC2 instance:

  1. Install Docker using the following command:
sudo apt install docker.io

2. Once the installation is complete, start the Docker service using the following command:

sudo systemctl start docker

3. Verify that Docker is running by running the following command:

sudo docker info

We should see information about our Docker installation, including the version and other details.

4. By default, the docker the command can only be run by the root user. To run Docker commands as a non-root user, we can add our user to the docker group using the following command:

sudo usermod -aG docker $USER

Replace $USER with the actual username.

5. To apply the changes, log out and log back into our EC2 instance.

That’s it! Docker is now installed and running on the EC2 instance.

Now Configure Docker Image with Nginx :

  1. Once Docker is installed, we can create a new Dockerfile that describes how to build our Nginx image. We can use a text editor to create a file called “Dockerfile” in a directory of our choice, with the following contents:
FROM nginx
COPY /path/to/nginx/conf /etc/nginx/

This Dockerfile will create a new image based on the official Nginx image and copy the Nginx configuration files into the image.

2. Build the Docker image by running the following command in the directory where our Dockerfile is located:

docker build -t my-nginx-image .

This command will build the Docker image using the Dockerfile and tag it with the name “demo-test-image”.

3. Once the image is built, we can run a Docker container based on the image using the following command:

docker run -d -p 80:80 demo-test-image

This command will start a Docker container based on the demo-test-image image and map port 80 on the container to port 80 on the host machine.

4. Finally, we can verify that the Nginx server is running by accessing the public IP address of our EC2 instance in a web browser.

If everything is working correctly, we should see the default Nginx welcome page.

--

--

A.T.M Ruhul Amin

Tech Lead | Java | Spring Boot | Python | React | Angular | Serverless | AWS Certified | AWS Community Builder | GitHub Link : https://github.com/ruhulmus