DockerLinkedinLearning

Using Docker

img

Exploring the Docker CLI

🧑‍💻 Exploring the Docker CLI: Your Terminal-Based Power Tool

The Docker CLI (Command Line Interface) is where the real container action happens. Whether you’re on a Mac, Windows, or Linux, this is your go-to for running and managing Docker containers.


🔥 Getting Started:

  • Open your terminal:

    • Mac: Command + Space → type Terminal

    • Windows: Start → search for PowerShell or Command Prompt

    • Linux: You already live here 😉


🧠 The Basics:

Run this to see all your Docker options:

docker --help

You’ll see a list of top-level commands like:

  • docker run – Run a container

  • docker pull – Download an image

  • docker build – Build an image from a Dockerfile

  • docker ps – List running containers

  • docker images – Show local images

  • docker network – Manage Docker networks (more advanced)


🧰 Pro Tip:—help is your best friend

Every Docker command supports the —help flag. For example:

docker network --help

This shows:

  • What the command does

  • Subcommands (like create, connect, disconnect)

  • Flags and options available

Try it on a subcommand too:

docker network create --help

If you ever forget a flag or aren’t sure what something does — just tack on —help. Even pros do it all the time. 👍


TL;DR:

Docker CLI is powerful but friendly. Use —help like a built-in cheat sheet to explore commands and subcommands. Start simple, grow powerful. 💪🐳


Creating Docker Container

img

🧠 Docker Containers for Dummies (AKA Us)

👨‍🏫 “Let’s go the long way so we can appreciate how clutch the short way is.” – Wise Instructor Vibes


🌱 Step 1: Create a Container – The Long Way (aka manually adulting)

TL;DR:

Containers come from images (pre-packaged apps w/ environment + entry point = vibes).

👨‍💻 Run this:

docker container create hello-world:linux

📦 If you don’t have the image? Docker’s like, “No prob fam, I’ll grab it from Docker Hub.” ✅

🧙‍♂️ Output = Container ID (kinda like your container’s Social Security Number).

But wait… It doesn’t run yet. 💀


🚫 Step 2: Check Yo’ Container (It’s chillin’, not workin’)

docker ps            # shows running containers (spoiler: yours ain’t there)
docker ps --all      # ah there it is, just *vibin'* in “created” mode

🚀 Step 3: Start the Party

docker container start <your-container-id>

😶 Still no “hello”? That’s ’cause it ran and dipped. Check the logs like a hacker:

docker logs <first-3-letters-of-id>  # Docker's got autocomplete magic

📜 BOOM. That sweet “Hello from Docker!” message. You did it, legend.


🧷 Bonus Round: Want Real-Time Drama?

docker container start --attach <id>

Now your terminal is attached to the container like you’re livestreaming its every move. 🎥


💡 Pro Tips:

  • You can restart containers without re-creating ’em. Think of them like reusable Starbucks cups ♻️

  • Docker doesn’t delete them unless you tell it to. Respect.


💬 Next up: The shortcut way (because we’re lazy but smart 😤)


Create a Docker container: The short way

img

⚡️ Docker Container Creation: The Short Way

👨‍🏫 “Remember the long way? Ew. Let’s upgrade.”

🎯 The Magic Command:

docker run hello-world:linux

💥 What Just Happened?

Docker did the whole dang thing for us:

docker run = docker container create + start + attach

No need to:

  • Manually start the container 🥱

  • Check logs with extra steps 🔍

  • Think too hard 🧠❌


🕵️‍♀️ Wanna Peek Under the Hood?

Wanna see what just ran?

docker ps --all

⛅ That “exited” status? That’s just Docker saying “I did my job, now I’m out ✌️”


📜 Need to See That Sweet Hello Again?

docker logs d90  # or whatever the first few characters of your container ID are

🎉 Output:

Hello from Docker!

Still hits like a warm hug from the command line 🫶


TL;DR Wrap-Up:

  • docker run is the Uber Eats of container commands. One stop, no drama.

  • No need to babysit containers unless they’re long-running.

  • Want the ID? Use docker ps —all

  • Forgot what happened? docker logs got you.


