21 Days of Docker-Day 3 - Building Container Continue

Up to this point, we understand, how to run the container and how to login to that container, let talk about the real world scenario where a developer is developing its code eg: in HTML and he wants to test this code using docker container. In this case, we need to test two scenario


He can access the newly developed website from his desktop or from anywhere in the network


He should be able to see his changes without login into the docker container

$ docker container run --name mynginx -dt -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
b8f262c62ec6: Pull complete
e9218e8f93b1: Pull complete
7acba7289aa3: Pull complete
Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
Status: Downloaded newer image for nginx:latest

Port Binding

  • By default, Docker Container can make connections to the outside world but the outside world cannot connect to containers.
  • If we want containers to accept an incoming connection from the world, we have to bind it to a host port
  • To do that we need to use -p flag
  • What this will do, any request coming to Port 80 of the host will redirect to the port of the container.
  • To verify the container is running
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
a05ce15d3e1d        nginx               "nginx -g 'daemon of…"   6 seconds ago       Up 4 seconds>80/tcp   mynginx
  • Some basic testing
$ curl localhost:80
<!DOCTYPE html>
<title>Welcome to nginx!</title>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>


  • Create a directory and a test file
mkdir /home/cloud_user/html_test
echo "this is coming from my local workspace" >> /home/cloud_user/html_test/index.html

NOTE: Please replace cloud_user with your own user.

  • Let’s execute the below command
$ docker container run --name mynginxnew -v /home/cloud_user/html_test:/usr/share/nginx/html -dt -p 80:80 nginx

-v or --volume: Consists of three fields, separated by colon characters (:). The fields must be in the correct order, and the meaning of each field is not immediately obvious.

  • In the case of bind mounts, the first field is the path to the file or directory on the host machine i.e the directory /home/cloud_user/html_test we created in the first step
  • The second field is the path where the file or directory is mounted in the container, in this case /usr/share/nginx/html which is the default www directory for nginx
  • The third field is optional, and is a comma-separated list of options, such as ro, consistent, delegated, cached, z, and Z

NOTE: Please make sure that port 80 is available in your container.

  • It’s time to perform some testing, Woot! it looks good
$ curl localhost
this is coming from my local workspace

If you still have some room to digest, let’s learn some more Docker command

Stop Container

  • To stop the docker container
$ docker container stop <container id>
$ docker container stop 6566f44a6397
  • To stop multiple container
$ docker container stop $(docker container ls -aq)

Start Container

$ docker container start <container id>
$ docker container start 6566f44a6397

Restart Container

$ docker container restart <container id>

Confusion: There is one common question, which is mostly asked in the Docker Community, what is the difference between docker run vs docker container run?

CLI restructured

Docker has grown many features over the past couple of years and the Docker CLI now has a lot of commands (40 at the time of writing). Some, like build or run are used a lot, some are more obscure, like pause or history. The many top-level commands clutter help pages and make tab-completion harder.

In Docker 1.13, Docker regrouped every command to sit under the logical object it’s interacting with. For example list and startof containers are now subcommands of docker container and history is a subcommand of docker image.

docker container list
docker container start
docker image history

These changes let us clean up the Docker CLI syntax, improve help text and make Docker simpler to use. The old command syntax is still supported, but we encourage everybody to adopt the new syntax.

For more info





I believe this enough for today, tomorrow let’s continue to dig more inside the Docker container.

Please follow me with my Journey

This time to make learning more interactive, I am adding

  • Slack
  • Meetup

Please feel free to join this group.



Meetup Group

If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/

8 Replies to “21 Days of Docker-Day 3 - Building Container Continue”

  1. *There are some interesting points in time in this article but I don?t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

  2. *Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I?d prefer to use some with the content on my blog whether you don?t mind. Natually I?ll give you a link on your web blog. Thanks for sharing.

Comments are closed.