> Creating and connecting to an AWS EC2 instace
Docker is an open-source platform that allows you to automate the deployment and management of applications within lightweight, isolated containers. Containers are standalone environments that encapsulate an application along with its dependencies, enabling consistent and reliable software delivery across different computing environments.
For the purpose of this tutorial, we're going to deploy Nginx web server within our EC2 instance which is running Amazon Linux. All commands should work for any other linux distro, but installation will only work only for Red Hat based distros or distros with yum as package manager. For other faamilies you might need to install using other managers like apt in debian based distributions.
We'll be running all of the following commands as root, so first step will be to switch from ex2-user to root. Then, we're gonna update the yum repository. It is generally a good practice to update the package repository before installing any new applications to ensure you have the latest versions of the packages and any necessary security updates. Finally we can run the yum install command.
[] $ sudo su -
[] $ yum update
[] $ yum install docker -y
Once installation gets finished, you can check the docker version in order to verify that it got installed properly as long as it shows some version number like this:
[] $ docker --version
Docker version 20.10.17, build 100c701
Docker is composed of two main components: a command line interface client (cli) and a daemon service. Wvery time we run a docker command, the cli amkes a request to the background service which takes care of fetching images, running containers, check containers that are running, etc.
Having that said, now we need to start the docker background service in our machine. For this we will use the standard service management commands to start and check that docker service is running:
[] $ service docker start
Redirecting to /bin/systemctl start docker.service
[] $ service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; preset: disabled)
Active: active (running) since Tue 2023-05-16 21:29:32 UTC; 2s ago
...
If you see an output similiar like above (Active: active (running) ), that means that the docker service is already available on your instance.
At this point, we are able to start executing the docker commands. First of all, we need to fetch an image. Docker images are the building blocks of containers in Docker. They are self-contained, portable, and lightweight packages that contain everything needed to run an application. So we can download the nginx image for our tutorial.
[] $ docker pull nginx
[] $ docker images
As you can see, the first command will download the nginx image, then the second one will show the following information related to all images that are already available:
As containers are created from images, now we can start our container from the image we already pulled by executing the following command:
[] $ docker run -d --name webserver -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
This long command will start our container with an already configured and running Nginx server. The meaning of the command and parameters used in the above command are:
Note that before running this command you have to create and/or move to the directory where you want to store the web page files you want to expose due to the Volume being configured with -v parameter.