DevOps Training
  • DevOps
  • What is DevOps
    • What DevOps Look like
    • Why DevOps Needed
    • DevOps Automation Tools
    • DevOps Principles
  • cloud computing
    • How DevOps links with Cloud Computing
    • What is cloud computing?
      • Platform as a service (PaaS)
      • Infrastructure as a service (IaaS)
      • Software as a service (SaaS)
      • Function as a Service
      • SaaS, PaaS, IaaS
  • Version Control
    • Git as Version Control
      • Setting up Remote Repo
      • Git Hooks
      • github vs gitlab vs bitbucket
      • Quick Recap Git
  • DevOps #01 Continuous Integration
    • Continuous Integration & Continuous Delivery
      • Understanding CI Tools
      • Prerequisite
      • Continuous Integration
      • CI Tools
      • Travis-CI
        • Travis CI with S3
        • Static Site Build S3
        • Beanstalk with AWS CLI
          • Elastic Beanstalk with Travis CI
        • Travis using Code Deploy EC2
          • Github and Code Deploy
          • Travis CI with Code Deploy
      • Gitlab-CI
        • CI Setup for application
        • Gitlab Runners on EC2
        • CI Integration with AWS
          • Deploying App using Gitlab CI
          • Gitlab CI with AWS S3
          • Gitlab CI with ECS
          • CI Integration with EC2
            • Update and Clean Gitlab.yml
        • Install Gitlab on EC2
      • CI/CD using Jenkins CI
        • Jenkins Build on EC2
        • Jenkins Build EC2 Ubuntu
        • Jenkins CI/CD
          • Create a Build Item
          • Create a Build Pipleine
            • Pipeline Using Docker
            • Pipeline Examples
          • Jenkins CI with S3
            • Jenkins CI - S3
          • Jenkins CI with EC2
    • Jenkins CI Cluster Mode
    • AWS Code Pipeline CI/CD
      • AWS CI/CD Tools
        • AWS Code Build
        • AWS Code Deploy to Beanstalk
        • AWS Code Deploy to EC2
        • AWS Pipeline - Example CI/CD
  • Docker
    • Docker
      • Docker for Developers
        • Install and setup
        • Docker Commands
        • Docker Images Container
        • Docker Architecture
    • Docker Demos
      • Node JS Container
    • Docker-compose
      • Using Docker Compose
      • Docker Compose Demo
  • AWS Quick Refresh
    • AWS Quick Recap - Videos
    • AWS Quick Recap
  • AWS Architecture - Lab
    • Application Deployment - 01
    • Application Deployment - 02
    • Application 3 tier Architecture
  • Basic Networking
    • Computer Networking for Beginners
      • Basic of Networking
      • Networking Protocols
      • OSI Model
      • Network address and Host address
      • Subnetting Type
    • Network Architecture
    • Networking Layers OSI Model
    • Internet protocol
      • CIDR and subnetting
        • Examples
      • AWS VPC Subnets
  • VPC and Networking
    • AWS VPC
    • VPC Demo
      • Bastion Host | Jump Server
  • AWS Components
    • AWS Components In Depth
      • AWS Storage
        • AWS EBS
        • AWS Cloudfront
        • AWS S3
      • AWS Compute
        • ECS
        • AWS VPC
          • VPC Components
        • AWS EC2
        • AWS ELB
          • Application Load balancer
            • Example
        • AWS EC2 Auto Scaling
          • Demo
        • AWS Route 53
        • AWS Lambda Serverless
          • AWS Lambda Serverless Computing
  • Assignments
    • Assignment 01-Node JS app on EC2
    • Assignment 02-Node JS with Mysql
    • Assignment-03
  • Microservices
    • Microservices Architecture
      • Docker and Docker-Compose
      • Docker-Compose Example 01
      • Docker-Compose Example 02
      • Hand-on | Building Microservices
    • Architecture Components
  • AWS ECS
    • AWS ECS
      • Introduction of ECS
Powered by GitBook
On this page
  • Running Apache Web Server
  • Detour: Stop the Container
  • What is that PORTS columm when you do a docker ps?
  • Accessing our web site
  • Show me the Proof?
  • Random Port Mapping
  • Specific Port Mapping

Was this helpful?

  1. Docker
  2. Docker
  3. Docker for Developers

Docker Images Container

PreviousDocker CommandsNextDocker Architecture

Last updated 5 years ago

Was this helpful?

In this section, we shall get acquainted with various other Docker commands and parameters that will give you a good grasp of how to deal with Images, running containers, container networking to some extent and more.

To learn that, it is best to dive deep into running one of the popular servers of all time , the Apache Web Server and working with its Dockerized Image from the Docker Hub.

Running Apache Web Server

Now, let’s try another experiment to help you understand how you will eventually envision your applications running inside Docker.

The title of this section indicates that we want to run the Apache Web Server. To do that, you should now start thinking that there must be a Docker image that someone must have created for Apache Web Server.

So, the first step is to do the following:

$ docker search httpd

