DEV Community

Arthur
Arthur

Posted on

Docker on Linux: A Practical Guide to Managing Containers, Images, and Applications

Hello, I'm Arthur.
Running applications on different servers can become complicated when each environment has different software versions, dependencies, and configurations.

This is one reason Docker has become an important tool in modern software development and server administration.

Docker allows developers to package applications and their dependencies into containers. These containers provide consistent environments and make application deployment easier.

In this guide, we'll explore practical Docker commands and learn how to manage containers on a Linux server.

1. Understand How Docker Works

Docker is a containerization platform that allows applications to run in isolated environments.

A Docker container includes the application and the dependencies it needs to operate.

There are three important concepts to understand:

  • Images: Templates used to create containers.
  • Containers: Running instances of Docker images.
  • Dockerfile: A configuration file used to build custom images.

Unlike traditional virtual machines, containers share the host operating system's kernel, making them lightweight.

2. Install Docker on Ubuntu

Start by updating your package list:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Docker using Ubuntu's official Docker installation instructions.

After installation, check the Docker version:

docker --version
Enter fullscreen mode Exit fullscreen mode

Check whether the Docker service is running:

sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

If necessary, start the service:

sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Enable automatic startup:

sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

3. Run Your First Docker Container

Once Docker is installed, you can test it using the Hello World image.

sudo docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Docker checks whether the image is available locally. If it isn't, Docker downloads it and runs the container.

This simple test confirms that Docker can successfully create and execute a container.

4. Download and Manage Docker Images

Docker images provide the foundation for creating containers.

To download an Nginx image:

sudo docker pull nginx
Enter fullscreen mode Exit fullscreen mode

List available images:

sudo docker images
Enter fullscreen mode Exit fullscreen mode

Remove an unused image:

sudo docker rmi nginx
Enter fullscreen mode Exit fullscreen mode

Before removing an image, make sure it isn't required by an existing container.

5. Create and Run a Container

Let's run an Nginx container and make its web server accessible through port 8080.

sudo docker run -d -p 8080:80 --name my-nginx nginx
Enter fullscreen mode Exit fullscreen mode

Here's what the command does:

  • -d runs the container in the background.
  • -p 8080:80 maps host port 8080 to container port 80.
  • --name assigns a name to the container.
  • nginx specifies the image.

Open your server's IP address with port 8080 in a browser to test the application.

6. Monitor Running Containers

Managing containers also means understanding their current status.

List running containers:

sudo docker ps
Enter fullscreen mode Exit fullscreen mode

List all containers, including stopped ones:

sudo docker ps -a
Enter fullscreen mode Exit fullscreen mode

View container logs:

sudo docker logs my-nginx
Enter fullscreen mode Exit fullscreen mode

Monitor resource usage:

sudo docker stats
Enter fullscreen mode Exit fullscreen mode

The stats command displays CPU, memory, network, and storage-related activity for running containers.

These details are useful when investigating resource consumption or application performance.

7. Build a Custom Docker Image

Docker also allows developers to create images for their own applications.

Create a project directory:

mkdir docker-project
cd docker-project
Enter fullscreen mode Exit fullscreen mode

Create a Dockerfile:

FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html
Enter fullscreen mode Exit fullscreen mode

Create a simple index.html file in the same directory.

Build the image:

sudo docker build -t my-web-app .
Enter fullscreen mode Exit fullscreen mode

Run the new image:

sudo docker run -d -p 8081:80 --name web-app my-web-app
Enter fullscreen mode Exit fullscreen mode

Your custom application is now running inside a Docker container.

For production environments, use an appropriate tested image version rather than relying on an unpinned latest tag.

8. Manage Container Lifecycle

Containers sometimes need to be stopped, restarted, or removed.

Stop a container:

sudo docker stop my-nginx
Enter fullscreen mode Exit fullscreen mode

Start it again:

sudo docker start my-nginx
Enter fullscreen mode Exit fullscreen mode

Restart a container:

sudo docker restart my-nginx
Enter fullscreen mode Exit fullscreen mode

Remove a stopped container:

sudo docker rm my-nginx
Enter fullscreen mode Exit fullscreen mode

These commands are useful during application updates and routine maintenance.

9. Troubleshoot Common Docker Problems

Docker containers can experience several common issues.

Container exits immediately

Check its logs:

sudo docker logs container_name
Enter fullscreen mode Exit fullscreen mode

Look for application errors, missing dependencies, or incorrect startup commands.

Port already in use

Check which process is using a port:

sudo ss -tulnp
Enter fullscreen mode Exit fullscreen mode

You may need to stop the conflicting service or choose another host port.

Container using too much memory

Monitor usage:

sudo docker stats
Enter fullscreen mode Exit fullscreen mode

Investigate the application before applying resource limits.

Docker service not running

Check the service:

sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

Review system logs if the service fails to start.

10. Choose the Right Infrastructure for Containerized Applications

Docker makes application deployment easier, but containers still depend on the resources available on the host server.

Multiple containers running databases, APIs, and web applications can require considerable CPU, RAM, storage, and network capacity.

When planning production deployments, HelloServer dedicated server hosting is worth exploring for comparing infrastructure options based on application requirements.

For specialized workloads involving AI models, machine learning, and GPU-accelerated processing, HelloServer GPU dedicated servers can also be considered when containerized applications require dedicated GPU resources.

Final Thoughts

Docker has made application deployment and environment management much easier for developers and system administrators.

Understanding images, containers, Dockerfiles, resource monitoring, and troubleshooting commands provides a strong foundation for working with containerized applications.

Start with simple containers, experiment with custom images, and gradually learn how to manage larger deployments.

The more you understand how containers interact with the host operating system, the easier it becomes to manage reliable application environments.

Top comments (0)