Getting Started with Using Docker

Docker containers provide OS level virtualization, while keeping the applications’ code, dependencies, and tools persistent across different environments. Docker containers share the host OS kernel, which makes them lightweight, fast to deploy, and easy to manage.

Learning Docker can be approached through a variety of resources, but this guide provides a practical, hands-on tutorial covering advanced topics and real-world applications that walk you through the core concepts, commands, and workflows needed to start building and running containerized applications.

Quick Tip: Interactive browser-based labs can aid in learning Docker.

Docker Installation

Docker can be installed on Windows, Mac, and Linux, and it’s available in several editions, including Community Edition (CE) and Enterprise Edition (EE).

See Also: What is Docker CE

To get started, visit the Docker website and download the appropriate version for your platform, or just Google “Install Docker” or “Download Docker”. Follow the prompt, and you are good to go. You would need to enable Hyper-V and Containers features before you click install. If you’re using a no UI Linux distribution, install Docker using “curl -fsSL https://get.docker.com -o get-docker.sh“.

Run ‘docker run hello-world‘ to verify Docker installation. Alternatively, use ‘sudo docker version‘ to check Docker installation on Linux. Once you have Docker installed, you can run the “docker” command from your terminal or command prompt.

See Also: How to Install Docker on Ubuntu 

Docker Commands 

Now that the installation is done, let’s look at some basic commands used in installing Docker. First, open your command prompt or code editor terminal and ensure you are in the PC’s directory. Then you should go ahead and test some of the essential Docker commands:

Docker Version

docker version

Docker Images

A Docker image is a read-only template containing instructions for creating a Docker container. You can think of an image as a snapshot of an application and its dependencies, ready to be run in a container. To list the Docker images on your system, run the following command:

docker images

To download a Docker image from a registry, use the Docker pull command followed by the image name and image tag name (if applicable):

docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

Quick Fact: Base images have no parent image, like Ubuntu or Busybox. Also, child images are built on base images, adding additional functionality.

Docker Containers

To create a Docker container, you need to start with an image and customize it as needed. To create a new Docker container from an image, use the Docker create command followed by the image name.

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

This command will start a new container based on the specified image and run the Docker command (if any). To list the running Docker containers, use the following command:

docker container ls or docker ps

To stop a running container, use the docker stop command followed by the container ID or name:

docker stop [OPTIONS] CONTAINER [CONTAINER...]

To push a specific image of a Docker image to a registry, use the docker push command followed by the image name and tag:

docker push [OPTIONS] NAME[:TAG]

To pull a Docker image from a Docker registry here, use the docker pull command followed by the image name and tag:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Now that we’ve covered the basics of Docker architecture and command-line tools, let’s move on to building Docker images.

How to Build Docker Images

The Docker images are the very core of every single containerized application. Before you can launch a container, Docker needs an image that contains everything the application needs. This includes runtime, application code, libraries, dependencies, and configuration files.

The Docker image is basically a blueprint. Every time Docker creates a new container, the environment remains consistent, regardless of the underlying operating system or host machine. This consistency is one of the primary reasons why Docker is a popular choice among teams for application development.

Docker Images Lifecycle

Understanding Docker Images

A Docker image includes multiple read-only layers on top of each other. These layers represent all the changes made during the image creation process. For instance, one layer may install packages, another might copy application files, while another configures environment variables.

When Docker builds an image, it stores these layers separately. If multiple images share the same base image, Docker reuses existing layers instead of downloading or rebuilding them. This approach reduces storage consumption and accelerates deployments.

Many developers begin with pre-built images available on Docker Hub. These official images provide trusted starting points for common operating systems, databases, programming languages, and web servers. Some examples would include Ubuntu, Nginx, MySQL, PostgreSQL, Node.js, and Python.

What Is a Dockerfile?

To create your own Docker image, you need a Dockerfile. Docker images are built using a Dockerfile with specific instructions. It’s a simple text file, and here’s everything that it contains:

  • Docker’s base images
  • The working directory
  • Required dependencies
  • Environment variables
  • Application files to copy
  • Commands to execute
  • Default startup process

For instance, if you’re about to deploy a Flask app, the Dockerfile will tell the engine precisely how to build everything required for the application to run within a container.

See Also: How to Use the Docker Build Command to Create an Image from a Dockerfile

Here’s an example of a basic Dockerfile:

FROM python:3.12
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

The example configuration begins with choosing an official image that will create a working directory and install the dependencies through the application code.

Choosing a Base Image

The base image is specified through the FROM instruction. Here’s an example:

FROM ubuntu:24.04
FROM nginx:latest
FROM python:3.12

The chosen base image has a direct impact on many imporances like startup speed, security, resource consumption, and size. For instance, if you choose an Ubunto image, it will contain an entire Linux user space, while many lightweight alternatives such as Alpine Linux can reduce the image size significantly.

