Skip to main content
Docker containers have become immensely popular due to their portability and ease of management. To illustrate their usefulness, let’s compare a traditional MariaDB database setup with one running inside a Docker container. Traditionally, to install MariaDB on a Linux system, you would run:
After the installation, you would need to adjust configurations in the /etc/mysql directory, create databases, configure users, assign privileges, and manage log files. When it comes time to migrate this multi-component setup to a cloud server, the scattered pieces across various directories can make the process cumbersome.
The image illustrates a cloud computing concept with elements like databases, settings, and logs connected to a central server, emphasizing container management.
By containerizing your application, such as setting up your MariaDB inside a Docker container, every component—including the daemon, configuration files, logs, and databases—resides within a single container. This makes migration as simple as copying one file.
Imagine moving your entire database within a single container to a cloud server or scaling it across multiple servers effortlessly.
The image illustrates a computer setup with a cloud outline, featuring Docker and MariaDB setup icons on the screen, emphasizing container management. The text suggests that everything will function as before.
Containers are designed to encapsulate applications, making them highly portable and scalable. Essentially, they are like copying and pasting an entire application setup in one go.
The image illustrates the concept of creating and managing containers using Docker, with APIs running on different servers.
In this article, we will explore practical examples of how to work with Docker by running an Nginx web server within a container.

Using Docker Commands

Before diving into running containers, note that you can often execute Docker commands without sudo if your Linux user is added to the docker group. If you encounter permission errors, you can either prepend sudo to your commands or add your user to the Docker group and then log out and back in. For example, you might see an error like this:
By using sudo, the command runs successfully:

Exploring Docker Commands

If you’re new to Docker, you can display all available commands by running:
This command provides the following overview:
One useful subcommand is search which allows you to find images like Nginx:
The output lists available images, and the top result is usually the official image:
You can pull the official Nginx image by running:
The term latest at the end of the image name is known as the image tag—it functions similarly to a version label. For instance, latest corresponds to the newest version, while a tag like 1.24.0 indicates a specific version. To pull an older version, run:

Managing Docker Images

After pulling images, it’s a good practice to remove any that are no longer needed. To list available images, run:
To delete a specific image, such as the nginx:1.22.1 image, use the rmi command. When multiple images share the same repository name, remember to specify the tag:
After deletion, verifying your images again should show only the remaining ones:

Running Containers

Running a container is as easy as issuing the docker run command. For instance, to run an Nginx container:
This command creates a new container and starts the Nginx daemon. However, running the container this way attaches you to its output, making it challenging to run additional commands simultaneously. The output may look similar to:
Pressing Ctrl-C will detach you from the container but will also stop it. To run the container in the background, publish ports, and assign it a descriptive name, use the following command:
This command does the following:
  • --detach: Runs the container in the background.
  • --publish 8080:80: Maps port 8080 on the host to port 80 in the container.
  • --name mywebserver: Assigns the name “mywebserver” to the container.
To view currently running containers, use:
You might see output like:
Remember that docker ps only shows running containers. Use docker ps --all to list both active and stopped containers.
To start a stopped container, reference its container ID or name:
And validate its status with:
If you need to stop a running container, simply run:

Testing the Published Port

After running your container with port mapping, it’s important to verify that it’s accessible. You can do this by connecting to the published port on your host using a tool like netcat (nc):
Type the HTTP request:
You should receive the HTML response of the Nginx index page:
Press Ctrl-D to terminate the netcat session.

Understanding Docker Run vs. Docker Start

It’s essential to understand the difference between docker run and docker start:
  • docker run: Creates a new container from an image and starts it.
  • docker start: Simply starts an existing, previously created container.
Use docker run when the container does not already exist and docker start to resume a stopped container.

Removing Containers and Images

Managing system resources involves cleaning up unused containers and images. To list all containers, run:
To remove a stopped container (for example, one named determined_perlman), execute:
Remember: the docker rm command removes containers, while docker rmi is used to remove images.
If an image is still in use, such as by the “mywebserver” container, attempting to remove it will result in an error:
In that case, stop and remove the container first:
Then remove the image:

Using Restart Policies

For scenarios where you need your Nginx container to restart automatically after errors or system reboots, you can add a restart policy. The basic command:
Can be enhanced with the --restart always option to ensure continuous operation:
If the specified image is not available locally, Docker will automatically pull it.
The image shows a section of a manual page for Docker, detailing the restart policy options for containers, including "no," "on-failure," "always," and "unless-stopped," with explanations for each.

Building Custom Docker Images

Sometimes, public images may not meet your exact needs, prompting you to build a custom image. Here’s how you can create your own image based on the Nginx image:
  1. Create a directory for your project and navigate into it:
  2. Create a simple index.html file to serve as your custom HTML content:
    (Insert a line of text as a placeholder for your custom HTML content.)
  3. Create a Dockerfile (note the capital D), which provides instructions for building your image:
    Add the following content to the Dockerfile:
    This tells Docker to use the official Nginx image as the base and copy your custom HTML file into the appropriate directory inside the container.
Other commonly used Dockerfile instructions include:
  • RUN: Executes commands during the image build (e.g., installing additional utilities).
  • CMD: Specifies the default command to run when the container starts (which can be overridden).
  • ENTRYPOINT: Ensures a command is always executed upon container initialization (and is less easily overridden).
  1. Build your custom image using the following command (replace “jeremy” with your Docker Hub username if applicable):
    You should see output similar to:
This custom image can now be used in the same way as any other Docker image.

Summary

This article has covered the essentials of creating, managing, running, and building Docker containers—from using public images to constructing your own custom container images. With Docker’s powerful features, deploying applications at scale becomes a seamless process. For further details and in-depth tutorials, consider exploring the following resources: Happy containerizing!

Watch Video