Docker Fundamentals and Essential Commands
Docker is a containerization platform that packages applications and their dependencies into isolated units called containers. These containers provide consistent environments across different systems, eliminating issues related to configuration and runtime discrepancies.
An image serves as a blueprint for creating containers. It is a read-only template that includes the application code, libraries, and settings. When executed, an image produces a container instance—a running environment where services operate.
Installing Docker
To set up Docker on a Linux system, use the package manager specific to your distribution. For instance, on Ubuntu, you can install it via apt.
Core Docker Commands
System Management Commands
- Start the Docker service:
sudo systemctl start docker - Stop the Docker service:
sudo systemctl stop docker - Restart the Docker service:
sudo systemctl restart docker - Check Docker status:
sudo systemctl status docker - Enable Docker to start on boot:
sudo systemctl enable docker - View Docker system information:
docker system info - Access general help:
docker --help - Get help for a specific command:
docker [command] --help
Image Management Commands
- List local images:
docker image list- Use
-ato show all images, including intermediate layers. - Use
-qto display only image IDs.
- Use
- Search for images in a registry:
docker search [image_name]- Limit results:
docker search --limit 3 nginx
- Limit results:
- Download an image:
docker image pull [image_name]:[tag]- Omitting the tag fetches the latest version.
- Check disk usage by images, containers, and volumes:
docker system df - Remove an image:
docker image rm [image_id]- Force removal with
-f.
- Force removal with
Dangling images are those without a repository name or tag, displayed as <none>. They can accumulate over time and should be cleaned up periodically.
Container Management Commands
-
Create and start a container:
docker container run [options] [image] [command]--name "container_name"assigns a custom name.-druns the container in detached mode.-itcombines interactive and TTY modes for shell access.-p [host_port]:[container_port]maps ports. Example:docker container run -it alpine /bin/sh
-
List containers:
docker container list- Show all containers with
-a. - Display only IDs with
-q.
- Show all containers with
-
Exit a container:
- Type
exitto stop the container. - Press
Ctrl+Pfollowed byCtrl+Qto detach without stopping.
- Type
-
Manage container state:
- Start a stopped container:
docker container start [container] - Restart a container:
docker container restart [container] - Stop a container gracefully:
docker container stop [container] - Force stop a container:
docker container kill [container]
- Start a stopped container:
-
Remove a stopped container:
docker container rm [container] -
Inspect containers:
- View logs:
docker container logs [container] - Check processes:
docker container top [container] - Get detailed information:
docker container inspect [container]
- View logs:
-
Access a running container:
- Execute a command:
docker container exec -it [container] /bin/bash - Attach to the main process:
docker container attach [container]Usingexecis preferred as it doesn’t stop the container upon exit.
- Execute a command:
-
Copy files between container and host:
- From container to host:
docker container cp [container]:[path] [host_path]
- From container to host:
-
Export and import containers:
- Export container filesystem:
docker container export [container] > backup.tar - Import as a new image:
docker image import - [user]/[image]:[tag] < backup.tar
- Export container filesystem:
-
Rename a container:
docker container rename [old_name] [new_name]
Docker Images and Layers
Images consist of multiple read-only layers. When a container starts, a writable layer is added on top, known as the container layer. Changes made during runtime reside in this layer, while underlying layers remain immutable.
Creating Custom Images
Use docker container commit to save container modifications as a new image:
docker container commit -m "Added network tools" -a "Admin" [container_id] custom_ubuntu:latest
Sharing Images
To share images, push them to a registry like Docker Hub or a private repository.
Data Volumes
Volumes persist data independently of containers, facilitating data sharing and backup.
Create a volume-mounted container:
docker container run -d -v /host/data:/container/data --privileged [image]
- Volumes support real-time updates and are not included in image updates.
- They persist until explicitly removed.
Volume Access Modes
- Read-write (default):
-v /host:/container:rw - Read-only:
-v /host:/container:ro
Volume Inheritance
A container can inherit volume definitions from another:
docker container run -it --volumes-from [source_container] --name secondary [image]
Installing Applications with Docker
General Workflow
- Search for an image.
- Pull the image.
- Verify the image.
- Run a container from the image.
- Stop the container when needed.
- Remove the container.
MySQL Installation Example
- Pull the image:
docker image pull mysql:5.7 - Run the container with persistence:
docker container run -d -p 3306:3306 \ -v /data/mysql/conf:/etc/mysql/conf.d \ -v /data/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=secure_pass \ --name db_instance mysql:5.7 - Access the container:
docker container exec -it db_instance bash - Connect to MySQL:
mysql -u root -p
Redis Installation Example
- Pull the image:
docker image pull redis:latest - Prepare configuration and data directories on the host.
- Adjust Redis configuration file to disable daemon mode and set a password.
- Run the container:
docker container run -d -p 6379:6379 \ -v /host/redis.conf:/usr/local/etc/redis/redis.conf \ -v /host/data:/data \ --name cache_instance redis redis-server /usr/local/etc/redis/redis.conf - Connect via CLI:
docker container exec -it cache_instance redis-cli