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
Install Docker using Ubuntu's official Docker installation instructions.
After installation, check the Docker version:
docker --version
Check whether the Docker service is running:
sudo systemctl status docker
If necessary, start the service:
sudo systemctl start docker
Enable automatic startup:
sudo systemctl enable docker
3. Run Your First Docker Container
Once Docker is installed, you can test it using the Hello World image.
sudo docker run hello-world
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
List available images:
sudo docker images
Remove an unused image:
sudo docker rmi nginx
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
Here's what the command does:
-
-druns the container in the background. -
-p 8080:80maps host port 8080 to container port 80. -
--nameassigns a name to the container. -
nginxspecifies 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
List all containers, including stopped ones:
sudo docker ps -a
View container logs:
sudo docker logs my-nginx
Monitor resource usage:
sudo docker stats
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
Create a Dockerfile:
FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
Create a simple index.html file in the same directory.
Build the image:
sudo docker build -t my-web-app .
Run the new image:
sudo docker run -d -p 8081:80 --name web-app my-web-app
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
Start it again:
sudo docker start my-nginx
Restart a container:
sudo docker restart my-nginx
Remove a stopped container:
sudo docker rm my-nginx
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
Look for application errors, missing dependencies, or incorrect startup commands.
Port already in use
Check which process is using a port:
sudo ss -tulnp
You may need to stop the conflicting service or choose another host port.
Container using too much memory
Monitor usage:
sudo docker stats
Investigate the application before applying resource limits.
Docker service not running
Check the service:
sudo systemctl status docker
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)