• 8 hours
  • Medium

Free online content available in this course.

course.header.alt.is_certifying

Got it!

Last updated on 11/10/22

Run Your First Local Container

In Part Two, it’s time to get to grips with Docker. We’ll start by looking at the command-line interface, which we use with the Docker daemon we installed earlier.

Understand Docker Hub

Before starting your first Docker container, think back to when you created your account on Docker Hub to download your version of Docker. This is also Docker’s official registry.

What is a registry?

Start Your First Docker Container

To start your first container, you need to use the  docker run hello-world  command.

➜ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
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/

When you use this command, the Docker daemon looks to see whether the image  hello-world  is available locally. If not, it will go and get it from the official Docker registry.

Run a Nginx Server With a Docker Container

You now know how to run a container, and you’ve understood the actions carried out by the Docker daemon when using the  docker run  command.

Now let’s take that a bit further. We’re going to run a container that starts a Nginx server using two options:  docker run -d -p 8080:80 nginx.

In this command, we’ve used two options:

  • -d  to detach the container from the console’s main process. This allows you to carry on using the console while your container works on another process.

  • -p  to define the use of ports. In our example, we’ve asked it to transfer the traffic from port 8080 to port 80 of the container. So, if you head to the addresshttp://127.0.0.1:8080, you’ll see the default Nginx page.

You may also need to “get into” your Docker container to carry out actions there. To do this, you’ll use the  docker exec -it <container name> bash  command. In this command, the  -it  argument gives you a fully operational bash shell. Once you’re in your container, you can use the  cd /usr/share/nginx/html  command to go into the directory that contains the  index.html  file to modify its content and see the result live athttp://127.0.0.1:8080.

Stop Your Docker Container

You’ve created a container with the  --detach  option, so at some point you’re going to have to stop it! To do this, we’ll use the  docker stop <container name>  command.

Now that your Docker container has been stopped, you can delete it with the  docker rm <container name>  command. This will destroy the container and its contents, however you can always recreate your container with the  docker run  command that we saw earlier.

Get an Image From Docker Hub

You may also need to get images from Docker Hub without actually running your container. To do this, you’ll need to run the following command:

➜ docker run hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Image is up to date for hello-world:latest

By running this command, you’re downloading an image directly from Docker Hub and storing it locally on your computer.

Show All Existing Containers

When you create containers with the  --detach  argument, you may need to know whether the containers are still active. To do that, you’ll need to use the  docker ps  command.

➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2da0910758b nginx "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp awesome_vaughan

You can also see all of the images locally present on your computer, with the  docker images -a  command.

➜ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 3 weeks ago 109MB

Understand How to Clean My System

After having run several tests on your computer, you may need to do a bit of housework. To clean things up a bit, you can delete all of the manual resources in Docker.

Or you can let Docker do the clearing up for you! Here’s the command to use for “pruning”:  docker system prune.

➜ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
941b8955b4fd8988fefe2aa91c7eb501f2d4f8c56bf4718fea8ed50904104745
a96e73c623fb6530ab41db6a82aca7017d54a99590f0b45eb6bf934ef8e4d3ed

Deleted Images:
deleted: sha256:797a90d1aff81492851a11445989155ace5f87a05379a0fd7342da4c4516663e
deleted: sha256:c5c8911bd17751bd631ad7ed00203ba2dcb79a64316e14ea95a9edeb735ca3ea

Total reclaimed space: 21.08MB

This will delete the following data:

  • All Docker containers that do not have running status

  • All networks created by Docker which are not used by at least one container

  • All unused Docker images

  • All caches used to create Docker images

Let’s Recap!

You now know how to download, start, and stop containers. You’re also able to define a Docker registry, and you know why we use them.

Let’s recap the important points in this chapter:

  • Starting a container with  docker run

  • Using the arguments  -d  and  -p  when starting a container

  • Pulling an image from the registry with the  docker pull  command

  • Cleaning the system with  docker system prune

In the next chapter, you’ll create your first Docker image using Dockerfile!

Example of certificate of achievement
Example of certificate of achievement