How to install Docker in Nginx server on EC2 instance
Looking to set up Docker on an EC2 instance that’s already running Nginx? Whether you’re a seasoned pro or just stepping into the DevOps world, you’ve come to the right place! In this guide, we’ll walk you through every step to take a fresh EC2 instance from zero to a fully configured Docker environment. And don’t worry — we’ll keep it simple, fun, and easy to follow!

Prerequisites
Before we dive in, make sure you have:
- An AWS account with an EC2 instance running Ubuntu
- SSH access to your instance
- Basic familiarity with Linux commands
- A cup of tea ☕
Let’s Connect to Our EC2 Instance!
First things first, let’s SSH into our EC2 instance. If you’re using a .pem
file:
ssh -i your-key.pem ubuntu@your-instance-ip
Pro tip: Add this to your SSH config file to save typing time!
Step 1: System Preparation - The Foundation
Just like you wouldn't build a house on shaky ground, we need to make sure our system is up-to-date. Let's start with some housekeeping:
sudo apt update
sudo apt upgrade -y
Why this matters: These commands ensure we're working with the latest security patches and system updates. Think of it as getting a clean slate before we start our Docker adventure!
Step 2: Installing the Prerequisites - Getting Our Tools Ready
Time to install some essential packages. Think of these as our toolbox:
sudo apt install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
gnupg
What each tool does:
apt-transport-https
: Allows apt to download through HTTPSca-certificates
: Lets the system check security certificatescurl
: Tool for transferring datasoftware-properties-common
: Helps manage software repositoriesgnupg
: For encryption and signing, used to verify Docker packages
Step 3: Add Docker’s GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Purpose:
- Downloads Docker’s official GPG key
- Verifies that packages are from Docker (security measure)
- Prevents installation of malicious packages
Step 4: Add Docker Repository:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Purpose:
- Adds official Docker repository to your system
arch=$(dpkg --print-architecture)
: Detects your system architecture (x86_64, arm64, etc.)$(lsb_release -cs)
: Gets your Ubuntu version codename- Ensures you get the correct version of Docker for your system
Step 5: Update Repository
sudo apt update
Purpose:
- Refreshes package list with newly added Docker repository
Step 6: Install Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io
Components installed:
docker-ce
: Docker Community Edition enginedocker-ce-cli
: Command line interfacecontainerd.io
: Container runtime
Step 7: Post-Installation Setup - Making Life Easier
Let's make Docker start automatically and give ourselves permission to use it:
# Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker
# Add your user to the Docker group
sudo usermod -aG docker $USER
Pro tip: Log out and back in for the group changes to take effect!
Step 8: Testing Our Installation - The Victory Lap
Let's make sure everything works:
# Run the hello-world container
docker run hello-world
If you see a welcome message, congratulations! 🎉 You've successfully installed Docker!
Step 9: Security Configuration - Keeping Things Safe
Let's add some basic security configurations:
sudo nano /etc/docker/daemon.json
Add this configuration:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"no-new-privileges": true,
"live-restore": true,
"userland-proxy": false
}
Bonus: Nginx and Docker Working Together
Since we have Nginx running, we might want to open up the necessary ports:
sudo ufw allow http
sudo ufw allow https
Conclusion
And there you have it! You've successfully installed Docker on your EC2 instance with Nginx. Your server is now ready to run containers alongside your Nginx server. Remember, this is just the beginning - with Docker installed, you can now explore the vast world of containerization!