21 Days of Docker-Day 2 — First Docker Container

Docker Images

  • To understand docker images, let’s try to run the below command
docker image pull alpine
$ docker image pull alpine
Using default tag: latest
latest: Pulling from library/alpine
9d48c3bd43c5: Pull complete 
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

NOTE: Alpine is a lightweight Linux distribution, so it’s quick to pull down and run, making it a popular starting point for many other images.

  • Similar to the hello world example, the docker daemon tried to find an image named alpine locally and when it’s not able to locate image locally fetches it from DockerHub.
  • To list the downloaded images on your system
$ docker image ls
alpine                                                                                  latest              961769676411        7 weeks ago         5.58MB
hello-world                                                                             latest              e38bc07ac18e        18 months ago       1.85kB
  • With this downloaded alpine image, let’s run a docker container and to do that run the below command
docker container run apline ls -l
$ docker container run alpine ls -l
total 56
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 bin
drwxr-xr-x    5 root     root           340 Oct  8 22:06 dev
drwxr-xr-x    1 root     root          4096 Oct  8 22:06 etc
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 home
drwxr-xr-x    5 root     root          4096 Aug 20 10:30 lib
drwxr-xr-x    5 root     root          4096 Aug 20 10:30 media
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 mnt
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 opt
dr-xr-xr-x  264 root     root             0 Oct  8 22:06 proc
drwx------    2 root     root          4096 Aug 20 10:30 root
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 run
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 sbin
drwxr-xr-x    2 root     root          4096 Aug 20 10:30 srv
dr-xr-xr-x   13 root     root             0 Oct  8 22:06 sys
drwxrwxrwt    2 root     root          4096 Aug 20 10:30 tmp
drwxr-xr-x    7 root     root          4096 Aug 20 10:30 usr
drwxr-xr-x   11 root     root          4096 Aug 20 10:30 var

Let’s try to figure out what happens behind the scene

  • When we execute the run command, in this case, the docker client finds the image(alpine) and creates the container and then runs a command(ls -l) in that container.
  • So docker executes the command inside the container, for which you saw the directory listing.
  • After the command finished, the container shutdown and exited.

One of the Popular newbie questions is what is the difference between Docker and Virtual Machine?

Let’s take the above example, If I need to perform the same steps in case of VM, I need to boot the whole VM(which takes 2+ min) as it’s the emulated hardware stack before running the command. Docker container function at the application layer so they skip most of the steps VM requires and just run what required for the app and that is the reason why containers are fast!.

To check all the running container in Docker, run the below command

$ docker container ls
 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
  • To check all the container (running and stopped)
docker container ls -a
$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS                    NAMES
a16405dd5bd8        alpine              "ls -l"                  About a minute ago   Exited (0) 59 seconds ago                            trusting_buck
2276a5e2a207        hello-world         "/hello"                 6 minutes ago        Exited (0) 6 minutes ago                             hardcore_brown
5d5a221cb3ec        b7b28af77ffe        "sh"                     11 minutes ago       Up 11 minutes                                        myalpine1
206c05f0398a        b7b28af77ffe        "sh"                     16 minutes ago       Exited (0) 16 minutes ago                            myalpine

NOTE: STATUS column shows that these containers exited after some time ago.

It’s time to do something exciting, let’s run our first interactive container, by executing the below command

$ docker container run -it alpine /bin/sh
 / # 
  • Whoo, something happens, I am now inside the Linux shell 
  • Docker has a facility to run the container in an interactive terminal.
-i, --interactive                    Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
  • As I am inside Linux shell, I can run/test some of the Linux command
/ # uname -a
Linux adb11d821ff7 4.4.0-164-generic #192-Ubuntu SMP Fri Sep 13 12:02:50 UTC 2019 x86_64 Linux

NOTE: Alpine is a small Linux OS so several commands might be missing.

Fun Fact if you notice the funny name assigned when you boot these containers eg: silly_banzai if you ever wonder from where docker is getting these funny names

$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5ea51975c87 alpine "bash" 7 seconds ago Created
silly_banzai

https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go

  • But you can always specify some meaningful name to your container, using — name flag
$ docker run --name myalpine -it alpine sh
  • If you run docker container ls -a command again
$ docker container ls -al
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
bf42d1cfc5a5        alpine              "sh"                10 seconds ago      Exited (0) 5 seconds ago                       myalpine
  • Now whatever command we have executed so far, is good for testing purpose and to get our hand’s dirty, one of the major issues is, the moment we exit out of the container, by running exit command or CTRL + D key, it’s going to shut down the container
$ docker run --name myalpine -it alpine sh
/ # exit
  • We can verify that docker container ps -l
$ docker container ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
206c05f0398a        alpine              "sh"                About a minute ago   Exited (0) About a minute ago                       myalpine
  • Now we need to do something so that our container will not exit/shutdown and it will run in the background
  • TADA, here comes the -d flag, which is going to run a container in detach/background mode
-d, --detach                         Run container in background and print container ID
$ docker run --name myalpine1 -dt alpine sh
5d5a221cb3ecba64793e95fa46bc2f09816f6b7f4ce92b0c9108c22be10ae37a
  • To verify it
$ docker container ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
5d5a221cb3ec        alpine              "sh"                     4 seconds ago       Up 3 seconds                                 myalpine1
  • We have explored enough command today, let continue our journey in Day 3 with more Docker commands and concepts.

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/

12 Replies to “21 Days of Docker-Day 2 — First Docker Container”

  1. great post, very informative. I ponder why the opposite experts of this sector do not notice this. You should proceed your writing. I’m confident, you have a great readers’ base already!

      1. Hi

        I have added this functionality, If you look at the right side of the blog, you will see something like this “Subscribe to 100 Days of DevOps by Email”. Please let me know if there is any other issue.

        Thanks
        Prashant

  2. This design is incredible! You certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

  3. I like the helpful info you provide in your articles. I will bookmark your blog and check again here frequently. I’m quite certain I抣l learn plenty of new stuff right here! Good luck for the next!

  4. It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this blog with my Facebook group. Chat soon!

  5. You can certainly see your expertise in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always go after your heart.

Leave a Reply

Your email address will not be published.