Many production environments go with much lighter options, first to minimize the resource usage, and second to reduce the attack surface.

Building a Docker Image

To build your first Docker image, you need to open the terminal and navigate to the projects directory. You can use the following command:

docker build -t [appname]:1.0 .

Here’s exactly what this command means:

  • docker build starts the image-building
  • -t assigns your project’s name and tag
  • [appname] is the Docker image name
  • 1.0 is the version tag of the application
  • . specifies the build context (access)

These commands are processed sequentially by the Docker Engine. At the end, this will create a Docker image, ready to be deployed as a containerized application. You can check whether this was successful:

docker images

The output should show you the first Docker image as well as its repository name, tag, image ID, and the creation date, time, and size. This means that you’ve successfully generated your first Docker image file.

Note: After creating an image, you can upload it to Docker Hub for sharing. Respectively, you can use Docker Hub to download an image ready to be containerized.

How to Run Docker Containers

When you have an image that you would like to containerize, the next step is to launch it. The container represents an active instance created from the image. Every single container running on a system starts from the image and works in an isolated area with its own dedicated processes, network, and filesystem.

This is the separation (isolation) that provides the developers with a way to run apps consistently across different operating systems, servers, and environments.

About Images and Containers

Before we can launch an image, we need to break down a common misconception amongst beginners: the difference between an image and a container. The image is the blueprint, while the container is the running instance of this image. Hence, you can launch multiple Docker containers from the same image.

A good example here would be creating multiple containers from a simple Nginx image:

docker run nginx
docker run nginx
docker run nginx

As you can see, although there is only one single Nginx image, Docker can treat it as multiple containers.

Running a Docker Container

As you might have already guessed, the command you need to use to run a container from an existing image is the “run” command. What’s interesting here is that if the image does not exist locally, Docker will start pulling images from the Docker Hub automatically.

Here’s how the container launching process works:

  1. Docker will search for the image locally.
  2. If not locally, Docker would download it.
  3. A creation of the writable container layer.
  4. Container network configuration process.
  5. Starting the container application process.

When this process is completed, you will end up with a completely operational container. From there on, what’s left is management, tweaking, and adjustments.

Viewing Running Containers

When you launch a container, you can check whether it’s running or not by using this Docker command:

docker ps

The Docker ps command will show you all currently active containers, along with all the parameters, like Container ID, Image Name, Command, Creation Time, Status, Port Mappings, and Container Names.

Here’s a surface-level example:

CONTAINER ID   IMAGE    STATUS
5f8b4d1c1234   nginx    Up 2 minutes

When you’re running multiple containers, you might need to also see the stopped containers:

docker ps -a

This provides a complete view of what’s happening with all containerized applications in the environment.

Containers in Detached Mode

In Docker, detached mode means that the container is running in the background of the terminal. When you start a container in detached mode, Docker starts the container with a unique ID and immediately relinqishes the control of the terminal back to you, so you can continue executing any other commands.

Here’s a simplified comparison:

Feature:Foreground Mode (Default):Detached Mode (-d):
Terminal ControlLocked. You must wait for the process to finish or manually stop it.Free. You get your command prompt back immediately.
Logs and OutputStreams directly to your terminal screen in real-time.Hidden. The container runs in the background.

By default, after running a new container, it runs in the foreground. This means that the terminal will be attached to the container process. Therefore, for services that are intended to run for longer periods of time, such as web servers and APIs, you would need to switch to detached mode to free your terminal.

docker run -d nginx

The -d flag here will instruct the docker to launch the container in the background and return the contol of the terminal. You should always verify the status to be sure:

docker ps

Running containers in the background is critical for large-scale operations, so administrators can retain control of the terminal while the containers run in the background.

Naming Docker Containers

When you run a container, Docker generates a random name for each container, unless you specify one:

docker run -d --name testserver nginx

Using the tag –name, you can provide names to your containers, which is a much easier way to manage them than using the automatically generated IDs.

Removing Docker Containers

Inactive containers can consume resources, which can otherwise be spent productively. There is a way to permanently remove stopped containers without removing or altering the original container image.

docker rm testserver

Note: You can then launch a completely new container from the same image whenever needed.

Creating Docker Volumes

An important concept to understand with Docker containers is data persistence. Typically, the data is stored within the container’s filesystem. When a container is deleted, the data is deleted with it, which could be a problem in workloads like databases, content management systems, and other services that need the data to persist beyond the life of a single container.

Docker Volumes provide a dedicated storage mechanism managed by Docker itself. Instead of storing data inside the container, Docker volumes store data outside a container’s filesystem, still accessible by applications running from the other containers (persistent storage).

Creating a volume can be done with the command ‘docker volume create‘.

You can view existing volumes using:

docker volume ls

This separation between containers and data provides flexibility during app deployment. Administrators can replace a container, update an image, or launch a completely new Docker container and persist data.

