Manage Container Lifecycle with Docker

  1. Now that we have installed and verified Docker, let’s create our first container. This command pulls the hello-world image from Docker Hub and runs it as a container.
student@ubuntu:~$ sudo docker container run hello-world

Output:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest:
sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent
it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
  1. List the container running on our host, by executing the following command:
student@ubuntu:~$ sudo docker container ls

Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Note: No container is listed, that is because “docker container ls” command
only lists running container by default.
  1. List the containers running on our host again, by specifying a different option:
student@ubuntu:~$ sudo docker container ls -a

Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
212315cf9ce9 hello-world "/hello" 4 minutes ago Exited (0) 4 minutes
ago epic_feynman
  1. In order to know all the options and get help from Docker, we can execute the following command:
student@ubuntu:~$ sudo docker container ls --help

Output:

Usage: docker container ls [OPTIONS]
List containers
Aliases:
docker container ls, docker container list, docker container ps, docker ps
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format
with column headers (default)
'table TEMPLATE': Print output in table format
using the given Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go
template.
Refer to https://docs.docker.com/go/formatting/ for
more information about formatting output with templates
-n, --last int Show n last created containers (includes all states)
(default -1)
-l, --latest Show the latest created container (includes all
states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
  1. Interact with a running container. This command starts up an ubuntu container and opens an interactive bash shell in the foreground:
student@ubuntu:~$ sudo docker container run -it ubuntu /bin/bash

Output:

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
a48641193673: Pull complete
Digest:
sha256:6042500cf4b44023ea1894effe7890666b0c5c7871ed83a97c36c76ae560bb9b
Status: Downloaded newer image for ubuntu:latest
root@8bbf327d2b32:/#
Note: Press exit to close the container shell.
  1. Start an ubuntu container, but this time we will send the container to the background:
student@ubuntu:~$ sudo docker container run -dit ubuntu /bin/bash

Output:

c59dd04833466ee4aee856b574d918c7a3368b87b9ac7ca70b4ca9800555a33c
  1. Now that the container is running in the background, we can interact with it by attaching to the container.
student@ubuntu:~$ sudo docker container attach c59dd04833466e
root@c59dd0483346:/#
Note: This time we will press ctrl+p,ctrl+q to send the container to
background.
root@c59dd0483346:/# read escape sequence
student@ubuntu:~$
  1. Let’s start an nginx web server and expose a port on the host to access the application. We will also make use of additional options like –name to name our container:
student@ubuntu:~$ sudo docker container run -dit --name webserver -p 8080:80
nginx

Output:

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
af107e978371: Pull complete
336ba1f05c3e: Pull complete
8c37d2ff6efa: Pull complete
51d6357098de: Pull complete
782f1ecce57d: Pull complete
5e99d351b073: Pull complete
7b73345df136: Pull complete
Digest:
sha256:2bdc49f2f8ae8d8dc50ed00f2ee56d00385c6f8bc8a8b320d0a294d9e3b49026
Status: Downloaded newer image for nginx:latest
51e39209944cd417424eeff133b00d0ca7dae4e44950300ca7929211fc04ca05
  1. We have exposed the container on port 8080 of the localhost; let’s access the application.
student@ubuntu:~$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
  1. Create a custom html page and copy it to our container.
student@ubuntu:~$ cat > index.html << EOF
> Welcome to Docker Training!!
> EOF
student@ubuntu:~$ sudo docker container cp index.html
webserver:/usr/share/nginx/html/index.html

Output:

Successfully copied 2.05kB to webserver:/usr/share/nginx/html/index.html
  1. Access the application; we should now see the default page changed by our copy command.
student@ubuntu:~$ curl localhost:8080

Output:

Welcome to Docker Training!!
  1. Stop and remove the container.
student@ubuntu:~$ sudo docker container stop webserver

Output:

webserver
  1. Remove all the stopped and completed containers.
student@ubuntu:~$ sudo docker container prune

Output:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
51e39209944cd417424eeff133b00d0ca7dae4e44950300ca7929211fc04ca05
8bbf327d2b32fdc73ff038ff38a7b6df4debcb59226f8e4b35d26b78a4668b99
212315cf9ce9638786eb60ed801b48f0f43e83a0d32fd5cc222b376154ec8585
Total reclaimed space: 1.127kB
  1. There are additional container run options; explore the sudo docker container –help command and try additional options.