Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Docker Fundamentals and Essential Commands

Tech 1

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

  1. List local images: docker image list
    • Use -a to show all images, including intermediate layers.
    • Use -q to display only image IDs.
  2. Search for images in a registry: docker search [image_name]
    • Limit results: docker search --limit 3 nginx
  3. Download an image: docker image pull [image_name]:[tag]
    • Omitting the tag fetches the latest version.
  4. Check disk usage by images, containers, and volumes: docker system df
  5. Remove an image: docker image rm [image_id]
    • Force removal with -f.

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

  1. Create and start a container:

    docker container run [options] [image] [command]
    
    • --name "container_name" assigns a custom name.
    • -d runs the container in detached mode.
    • -it combines interactive and TTY modes for shell access.
    • -p [host_port]:[container_port] maps ports. Example: docker container run -it alpine /bin/sh
  2. List containers: docker container list

    • Show all containers with -a.
    • Display only IDs with -q.
  3. Exit a container:

    • Type exit to stop the container.
    • Press Ctrl+P followed by Ctrl+Q to detach without stopping.
  4. 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]
  5. Remove a stopped container: docker container rm [container]

  6. Inspect containers:

    • View logs: docker container logs [container]
    • Check processes: docker container top [container]
    • Get detailed information: docker container inspect [container]
  7. Access a running container:

    • Execute a command: docker container exec -it [container] /bin/bash
    • Attach to the main process: docker container attach [container] Using exec is preferred as it doesn’t stop the container upon exit.
  8. Copy files between container and host:

    • From container to host: docker container cp [container]:[path] [host_path]
  9. 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
  10. 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

  1. Search for an image.
  2. Pull the image.
  3. Verify the image.
  4. Run a container from the image.
  5. Stop the container when needed.
  6. Remove the container.

MySQL Installation Example

  1. Pull the image: docker image pull mysql:5.7
  2. 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
    
  3. Access the container: docker container exec -it db_instance bash
  4. Connect to MySQL: mysql -u root -p

Redis Installation Example

  1. Pull the image: docker image pull redis:latest
  2. Prepare configuration and data directories on the host.
  3. Adjust Redis configuration file to disable daemon mode and set a password.
  4. 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
    
  5. Connect via CLI: docker container exec -it cache_instance redis-cli

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.