- Docker allows you to package applications with their dependencies, ensuring portability and consistency.
- Containers are created from pre-existing Docker images or customized using a Dockerfile.
- Container execution can be customized using parameters such as volumes, environment variables, and port mapping.
- Docker Compose and other orchestration tools help manage containers in complex environments.
Docker has become one of the essential tools in the development and deployment of modern applications.Its ability to create isolated and consistent environments allows developers to work without worrying about typical configuration issues between different systems.
In this guide you will learn, step by step and in great detail, how to create a Docker container from scratch.We'll cover everything from basic installation to advanced Docker Compose implementations, including creating custom images, using volumes, managing running containers, and best practices for optimizing your work environment.
What is a Docker container and why use it?
A Docker container is a lightweight and portable unit that contains an application along with all its dependencies.Unlike a virtual machine, which includes a full operating system, containers share the host operating system kernel, making them much faster and more efficient.
Thanks to Docker, you can develop, test and deploy your application in different environments without worrying about the differences between them.This feature makes it an ideal solution for both production environments and local testing and development.
Prerequisites for using Docker
- Supported operating system: Ubuntu, Debian, Fedora, CentOS, macOS or Windows.
- Access to superuser privileges (root or sudo) to install and run Docker.
- Internet connection to download official images from Docker Hub.
Installing Docker
Installing Docker is usually straightforward. On Ubuntu, you can install it with these commands:
sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io
To have Docker start automatically when you power on your system, you can run it like this:
sudo systemctl enable docker sudo systemctl enable containerd
Verifying the installation
Once installed, you can verify that Docker is up and running by running this command:
docker run hello world
This command will download a test image and display a message confirming that everything is working correctly.
Creating a Container: The Basics
To create a container you need a Docker image, which is a package containing all the components your application needs. Images can be downloaded from Docker Hub or built manually.
For example, to set up a basic Nginx server you can use:
docker run --name myweb -p 8080:80 -d nginx
With this instruction:
- –name mywebsite: gives a name to the container.
- -p8080:80: Redirects host port 8080 to container port 80.
- -d: runs the container in the background.
Creating custom images with Dockerfile
A Dockerfile allows you to build custom images step by step. Each instruction in the file adds a new layer to the final image. Here's a simple example:
FROM ubuntu:latest WORKDIR /app COPY . . RUN apt-get update && apt-get install -y curl CMD
This file does the following:
- FROM: Use the latest version of Ubuntu as a base.
- WORKDIR: sets the working directory.
- COPY: copies files from the host to the container.
- RUN: installs the curl package.
- CMD: defines the default command when running the container.
To build the image with this Dockerfile, you can refer to the guide at Create custom images with Dockerfile.
Running a container from your own image
Once the image is built, you can launch your application with:
docker run --name example -p 8080:80 myproject
This is how you create a container from the image you just created..
Docker container management
Docker provides several commands to manage your containersSome of the most useful ones include:
docker ps: lists running containers.docker ps -a: shows all containers, including stopped ones.docker stop nombre_contenedor: stops a container.docker rm nombre_contenedor: deletes a container.
Data Persistence: Volumes in Docker
By default, data stored inside a container is lost when it is deleted. To maintain persistent data, it is essential to mount volumesUsing volumes ensures that data survives container deletion and can be shared across containers.
Example with bind type volume:
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=key -v $PWD/data:/var/lib/mysql mysql:8.0
You can also use Docker-managed volumes:
docker volume create mysql_data docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=key -v mysql_data:/var/lib/mysql mysql:8.0
Environment Variables
Docker allows you to configure environment variables that help parameterize the behavior of containers.. For example: uterine
docker run -e MYSQL_ROOT_PASSWORD=admin -e MYSQL_DATABASE=store mysql:8.0
Resource allocation
You can limit the CPU and memory usage of a container. This is useful to prevent a single one from consuming all the system resources:
docker run --cpus=0.5 --memory=512m myimage
Basic orchestration with Docker Compose
Docker Compose simplifies multi-container management. Define all the necessary services in a YAML file and then run them with a simple command. To learn more about creating containers in Docker using Docker Compose, see use Docker Compose.
Basic docker-compose.yml example for MySQL:
version: '3' services: mysql: image: mysql:8.0 ports: - 3306:3306 environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=project - MYSQL_USER=user - MYSQL_PASSWORD=key volumes: - mysql_data:/var/lib/mysql volumes: mysql_data:
To lift the services:
Docker -Compose up -D
Best practices when using Docker
To keep your images and containers efficient, secure, and easy to maintain, follow these recommendations::
- Use official images whenever possible, since they are audited and optimized.
- Keep your containers light using small base images and removing unnecessary files after installing packages.
- Do not run processes as root, creates a specific user within the container.
- Use volumes to store persistent data and avoid losses due to failures or updates.
- Configure sensitive variables as secrets using environments or .env files, to prevent their exposure in the source code.
Practical example: deploying Pandora FMS with Docker
A real application with multiple containers is Pandora FMS. We can deploy both the server and the database with just two commands if we use Docker.
First, we set up the database:
docker run --name Pandora_DB -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pandora -e MYSQL_DATABASE=pandora -e MYSQL_USER=pandora -e MYSQL_PASSWORD=pandora -v mysqlvol:/var/lib/mysql -d rameijeiras/pandorafms-percona-base
The application server is then started pointing to the host IP:
docker run --name Pandora_app --rm -p 8081:80 -e DBHOST=127.0.0.1 -e DBNAME=pandora -e DBUSER=pandora -e DBPASS=pandora -e DBPORT=3306 -ti rameijeiras/pandorafms-community:740
With this you would have a functional instance of Pandora FMS in a matter of minutes..
Docker is a versatile tool that transforms the way applications are deployed and managed. From deploying simple containers for development to complex environments with multiple services, using it improves efficiency, reduces compatibility issues, and facilitates the scalability of your projects. Mastering Docker is an investment that can make a difference in managing your deployments and application development.
Passionate writer about the world of bytes and technology in general. I love sharing my knowledge through writing, and that's what I'll do on this blog, show you all the most interesting things about gadgets, software, hardware, tech trends, and more. My goal is to help you navigate the digital world in a simple and entertaining way.
