All-Things-Docker-and-Kubernetes

Lab 001: Running Simple Containers

Pre-requisites

Introduction

In this lab, we’ll run some simple containers.

whalesay

Run the command below. It will pull the image from Dockerhub and run it locally.

$ sudo docker run docker/whalesay cowsay Infinity and beyond!

You should see and output like this.

< Infinity and beyond! >
 ----------------------
    \
     \
      \
                    ##        .
              ## ## ##       ==
           ## ## ## ##      ===
       /""""""""""""""""___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
       \______ o          __/
        \    \        __/
          \____\______/

Now make the whale say “Let’s do this!”

$ sudo docker run docker/whalesay cowsay "Let's do this!"
 ________________
< Let's do this! >
 ----------------
    \
     \
      \
                    ##        .
              ## ## ##       ==
           ## ## ## ##      ===
       /""""""""""""""""___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
       \______ o          __/
        \    \        __/
          \____\______/ 

nyancat

Run the command below. It will pull the image from Dockerhub and run it locally.

$ sudo docker run -it --rm --name nyancat 06kellyjac/nyancat

To exit out of the animation, hit Ctrl-C.

Running a Web server

This runs the nginx web server in a container using the official nginx image, specifically the version 1.12.

$ docker run --name web-server -d -p 8080:80 nginx:1.12 

Here we assigned a container name “web-server” to the container and we also mapped the host’s port 8080 to the container port 80(http).

Notice the “-d” which means detached. The container runs in the background and you can simply “attach” to it using the docker attach command.

Verify by running a cURL to out localhost through port 8080. It should return a “200 OK” status.

# curl localhost:8080 -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 23 Jun 2022 09:46:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:29:18 GMT
Connection: keep-alive
ETag: "5964d2ae-264"
Accept-Ranges: bytes 

To stop the container,

$ docker stop web-server 

To start the container again,

$ docker start web-server 

Interactive Ubuntu container

We’ve launch one-off containers so far, which means once the process is done or when we exit out, the container is then killed. In this one, we’ll be able to “interact” with the actual container by running commands inside it.

$ sudo docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
405f018f9d1d: Pull complete
Status: Downloaded newer image for ubuntu:latest
root@938c4f054e92:/#
root@938c4f054e92:/#

Note that each container is independent from each other. To see this, let’s create a file in this Ubuntu container and then exit out. Remember that when you exit out of a container, the container is sort of “deactivated”.

root@938c4f054e92:/# echo "This file exists in Ubuntu container 1" > container1.txt 
root@938c4f054e92:/# cat container1.txt
This file exists in Ubuntu container 1
root@938c4f054e92:/# exit
exit

Now let’s run another Ubuntu container and check if the file exists here. We’ll see that this container doesn’t have the file.

$ sudo docker run -it ubuntu /bin/bash
root@40485b5df3ce:/# cat container.txt
cat: container.txt: No such file or directory 

Let’s restart the first Ubuntu container. Get the list of containers first.

$ docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED              STATUS                         PORTS     NAMES
40485b5df3ce   ubuntu            "/bin/bash"              About a minute ago   Exited (1) 4 seconds ago                 modest_galois
938c4f054e92   ubuntu            "/bin/bash"              4 minutes ago        Exited (0) 3 minutes ago                 nostalgic_dhawan

Restart the container and verify that it is running by issuing the docker ps commmand.

$ docker start 938c4f054e92
938c4f054e92 
$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
938c4f054e92   ubuntu    "/bin/bash"   7 minutes ago   Up 5 seconds             nostalgic_dhawan 

To interact with this container, we need to “attach”. Once attached, check the container1.txt file.

$ docker attach 938c4f054e92
root@938c4f054e92:/# 
root@938c4f054e92:/# 
root@938c4f054e92:/# cat container1.txt
This file exists in Ubuntu container 1

Executing Commands inside a Running Container

For this example, let’s run a simple Redis container.

$ docker run -d redis 
$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS      NAMES
dc9f84c643f9   redis     "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds   6379/tcp   sweet_brown 

To run “enter” the container and run the Redis CLi, use the exec it command, followed by the container-id and the command “redis-cli”. You should be able to run the Redis CLI now.

$ docker exec -it dc9 redis-cli 

Verify that the CLI works by setting a “testvalue” and retrieving it.

127.0.0.1:6379> set testvalue 100
OK
127.0.0.1:6379> get testvalue
"100" 

Removing Containers

To delete specific containers, use the rm command.

$ docker rm <container-id>

Cleanup

When you’re done with the lab, you can stop all running containers by running the command below.

$ docker stop $(docker ps) 

Once all containers have “Exited” status, remove them.

$ docker ps  -a 
$ docker container prune -f 

Finally, remove all images.

$ docker image prune -af