This gives the following output:

We will go with the OFFICIAL image for httpd. So let’s learn a new command i.e. docker pull

This command only pulls (downloads) the Docker image to your local repository and does not run it.

Give the following command at the boot2Docker prompt:

$ docker pull httpd

Be Patient. This will download the httpd image. If you run a docker images command, you should see http listed in it.

To make the default Apache Web Server run in the container, all we need to do is the following (Note that we are using the -d parameter i.e. running the Container in Detached mode). We have also given a name to our container via the — name parameter.

$ docker run -d --name MyWebServer httpd
54c39730ab784a998c3bb4522cf6421a4a0c7d46db54afa75202e76741032216

This will launch the default Apache HTTP Server based on the httpd image and since we have launched the container in detached mode, we got back a Container ID. Your ID need to be similar to the one that I got “54c39730ab784a998c3bb4522cf6421a4a0c7d46db54afa75202e76741032216”

Now, let us check if the Container is running. To do that we will use our familiar docker ps command as shown below:

$ docker ps

Note that it is in running status. Also notice that the NAMES column now has the name that we specified for our Container i.e. MyWebServer.

Detour: Stop the Container

If you wish to stop the Container at any point in time, you can now use the handy name that you provided instead of the CONTAINER _ID value in the docker stop command.

Try it now:

  1. To stop the Container, give the following command:

$ docker stop MyWebServer

2. Try the :

$ docker ps command.

3. I also suggest to remove the Container via the following command:

docker rm MyWebServer

Great! Now, let us start it back again.

$ docker run -d --name MyWebServer httpd<SOME_LARGE_CONTAINER_ID>

What is that PORTS columm when you do a docker ps?

Go right to the end and you will find 2 entries in the file, which I am reproducing here:

EXPOSE 80
CMD httpd-foreground

You can see that port 80 is exposed. All looks good for the moment.

Accessing our web site

Now, the next check you would do is use a utility like curl or even the browser on your local machine to check out the site.

But what would the IP address be?

If you are running the Docker for Mac or Docker for Windows, it should be the localhost. If you are running in a VirtualBox environment, find that out.

With that information in hand, let us launch the local browser and open the localhost URL.

Oops! No page appears. We were expecting at least the Apache Home page to appear over here but that does not seem to be the case.

This is because the default port that Apache Web Server runs on is port 80 but that port has not been exposed on the host side and as a result of that the web site is not accessible.

Show me the Proof?

Well I mentioned to you that port 80 is exposed by the Container but not by the Host. In other words, port 80 is the private port exposed by the Container but there does not seem to be a port mapped for the public facing host because of which we are seeing the problem.

It seems there is a docker command for that too. And its intuitively called port.

Try out the following command :

$ docker port MyWebServer
$

And the output is as expected i.e. there is no mapping done for the public facing port of the Host.

Luckily, help is on the way !

Random Port Mapping

First up, stop and remove the Container so that we can use the same Container Name i.e. MyWebServer.

$ docker stop MyWebServer
MyWebServer$ docker rm MyWebServer
MyWebServer

Now, let us start the httpd Container with an extra parameter i.e. -P. What this parameter does is that it “Publish all exposed ports to random ports”. So in our case, the port 80 should get mapped to a random port, which is going to be the public port.

Execute the following command:

docker@boot2docker:~$ docker run -d --name MyWebServer -P httpd60debd0d57bf292b0c3f006e4e52360feaa575e45ae3caea97637bb26b490b10

Next, let us use the port command again to see what has happened:

$ docker port MyWebServer80/tcp -> 0.0.0.0:32769

We can see that port 80 has got mapped to port 32769. So if we access our web site at

http://<HostIP>/<HostPort>

it should work now.

Specific Port Mapping

So what if we wanted to map it to a port number other than 32769. You can do that via the -p (note the lowercase) parameter.

This parameter format is as follows:

-p HostPort:ContainerPortFor e.g. -p 80:80 or -p 8080:80

The first parameter is the Host port and we are now making that 80. The second port is what Apache httpd exposes i.e. 80.

Let us check out everything again:

$ docker stop MyWebServer
MyWebServer$ docker rm MyWebServer
MyWebServer$ docker run -d --name MyWebServer -p 80:80 httpd
02ce77550940cb1f37361b74af8913e46d6a507d06c2579b8a8b49e389b1e75f

Now, let us give the port command again:

$ docker port MyWebServer80/tcp -> 0.0.0.0:80

The for the httpd Docker image is something that you should look at. In fact make it a habit to go through the documentation page for each of the images present since it will give you clear instructions on how to run it and any other configuration details.

You will notice that there is an entry in the PORTS column. The value for that is “tcp:80”. This means that port 80 is being exposed by the Container. This looks reasonable since Apache Web Server default port for HTTP is 80. If you are adventurous enough, you can take a look at the Dockerfile (don’t worry about what a Dockerfile is) for this project is .

So on my machine it is and it works!

image03
official documentation
here
http://192.168.59.103:32769