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

Scenario1:

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

Scenario2:

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
a05ce15d3e1d068dd69498f8c28598681b6e133a407d51083f820f6d5c7d0a16

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        0.0.0.0:80->80/tcp   mynginx
  • Some basic testing
$ curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<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>
</body>
</html>

Scenario2

  • 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
789f45a23f9fcffcdeff088c144b0bfca06a3e6b5e51278c70b69239e46c6f7e

-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)
6566f44a6397
c84b31290053

Start Container

$ docker container start <container id>
$ docker container start 6566f44a6397
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

https://docs.docker.com/engine/reference/commandline/container/

https://stackoverflow.com/questions/51247609/what-is-the-difference-between-docker-run-and-docker-container-run

https://forums.docker.com/t/docker-run-and-docker-container-run/30526

https://www.docker.com/blog/whats-new-in-docker-1-13/

https://github.com/moby/moby/pull/26025

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.

Slack:

https://100daysofdevops.slack.com/join/shared_invite/enQtNzg1MjUzMzQzMzgxLWM4Yjk0ZWJiMjY4ZWE3ODBjZjgyYTllZmUxNzFkNTgxZjQ4NDlmZjkzODAwNDczOTYwOTM2MzlhZDNkM2FkMDA

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.