Post

Docker Basics - Hoisting the Sails šŸš¢

Docker Basics - Hoisting the Sails šŸš¢

Welcome back to ā€œDocking the Ship: Navigating Docker for Developersā€! After learning what Docker is and why itā€™s a game changer, itā€™s time to dive into the fundamentals. In this post, weā€™ll focus on installing Docker, setting up your environment, and running your first container. Ready to hoist the sails? Letā€™s get started!


Why Learn the Basics?

Understanding Docker basics is essential to getting comfortable with its powerful yet simple tools. These foundational skills will enable you to explore more advanced features and workflows in the future.


1. Installing Docker

To get started with Docker, youā€™ll first need to install it on your system. Hereā€™s a quick overview:

Step 1: Download Docker

  • Visit the Docker Desktop Download Page and download the version for your OS (Windows, macOS, or Linux).
  • For Linux users, refer to the official documentation for installation instructions.

Step 2: Install and Set Up

  • Run the installer and follow the setup instructions.
  • For Windows and macOS, Docker Desktop provides an easy-to-use GUI and CLI tools.

Step 3: Verify Installation

After installation, verify that Docker is running by opening a terminal and typing:

1
docker --version

If it returns the Docker version, youā€™re all set to move forward! šŸš€

Output: Docker version on my machine

1
Docker version 27.4.0, build bde2b89

Verify client and server version

1
docker version

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Client:
 Version:           27.4.0
 API version:       1.47
 Go version:        go1.22.10
 Git commit:        bde2b89
 Built:             Sat Dec  7 10:35:43 2024
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.37.1 (178610)
 Engine:
  Version:          27.4.0
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.10
  Git commit:       92a8393
  Built:            Sat Dec  7 10:38:33 2024
  OS/Arch:          linux/arm64
  Experimental:     true
 containerd:
  Version:          1.7.21
  GitCommit:        472731909fa34bd7bc9c087e4c27943f9835f111
 runc:
  Version:          1.1.13
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

2. Running Your First Container

Now that Docker is installed, itā€™s time to run your first container! Containers are lightweight, portable environments that run your applications.

Step 1: Pull an Image

Docker uses images to create containers. Letā€™s start with the classic ā€œHello, World!ā€ example. Pull the hello-world image by running:

1
docker pull hello-world

Output:

1
2
3
4
5
6
Using default tag: latest
latest: Pulling from library/hello-world
478afc919002: Pull complete
Digest: sha256:5b3cc85e16e3058003c13b7821318369dad01dac3dbb877aac3c28182255c724
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

Step 2: Run the Container

Create and run a container from the image:

1
docker run hello-world

This command downloads the image (if not already available) and starts a container that displays a ā€œHello, World!ā€ message.

1
Hello from Docker!

Step 3: Check Running Containers

To view active containers, use:

1
docker ps

No containers running? Thatā€™s okayā€”hello-world finishes after it outputs the message. To see all containers (running or stopped):

1
docker ps -a

Output:

1
2
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS                        PORTS                               NAMES
9588e7e356ab   hello-world                         "/hello"                 21 minutes ago   Exited (0) 21 minutes ago                                         jovial_dirac

3. Understanding Essential Docker Commands

Hereā€™s a quick reference to some basic Docker commands youā€™ll use frequently:

  • docker run: Create and start a container from an image.
  • docker ps: List currently running containers.
  • docker ps -a: List all containers (including stopped ones).
  • docker stop [container_id]: Stop a running container.
  • docker rm [container_id]: Remove a stopped container.
  • docker images: List all downloaded images.
  • docker rmi [image_id]: Remove an image from your system.

4. Hands-On Practice

Try running a few additional containers to get more comfortable:

  • Nginx Web Server:

    1
    
    docker run -d -p 8080:80 nginx
    

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    Unable to find image 'nginx:latest' locally
    latest: Pulling from library/nginx
    f5c6876bb3d7: Pull complete
    bdb964b66a74: Pull complete
    b5368be906ec: Pull complete
    755bf136756e: Pull complete
    aafc2e219c20: Pull complete
    261c6a94b398: Pull complete
    a8066ea4829b: Pull complete
    Digest: sha256:42e917aaa1b5bb40dd0f6f7f4f857490ac7747d7ef73b391c774a41a8b994f15
    Status: Downloaded newer image for nginx:latest
    7dce55307298b2134286524d26e1286f3e1c972cf0b5c3208a9ccf4a54511f2e
    

    Visit http://localhost:8080 in your browser to see the Nginx welcome page.

  • BusyBox Shell:

    1
    
    docker run -it busybox
    

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    
    Unable to find image 'busybox:latest' locally
    latest: Pulling from library/busybox
    559c60843878: Pull complete
    Digest: sha256:2919d0172f7524b2d8df9e50066a682669e6d170ac0f6a49676d54358fe970b5
    Status: Downloaded newer image for busybox:latest
    / # whoami
    root
    / # exit
    

    This gives you an interactive shell inside a lightweight BusyBox container. Type exit to leave.


5. Troubleshooting Common Issues

Sometimes things donā€™t go as planned. Here are quick fixes for common problems:

  • Permission Issues:
    If you encounter permission errors, ensure your user belongs to the Docker group (Linux).

    1
    
    sudo usermod -aG docker $USER
    
  • Docker Daemon Not Running:
    Ensure the Docker service is active.

    1
    
    sudo systemctl start docker
    
  • Network Issues:
    Check your firewall or proxy settings if pulling images fails.


Key Takeaways

  • Docker containers are lightweight, fast, and portable.
  • Images are the building blocks of containers.
  • Mastering basic Docker commands is the first step toward becoming proficient in containerization.

Resources:


Whatā€™s Next?

In the next post, Weā€™ll dive deeper into imagesā€”how they are built, customized, and optimized. Stay tuned as we continue our Docker journey!

Stay Tuned for the Next Post: Docker Images: Crafting Your Vessel


Got questions or feedback? Drop a comment below or reach outā€”Iā€™d love to hear your thoughts!

This post is licensed under CC BY 4.0 by the author.

Trending Tags