Running Multiple Containers

Sometimes running multiple containers is a necessity. For instance, a web application might require a frontend container, an API container in the background, a database container, and a caching container.

In these scenarios, multiple containers communicate across a shared Docker network while maintaining isolation from one another. One container might handle user requests while another container manages database operations. In larger environments, two containers, ten containers, or even hundreds of them may work together to deliver a single application. This architecture forms the foundation of running multi-container applications and modern microservices deployments.

Interesting Fact: Docker’s popularity helped shape many cloud-native services. AWS Elastic Beanstalk has supported single-container Docker deployments since April 2014. AWS Elastic Container Service allows scalable Docker container management.

Understanding Docker Compose

As applications grow in number and complexity, managing multiple Docker containers individually could become increasingly difficult. Docker Compose simplifies this process by allowing developers to define and launch an entire application stack using a single configuration file and command.

So, Docker Compose was originally launched as Fig in January 2014. Docker Compose uses a docker-compose.yml file to manage multi-container applications. Compose allows defining services, networks, and volumes in one file. This approach helps teams maintain consistency across development, testing, and production environments while reducing the complexity of configuring containerized applications.

Docker Container Workflow Example: (WordPress Website)

We’ve gone through the basic concepts of Docker and how they work. To put this knowladge in practise, we’re going to show you how Docker containers simplify the deployment of a basic WordPress website. In just a few steps, you’ll launch WordPress, connect it to a database, verify the deployment, and then manage the container lifecycle.

Note: Docker version 18.05.0-ce is used in the tutorial.

Step 1: Create a Docker Network

The very first step is to create a dedicated network, so WordPress and MySQL can easily communicate. Docker networking is responsible for connecting containers and controlling how they communicate with each other and external systems. Docker creates three default networks: bridge, host, and none. So, for most deployments, the bridge network is the preferred option because it provides isolation while allowing connected containers to exchange traffic.

Containers on the same bridge network can communicate using IP addresses. Containers can resolve each other’s names on user-defined networks. This eliminates the need to manually track container IP addresses, making multi-container applications much easier to manage.

Docker networking supports multiple types, including bridge and overlay. While overlay networks are commonly used in clustered environments, a bridge network is sufficient for this WordPress example.

docker network create wordpress-network

The above command creates a custom bridge network that enables communication between WordPress and MySQL. This is critical for any underlying system before deploying applications.

Step 2: Launch the MySQL Container

Any WordPress website requires a database to store posts, pages, settings, and all the user information.

docker run -d \
--name wordpress-db \
--network wordpress-network \
-e MYSQL_ROOT_PASSWORD=useasecurepassword \
-e MYSQL_DATABASE=wordpress \
mysql:8

💡Reminder: If you don’t have the image locally, Docker will automatically download it.

Step 3: Launch the WordPress Container

You now have the database running. It’s time to launch the WordPress application:

docker run -d \
--name wordpress-site \
--network wordpress-network \
-p 8080:80 \
-e WORDPRESS_DB_HOST=wordpress-db:3306 \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=useasecurepassword \
-e WORDPRESS_DB_NAME=wordpress \
wordpress

Note: The WordPress container connects to the database container through the Docker network and publishes the website on port 8080.

Step 4: Access the WordPress Website

To access the WordPress website, open a web browser and go to “http://localhost:8080“. If you can’t reach the website, verify that the containers are running through “docker ps” and check whether port 8080 is open. After that, you should see both the “WordPress” and “MySQL” containers listed as active.

If you want to stop the containers, run a single command for each:

docker stop wordpress-site
docker stop wordpress-db

The container stops. If you want to remove the containers, run these:

docker rm wordpress-site
docker rm wordpress-db

In this case, a Docker container acts as a standardized unit that packages an app and its dependencies into a portable environment. So, unlike a traditional virtual machine, containers do not require a separate guest OS, which helps reduce resource consumption and improve startup times. Also, if you no longer need an image after removing its containers, you can delete it using Docker RMI.

Note: You can also create a container copy from the same image whenever you need to launch additional instances of the application.

See Also: Docker vs Kubernetes

Take Your Docker Deployments Further With ServerMania

ServerMania dedicated servers hosting

We understand how challenging could be when you’re experimenting with Docker Desktop, building your first Ubuntu image, or trying to deploy large-scale workloads. Here at ServerMania, we make this simple through Dedicated Servers and Cloud Solutions (AraCloud).

Modern Docker environments rely on technologies such as the Docker Daemon, service discovery, and auto scaling to support growing applications while ensuring consistency in development and production. We deliver enterprise-grade hosting backed by powerful hardware, global network connectivity, and expert support, giving businesses the resources needed to build, deploy, and scale modern applications.

💬 If you have any questions, get in touch with our 24/7 support team or book a free consultation with a Docker containerization expert to discuss your next project. We’re available right now!