Great! Let's start with the basics of Docker commands. First, we’ll verify that Docker is installed and running correctly on your system.
Step 1: Verify Docker Installation and Get Version Information
Check Docker Version
Run the following command to ensure Docker is installed and to see the version:Example Output:
Check Docker Engine Status
Use this command to confirm Docker is running:This will provide detailed information about your Docker setup, such as the number of containers, images, and the server details.
Test with Hello World Container
Run this command to test if Docker is working properly:This pulls a small image named
hello-world
from Docker Hub and runs it. If successful, you'll see a message confirming Docker is installed and functioning.
Step 2: Basic Docker Commands for Images and Containers
1. Listing Docker Images
View all images available on your system:
Example Output:
2. Pulling an Image
Download an image from Docker Hub (e.g., nginx
):
Example Output:
3. Running a Container
Run a container from an image:
This will start a container using the nginx
image. If successful, the container will run but exit immediately because there’s no foreground task.
To run it interactively and keep it alive:
-d
: Runs the container in detached mode (background).-p 8080:80
: Maps port 8080 on your machine to port 80 inside the container.
Open your browser and visit http://localhost:8080
to see the Nginx welcome page.
4. Listing Running Containers
View all currently running containers:
Example Output:
5. Stopping a Container
Stop a running container using its CONTAINER ID
or NAME
(from docker ps
):
6. Removing a Container
Remove a stopped container:
Step 3: Managing Containers with Crucial Flags Explained
1. Running Containers with Names
By default, Docker assigns random names to containers. You can specify a name for better management:
Explanation of flags:
--name my_nginx
: Assigns a custom name (my_nginx
) to the container.-d
: Runs the container in detached mode (in the background).-p 8080:80
: Maps port 8080 on your local machine to port 80 inside the container, allowing access viahttp://localhost:8080
.
2. Viewing All Containers
List both running and stopped containers:
Explanation of flags:
-a
: Displays all containers, including those that have stopped.
3. Starting a Stopped Container
Restart a stopped container:
- This restarts the container without re-creating it.
4. Accessing a Running Container
Use an interactive shell inside a running container:
Explanation of flags:
exec
: Executes a command in a running container.-it
: Combines-i
(interactive mode) and-t
(allocates a pseudo-terminal) to interact with the container via a shell.
Once inside, you can run commands like ls
, cd
, or view configuration files. Type exit
to leave the container.
5. Stopping and Removing Containers
Stop and remove containers with a single command:
Explanation of flags:
-f
: Forcefully stops and removes a running container.
6. Inspecting a Container
Get detailed information about a container:
This outputs JSON data, including network settings, environment variables, and resource usage.
While bothdocker run -it
and docker exec -it
involve interacting with containers, they are not the same. Let me clarify the difference:1. docker run -it firstdocker bash
Purpose: Used to create and start a new container interactively from an image.
Key Characteristics:
- It starts a brand-new container from the specified image (
firstdocker
in this case). - The
-it
flags make the container interactive, allowing you to access its terminal. - The command (
bash
) specifies the shell to run inside the container.
- It starts a brand-new container from the specified image (
Scenario: This is useful when you want to launch a new container and immediately interact with it.
Example Workflow:
- This creates a new container from the
ubuntu
image. - Once inside, you can run Linux commands in the interactive shell.
- This creates a new container from the
2. docker exec -it my_nginx bash
Purpose: Used to execute a command inside an already running container.
Key Characteristics:
- It does not create a new container; instead, it attaches to an existing container (
my_nginx
in this case). - The
bash
command opens an interactive shell in the specified running container.
- It does not create a new container; instead, it attaches to an existing container (
Scenario: This is useful when you need to debug, explore, or interact with a running container without stopping or restarting it.
Example Workflow:
- This connects you to the terminal of the already-running
my_nginx
container.
SDK version of Commands that we learned till now:
Step 4: Managing Docker Images
1. Listing Images
View all images stored on your system:
Explanation of columns:
- REPOSITORY: The image's name (e.g.,
nginx
). - TAG: The version of the image (e.g.,
latest
). - IMAGE ID: A unique identifier for the image.
- CREATED: When the image was built.
- SIZE: Size of the image.
2. Removing an Image
Delete an image by its ID or name:
If the image is being used by any container, you’ll get an error. Use the -f
flag to force removal:
3. Pulling Images
Download an image from Docker Hub or another registry:
Example:
- If no tag is specified, Docker defaults to
:latest
.
4. Building a Custom Image
You can create your own image using a Dockerfile
:
- Create a file named
Dockerfile
with the following content: - Build the image:
Explanation of flags:
-t
: Assigns a tag/name to the image (my_custom_image
)..
: Specifies the build context (current directory).
5. Inspecting an Image
Get detailed metadata about an image:
This displays details such as layers, creation date, and configuration.
6. Saving and Loading Images
- Save an Image to a File:
- Load an Image from a File:
If you have multiple Dockerfiles in your folder and want to build an image based on a specific file, you should use the -f
(or --file
) flag with the docker build
command to specify which Dockerfile to use.
Syntax:
Example:
Assume your folder contains:
To build an image using Dockerfile.dev
:
To build an image using Dockerfile.prod
:
Explanation:
-t my-dev-image
: Tags the built image asmy-dev-image
-f Dockerfile.dev
: Specifies the Dockerfile to use.
: The build context (current directory)
While save
and load
are used for images, export
and import
work for containers:
Export a Running/Stoppable Container:
This saves the container’s filesystem as a
.tar
file.Import as a New Image:
Comments
Post a Comment