Docker Course Labs

Running Containers

Containers are a virtual computing environment which Docker creates. When you run a container it typically runs a single process, and you can have that process running in different ways.

CLI Reference

CLI overview

The Docker command line sends instructions to the Docker API.

Commands are grouped by types of object (e.g. containers and networks). You can always print help to list the available commands and the options for a specific command:


docker container --help

docker container run --help

Running a one-off container

Docker containers run as long as the process inside the container keeps running. When the process finishes, the container exits.

You can run a simple container to run a single Linux command:

docker run alpine hostname

You'll see a lot of output when you run this - Docker pulls the Alpine Linux container image so it can run locally, starts a container, runs the hostname command and prints the output.

📋 Run another container with the same hostname command.

Not sure how?

Just repeat the same command:

docker run alpine hostname

The container image doesn't get pulled this time, but the output is a different random string.

Those containers ran a single command - printing the name of the machine, which is set by Docker. When the process completed, the container exited.

📋 Print a list of all your containers.

Not sure how?
# print running containers:
docker container ls

# or use ps
docker ps 

# the -a flag shows all statuses
docker ps -a

You have two containers, both in the exited state. The container IDs match the hostname output - when Docker creates a container it assigns a random ID and sets it as the machine name.

Those containers didn't do anything useful, but one-off containers can be good for automation tasks, they can be packaged with all the tools and scripts they need.

Try a container which generates a security certiicate you can use for HTTPS in dev environments:

docker run -it kiamol/ch15-cert-generator

The -it flag attaches your terminal, so you can send commands to the container. When you see the output Certs generated, exit the container by pressing Ctrl-C / Cmd-C.

📋 The cert-generator container has exited; print out the logs from the process.

Not sure how?
docker container ls -a  # to find the container ID

docker container logs <container-id>

Container logs are the output from the container process - Docker saves them when the container exits.

The cert-generator image packages the OpenSSH library with a script to generate HTTPS certificates for a set of domains, with default settings. You'd need to install the libraries and download the script to get the same result without Docker.

Running an interactive container

One-off containers run and then stop. You can run a long-running process in a container instead, and connect your terminal to the container's terminal.

This is like connecting to a remote machine - any commands you run actually run inside the container:

docker run -it alpine

Now you're connected to the container, you can explore the environment:

ls /
cat /etc/os-release


You can see the container behaves like a full Linux machine, running Alpine Linux.

Linux OS containers for Docker usually have the bare-minimum toolset installed.

The Ubuntu team publish a package for Ubuntu Server but it doesn't have all the usual tools installed. There's no curl, so you can't make HTTP calls, but the container runs as the root user so you have permissions to install anything you need:

docker run -it ubuntu

curl   # command not found

apt-get install -y curl  # this doesn't work - there's no install cache

apt-get update  # update the cache

apt-get install -y curl  # now this works



The changes you've made are only for that one container.

📋 Run another Ubuntu container and see if it can use curl.

Not sure how?
# you can do this as a one-off container - it won't work:
docker run ubuntu bash -c 'curl'

Interactive containers can do fun things with the screen - this is useful to impress people with your hacking skills :)

docker run -it sixeyed/hollywood

# Ctrl-C / Cmd-C to stop the process

# and exit the container

Running a background container

Interactive containers are great for testing commands and exploring the container setup, but mostly you'll want to run detached containers.

These run in the background, so the container keeps running until the application process exits, or you stop the container.

You'll use background containers for web servers, batch processes, databases message queues and any other server process.

docker run -d -P --name nginx1 nginx:alpine

You now have a simple web server running in the container called nginx1.

📋 Find out which port the container is listening on, and try browsing to the web server.

Not sure how?
# print the container's port mapping
docker container port nginx1

# browse to the port with curl or your browser
curl localhost:<container-port>

Docker listens for incoming traffic on your machine's network port and forwards it to the container.


We've run containers using Alpine Linux and Ubuntu, and also with Nginx installed. They're all public packages available on Docker Hub.

In this lab you'll work with Java containers:

Stuck? Try hints or check the solution.


Cleanup by removing all containers:

docker rm -f $(docker ps -aq)