Docker is an open source platform which can be used to run/test applications in an isolated environment on the host system. Docker could be defined as an implementation of container using the concept of images.
Unlike VM which is a complete OS intalled on host system managed by hypervisors and needs dedicated resources from the host machine, docker uses Linux Containers which directly interacts with the linux kernel like any other process on linux to request resources.
Install apache inside a docker container on ec2 instance running Ubuntu (ami-ca381398) and access it using the hosts public IP.
Installing Docker: Run the following commands on terminal to install the docker.
sudo apt-get update sudo apt-get install docker.io source /etc/bash_completion.d/docker.io #to enable tab-completion of Docker commands in BASH
Run following command to verify the successful installation of docker
sudo docker info
We would see the following output on terminal which would list the version of docker installed on the system.
Client version: 1.0.1 Client API version: 1.12 Go version (client): go1.2.1 Git commit (client): 990021a Server version: 1.0.1 Server API version: 1.12 Go version (server): go1.2.1 Git commit (server): 990021a
Fowarding Traffic: We need to foward all traffic to docker which we be required as we are running apache web server inside docker container.Enable forwarding with UFW(Uncomplicated Firewall):
sudo vi /etc/default/ufw
Replace “DEFAULT_FORWARD_POLICY=”DROP”” with “DEFAULT_FORWARD_POLICY=”ACCEPT” and save the file.
Search Docker Images: We would need to download Ubuntu image on host Ubuntu system on which we would install Apache web server. Varoius customize docker images are available on Docker repository which could be searched using the following command. This command would search and list all images having Ubuntu string in their name.
sudo docker search ubuntu
Download Ubuntu Image: This command would download Ubuntu version 14.04 image.
sudo docker pull ubuntu:14.04
We can get list of all available images on our local system using:
sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu 14.04 2d24f826cb16 8 days ago 192.7 MB
In the above output, we can see the image id (2d24f826cb16) which is unique for every image. Just to compare, the size of this image is 192.7MB which is very small than than it’s VM image.
Now ,we have Ubuntu image which we would use to run container inside it and install Apache inside this container.
Example: We would take a simple example to run an application inside container which would print “hello”.
sudo docker run 2d24f826cb16 echo "hello"
docker run: run applications inside containers.
2d24f826cb16: The image which we want to use to run container inside. We have used image id of the downloaded Ubuntu. We can either use image id or image name i.e. ubuntu:14.04.
echo “hello”: the command we want to run inside the container.
Docker container would be active until the command we ran is active. We can check the status of the exited/running containers using:
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6cd7aacb64fb ubuntu:14.04 echo hello 9 minutes ago Exited (0) 1 minutes ago backstabbing_sinoussi
The above output says container with id 6cd7aacb64fb has been exited a minute ago. Like images, containers do have unique container id and name (backstabbing_sinoussi in this case). These name are automatically generated by docker. We can provide custom name using “–name” option n the docker run command.
In our case we need a container where we can install Apache web server and configure it the way we want. To achieve this we need an interactive container.
sudo docker run -t -i ubuntu:14.04 /bin/bash
This command would get us an interactive container.
-t : assigns a terminal inside the container.
-i : create a interactive connection with container by grabbing STDIN.
/bin/bash : this command would launch bash shell inside container.
Now we are inside container. Type exit to come out of the container.
Before moving ahead, we would need the 80 port of host machine to be mapped with the 80 port of our container since we would be using host’s public ip and 80 port to take in requests and host would forward these requests to the Apache web server running at port 80 of container.
This commands would create container name “WebServer” and map the ports.
sudo docker run --name WebServer -p 80:80 -t -i ubuntu:14.04 /bin/bash
-p: maps the host’s 80 port with 80 port of container
Now we are inside the container. We would need to install Apache inside it and start it.
apt-get update apt-get install apache2 service apache2 start
Now try to access the webserver using the public ip of host system in web browser.
Please verify 80 port is open on the security group of the ec2 instance of host system.
Now, the web server should work fine.
Press Ctrl+p and Ctrl+q to come out of the container. This would keep the container running behind. We can verify it using
sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8931afa5aaa6 ubuntu:14.04 /bin/bash 9 minutes ago Up 9 minutes 0.0.0.0:80 80/tcp WebServer
The status WebServer container is “Up” and is running from last 9 minutes.
If we again need to make some changes inside this container, we need to attach to this container using docker attach command.
sudo docker attach WebServer
We can save this “Webserver” Container as image using docker commit command.
sudo docker commit -m "Apache web server" -a "Navjot Singh" 8931afa5aaa6 ubuntu:Apache
-m : for description of image
-a : Name of the Author
8931afa5aaa6 : container’s ip whose image we are creating.
ubuntu:Apache : specified the image name as “ubuntu” and tag as “Apache”
We can see this image using:
sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu Apache dcc12acbb267 14 seconds ago 227.6 MB ubuntu 14.04 2d24f826cb16 8 days ago 192.7 MB
We would continue with some more scenarios in the next blog which would cover the creating image using dockerfile and allowing communication between two docker containers.
Check out our blog Amazon EC2 Container Service (ECS) – Docker Container Management