AWS Remote SSH with VSCode + Apache Server in Ubuntu with Docker
Docker is like the shipping container of the software world. It helps developers put their apps and everything those apps need into a special container. This container is like a small process that can run the app anywhere, whether on a laptop or a big server. What is nice is that Docker makes sure the app works the same everywhere, so no more “it works on my computer” issues. It is a way to neatly package your app so that it’s easy to send to others and works smoothly no matter where it goes.
In this article, we will briefly touch on how to access an EC2 instance on VSCode via the Remote SSH extension. Following that explanation, we will install an Apache server on a Docker Ubuntu Image using BASH. Additionally, we will verify we can reach the web server. Lastly, we will push our new image to Dockerhub.
Prerequisites: AWS Account, VSCode (Docker Extension), Docker Account
Briefly, let us touch on connecting EC2 to VSCode. Create your instance in the EC2 Dashboard as you normally would, and ensure you have a key pair with its file path noted. Ensure that you have Port 22 open in your security group. Navigate to the “Connect” tab and take note of the highlighted command to SSH. We will come back to it, momentarily.
In VSCode, on the left tab, you should see four squares that represent the “Extension” tab. Look up Remote SSH and hit download.
Click on the green bar and select “Connect to Host” → “Configure SSH Host. The first option should correspond to the current user. Click on it. Remember the SSH command from above? Observe how we format it.
The host is our service, which is aws-ec2. The HostName is .ec2–35–153–183–94.compute-1.amazonaws.com. Ubuntu represents the user. Lastly, we IdentifyFile with the location of our key pair used to create EC2.
Save the configuration file and you are ready to connect to your Host. Click the same icon on the bottom left and select Connect to Host. You should now be able to see your host. A successful connection will reflect a yellow icon on the bottom left. This marks the end of the Remote SSH Section.
To proceed with the Docker section, ensure you have the Docker extension on VSCode. From there, install docker on your remote client. I used “docker -help” to ensure the right command was used for installing the packages.
We are tasked to run a Docker Ubuntu container running detached and on port 80. Bash must be used to update all the packages. This can be done with the following command, in which we name “instance_docker”.
docker container run -d -p 80:80 -it --name instance_docker ubuntu bash
If you do not have the Docker image, it will be pulled automatically from Dockerhub. To ensure the container is running, execute “docker ps -a”.
To interact with the container and its internal process, we will use “exec”, along with updating its packages with the following commands.
docker exec -it instance_docker bash // Enter the container
apt update -y // Update packages
While still in the container, run the following to install and ensure the Apache server is installed on the container.
apt install apache2 -y // install apache
service apache2 start // start apache
service apache2 status // verify it is working
Refer back to your EC2 instance security group. In addition to Port 22, also open up Port 80, and select traffic from “Anywhere”. This will allow HTTP web traffic to access your Apache server. Paste your public IPv4 address.
Lastly, to push our image, we log in with Docker credentials. Before pushing to the Dockerhub repo, we must create an image of our container.
docker login // will prompt username and password. Response should be successful
docker commit instance_docker //creates a new image
docker image ls // view current images
Lastly, to tag and push our image, we will use the following command. We start with our image (ubuntu) and tag it to our desired repository. We assign it a custom name, “latest”.
docker image tag ubuntu yasht44/luit:latest
docker puush yasht44/luit:latest
Ensure that you use your docker username correctly. If done correctly, your image should appear on your repository hosted in Dockerhub.