Unit 1 Chapter 3: Creating and Deleting Images and Containers
In this chapter, you will try out the Hello World and BusyBox images.
Topics Covered
- Basic Docker commands
List of Docker Containers
Enter the command docker ps -a
to show all of the Docker containers currently in your system. Of course, there will be none, and the screen output will be the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
List of Docker Images
Enter the command docker images -a
to show all of the Docker images currently in your system. Again, there will be none, and the screen output will be the following:
REPOSITORY TAG IMAGE ID CREATED SIZE
Hello World Image
- Enter the command
docker run hello-world
. - The screen output should be something like this:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
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/
- Because you didn't already have the hello-world Docker image on your machine, Docker downloaded it and created a new Docker container from it. This Docker image is configured to automatically print out that "Hello from Docker" message.
- Enter the command
docker ps -a
to list the Docker containers. You should see something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a4c943829db hello-world "/hello" 40 seconds ago Exited (0) 39 seconds ago quirky_sutherland
- As you can see, you now have one Docker container based on the hello-world Docker image. The last command used in this Docker container is "/hello".
- Enter the command
docker images -a
to list the Docker images. You should see something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
- As you can see, you have the hello-world Docker image. It's extremely small (only 13.3 kB), so there's not much to it.
BusyBox Image
- Enter the command
docker run busybox
. This downloads the busybox Docker image and starts up a container based on it. The screen output should look something like this:
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
76df9210b28c: Pull complete
Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Status: Downloaded newer image for busybox:latest
- Enter the command
docker ps -a
to list the Docker containers. You should see something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c96a3b056e8f busybox "sh" 16 seconds ago Exited (0) 15 seconds ago romantic_greider
6a4c943829db hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago quirky_sutherland
- As you can see, you now have a second Docker container. The newer one is based on the busybox Docker image. The last command used in this Docker container is "sh".
- Enter the command
docker images -a
to list the Docker images. You should see something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 7 days ago 1.22MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
- As you can see, you now have the busybox Docker image as well.
- Enter the command
docker run -it busybox sh
. - Now your terminal has the prompt "/ #", which signifies that any commands you type are executed within the Docker container instead of within your host system.
- Enter the command
pwd
. You'll see that you're in the very top directory, "/". - Enter the command
whoami
. You'll see that you are the root user. - Enter the command
ls -l
. The screen output should be something like this:
total 36
drwxr-xr-x 2 root root 12288 Jun 2 02:37 bin
drwxr-xr-x 5 root root 360 Jun 10 05:19 dev
drwxr-xr-x 1 root root 4096 Jun 10 05:19 etc
drwxr-xr-x 2 nobody nogroup 4096 Jun 2 02:37 home
dr-xr-xr-x 224 root root 0 Jun 10 05:19 proc
drwx------ 1 root root 4096 Jun 10 05:19 root
dr-xr-xr-x 13 root root 0 Jun 10 05:19 sys
drwxrwxrwt 2 root root 4096 Jun 2 02:37 tmp
drwxr-xr-x 3 root root 4096 Jun 2 02:37 usr
drwxr-xr-x 4 root root 4096 Jun 2 02:37 var
- Enter the command "exit" to return to your host OS.
- Enter the command
docker ps -a
to list the Docker containers. Note that there is now a second Docker container based on the busybox image. You should see something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5609ad9dc651 busybox "sh" 41 seconds ago Exited (0) 2 seconds ago nostalgic_spence
c96a3b056e8f busybox "sh" About a minute ago Exited (0) About a minute ago romantic_greider
6a4c943829db hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago quirky_sutherland
Deleting Docker Containers
- Note that each Docker container has a container ID number, which is listed in the CONTAINER_ID column.
- For each Docker container, enter the command "docker rm (container ID)". In this example, you'd enter the following commands:
docker rm 5609ad9dc651
docker rm c96a3b056e8f
docker rm 6a4c943829db
- Enter the command
docker ps -a
. You should see that there are no Docker containers. The screen output should be:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- Enter the command
docker images -a
. You'll see that both Docker images are still in place. The screen output should be something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 7 days ago 1.22MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
- Enter the command
docker run busybox
. There should be no screen output this time, because the busybox Docker image is still stored locally. Thus, there is no need to download it. - Enter the command
docker ps -a
. The screen output should be something like:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8a04c9893c7 busybox "sh" 4 seconds ago Exited (0) 4 seconds ago cranky_poitras
- Remove the Docker container. For the above example, the command is
docker rm f8a04c9893c7
. - Enter the command
docker ps -a
. The list of Docker containers should be empty.
Deleting Docker images.
- Enter the command
docker images -a
. The output should still be something like:
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 1c35c4412082 7 days ago 1.22MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
- For each Docker image, enter the command "docker rmi (image ID)". In this example, you'd enter the following commands:
docker rmi 1c35c4412082
docker rmi bf756fb1ae65
- Enter the command
docker images -a
. The list of Docker images should be empty, and the screen output should be:
REPOSITORY TAG IMAGE ID CREATED SIZE
Deleting Docker Networks
- I've had times when I had an excessive number of Docker networks, which led to error messages when I tried to start a new Docker container. So it's a good idea to remove excess networks sometimes.
- To delete all Docker networks, enter the command
docker network prune -f
.