To launch an Amazon EC2 free-tier instance and install Docker:
Go to the EC2 dashboard and click Launch Instance.
Choose a free-tier eligible AMI image like Amazon Linux 2 AMI.
Select a free-tier eligible instance type like t2.micro.
Configure storage, VPC details, and add appropriate tags.
Configure a security group to allow inbound traffic like SSH.
Review and launch the instance.
Connect to the instance using SSH. The public DNS name will be shown.
To install Docker, first update the system packages:
Create an EC2 instance
2. Install docker
sudo yum install docker -y
3. Start the Docker service
sudo service docker start
Output:
[ec2-user@ip-172–31–27–244 ~]$ sudo service docker start
Redirecting to /bin/systemctl start docker.service
[ec2-user@ip-172–31–27–244 ~]$ date
Fri Feb 24 18:13:02 UTC 2023
4. Check the status of Docker service
Check status:
systemctl status docker
Output:
[ec2-user@ip-172–31–27–244 ~]$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2023–02–24 18:12:56 UTC; 18s ago
Docs: https://docs.docker.com
Process: 3541 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
Process: 3540 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
Main PID: 3544 (dockerd)
Tasks: 7
Memory: 20.8M
CGroup: /system.slice/docker.service
└─3544 /usr/bin/dockerd -H fd:// - containerd=/run/containerd/containerd.sock - default-ulimit nofile=32768:65536
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.307285735Z" level=info msg="ClientConn switching balancer to \"pick_first…dule=grpc
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.345070523Z" level=warning msg="Your kernel does not support cgroup blkio weight"
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.345497426Z" level=warning msg="Your kernel does not support cgroup blkio …t_device"
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.345939221Z" level=info msg="Loading containers: start."
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.528538493Z" level=info msg="Default bridge (docker0) is assigned with an … address"
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.575575709Z" level=info msg="Loading containers: done."
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.597059083Z" level=info msg="Docker daemon" commit=a89b842 graphdriver(s)=…=20.10.17
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.597530250Z" level=info msg="Daemon has completed initialization"
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal systemd[1]: Started Docker Application Container Engine.
Feb 24 18:12:56 ip-172–31–27–244.eu-central-1.compute.internal dockerd[3544]: time="2023–02–24T18:12:56.623407852Z" level=info msg="API listen on /run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
[ec2-user@ip-172–31–27–244 ~]$
6. Before proceeding further we have to check if we are able to execute Docker commands as non-root user. To verify this, let’s execute the docker info command as below:
docker info
Output:
[ec2-user@ip-172–31–27–244 ~]$ docker info
Client:
Context: default
Debug Mode: false
Server:
ERROR: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info": dial unix /var/run/docker.sock: connect: permission denied
errors pretty printing info
As expected, it did not work.
7. So, we now add our user _ec2_user_ to the _Docker_ group.
sudo groupadd docker
_Note:_ if the _Docker_ group does not exist, we can create it using the below command and then add the user to the group:
sudo groupadd docker
For this to take effect, we can either restart our EC2 instance or refresh the group list manually, using the below command:
sudo usermod -aG docker ec2-user
_Note:_ If all goes well, we don’t expect the command to give a terminal output.
8. Execute the `docker info ` command again.
[ec2-user@ip-172–31–27–244 ~]$ docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.17
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
runc version: 5fd4c4d144137e991c4acebb2146ab1483a97925
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 5.10.165–143.735.amzn2.x86_64
Operating System: Amazon Linux 2
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 964.8MiB
Name: ip-172–31–27–244.eu-central-1.compute.internal
ID: VQRN:EH7L:TK32:FZHR:35WC:LTIA:VVZ4:MYZY:SQFZ:J4FI:OF6K:ADB2
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Deploy a container image
1. Let’s create an _index.html_ that will be hosted on our Apache container
<html>
<head>
<title>
Tanushree's Docker test
</title>
</head>
<body>
<b>Hello World! This is my first Docker run!</b>
<body>
</html>
2. Let’s create our first ever **Dockerfile**
This is a simple Dockerfile, that is pulling the httpd:2.4 image from Dockerhub, and copying _index.html_ from our EC2 file system to the apache docs hosting path.
FROM httpd:2.4
COPY index.html /usr/local/apache2/htdocs/index.html
3. Now we build our Docker image based on this _Dockerfile_
docker build -t
-t : defines the : for the docker image. (If no tag is defined, the default tag _latest_ gets assigned)
(.)period : the path where Dockerfile is present (period represents the current directory)
Example:
[ec2-user@ip-172–31–27–244 ~]$ docker build -t apache-docker-example .
Sending build context to Docker daemon 13.31kB
Step 1/2 : FROM httpd:2.4
2.4: Pulling from library/httpd
bb263680fed1: Pull complete
9e8776e4b876: Pull complete
f506d7aab652: Pull complete
05289ee4f284: Pull complete
b7f64f2f8747: Pull complete
Digest: sha256:db2d897cae2ad67b33435c1a5b0d6b6465137661ea7c01a5e95155f0159e1bcf
Status: Downloaded newer image for httpd:2.4
-> 3a4ea134cf8e
Step 2/2 : COPY index.html /usr/local/apache2/htdocs/index.html
-> ff35af99cd38
Successfully built ff35af99cd38
Successfully tagged apache-docker-example:latest
[ec2-user@ip-172–31–27–244 ~]$
4. Verify if the image got created
docker images
This command lists the details of all available Docker images in our local
Example:
[ec2-user@ip-172–31–27–244 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache-docker-example latest ff35af99cd38 29 seconds ago 145MB
httpd 2.4 3a4ea134cf8e 2 weeks ago 145MB
hello-world latest feb5d9fea6a5 17 months ago 13.3kB
centos latest 5d0da3dc9764 17 months ago 231MB
[ec2-user@ip-172–31–27–244 ~]$
5. Run the docker container from the docker image created in step 3
docker run -d - name -p :
-d : to run as a daemon process -p: for port mapping. mapping local port 80, to docker internal port 80
[ec2-user@ip-172–31–27–244 ~]$ docker run -d - name helloworld -p 80:80 apache-docker-example
5c8e630daf7ad4843bccddf27eb44c7c4711ec02e9dfdc77de8bc25bbf75e0b4
[ec2-user@ip-172–31–27–244 ~]$
6. Check running Docker processes
docker ps
Lists the running docker process and associated details.
Example:
[ec2-user@ip-172–31–27–244 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c8e630daf7a apache-docker-example "httpd-foreground" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp, :::80->80/tcp helloworld
[ec2-user@ip-172–31–27–244 ~]$
7. Launch the EC2 public I.P address on port 80 in a web browser to verify if apache was successfully installed and see if our index.html was hosted.
8. To stop a docker process
docker stop <docker-container-name>
Example: docker stop alexis_docker ### Push container image to Docker Registry
i. Create a Dockerhub account on the Docker [website](https://hub.docker.com)
ii. Login to dockerhub from your EC2 instance
docker login
Enter the Docker user name and password created in (i) when prompted. This will store your credentials in /home/ec2-user/.docker/config.json.
_Note:_ it is not a recommended practice to store credentials plainly, rather in a secrets store. (I still need to study up how to configure this with AWS Secrets Manager. )
iii. Tag the docker image we wish to push to Dockerhub against our account
docker tag : /:<tag-you-wish-to-assign
Example:
docker tag apache-docker-example:latest akhlab/apache-docker-example:latest
iv. Push the image to Dockerhub
docker push <dockerhub-username>/<name-you-assigned-in-step-iii>:<tag-you-assigned-in-step-iii>
Example:
docker push akhlab/apache-docker-example:latest
v. Verify the image availability in your DockerHub account