Create a Docker container from Dockerfiles, part 1

So far: We’ve just been vibing with pre-built containers from Docker Hub

But now: We’re about to DIY our own container from scratch 🔨

🗂️ What’s in the files

  • Dockerfile — our secret recipe 📜

  • entrypoint.bash — a lil’ script that just tells us the time ⏰ (cute, right?)


💡 Key Dockerfile 🔑 keywords

  • FROM:

    Sets the base image (like saying “I want to start with Ubuntu vibes”)

    Docker will pull it from the net if it’s not local 🌐

  • LABEL:

    Meta info – who made this, etc. (think of it like the IG bio for your image)

  • USER:

    Default is root = powerful but dangerous 😬

    Use something like nobody for safety. Stay lowkey, stay safe 🛡️

  • COPY:

    Copy files from your local folder into the image.

    The “context” = the folder you’re building from 👀

  • RUN:

    Runs commands during image creation.

    Like installing curl + bash because your script needs it 🧑‍💻

  • ENTRYPOINT:

    Sets what the container actually does when it runs (e.g., run that bash script!)

    Could also use CMD, but they ain’t the same 😤 – check docs for that tea 🍵


🧠 TL;DR:

We’re now cooking our own Docker image using a Dockerfile, setting it up with the right tools, locking it down with the nobody user, and telling it what to do when it runs. Super secure, super chill 😎


Create a Docker container from Dockerfiles, part 2 

🧠 The Mission

Turn that boring text file (aka Dockerfile) into a full-blown Docker Image, then run it like a boss. 💼

🛠️ Step 1: Build it like Bob 👷

docker build -t our-first-image .

🎯 Breakdown:

  • docker build: The builder tool 🧱

  • -t our-first-image: Give your image a cute name so you don’t have to memorize random IDs 🧠

  • . = current folder = your build context (aka, “this is where my files are bro”) 🗂️

📌 If your Dockerfile was called something else like app.Dockerfile, then:

docker build -t our-first-image -f app.Dockerfile .

⚙️ What Docker does behind the scenes:

Docker reading your Dockerfile:

  • It runs each line in your Dockerfile

  • Every line = 🧱 a new image layer

  • Layers = 🧇 like a stack of pancakes.

  • Final step: Squish it all together into one mega image 🤝


📦 Intermediate Images = Temp Layers

They’re like mini-versions of your image after each command.

“Temporary but essential, like YOUR gym motivation”


✅ All done? Run it!

docker run our-first-image

And boom 💥 you just ran a container from your own image.


🕒 And the result?

Your container says:

“Yo it’s [current time] ⏰, thanks for booting me up.”


Interact with your container

🧪 The Upgrade

Before: containers run → finish → vanish 💨

Now: We’re running server containers that keep chillin’ until we say otherwise 🧘


🚀 Building a Persistent Server Image

docker build -f server.Dockerfile -t our-first-server .

🧠 Remember:

  • -f = use this specific Dockerfile

  • -t = name your masterpiece

  • . = current folder is the context


😬 When you docker run our-first-server and it just hangs

You: Why?

Because the app inside is a server. It doesn’t exit, it just chills there… forever.


🔫 Killing it with fire (in another terminal)

docker ps  # get container ID
docker kill <container_id>

You when you find and kill that stuck container:


🧍 Detach Mode = Your Best Friend:

docker run -d our-first-server
  • -d = run in the background

  • Saves your sanity and keyboard 🧘‍♂️


👀 Is it really running?

docker ps

“He lives!”


🧠 Bonus Hacks: Exec is OP

Run a command inside the container (without logging in)

docker exec <container_id> date

You: “Hey container, what time is it?”

Container: April 15th


Start a terminal inside the container

docker exec --interactive --tty <container_id> bash
  • —interactive = I’m typing things 😎

  • —tty = make it feel like a real terminal

When you’re debugging inside a container like a pro:


🏃 Exit like a ninja

Ctrl + D to dip out of the container shell

Don’t spam it or you might log out of your real terminal lol