Hands-on: Part 2 COMMANDING DOCKER
In this hands-on exercise, we will dive into some of the most essential Docker commands. Docker is a versatile tool for containerizing applications, making it easier to build, ship, and run them consistently across various environments. With these commands, you’ll have the ability to effectively manage containers, images, volumes, and networks.
Starting the AWS EC2 Instance Running Docker
To begin, access your AWS account, navigate to EC2, and start the virtual machine (VM) that has been set up for Docker usage.
Getting Started
Let’s walk through the steps to get started with this exercise:
Verify Docker Installation
Ensure that Docker is installed correctly by running the following command:
docker - version
Pulling an Image:
Pull an image from a Docker registry. For instance, let’s grab the nginx
image from DockerHub and download it to our local machine:
docker pull nginx
Confirm that the image was successfully downloaded using the following command, which provides information about your Docker images, including the repository, tag, image ID, and size:
docker images
Creating a Container:
Create a Docker container named my-nginx
using the following command:
docker create - name my-nginx nginx
Starting the Container:
Initiate the container you’ve just created with the command:
docker start my-nginx
Listing Running Containers:
Check the list of currently running containers:
docker ps
Listing All Containers:
To see all containers, regardless of their status, use:
docker ps -a
Filtering Containers by Status:
You can filter and list containers by their status. For instance, to list all containers that have exited, run:
docker ps -f status=exited
Stopping a Running Container:
To stop a running container, replace {container_name}
with the actual name or ID of the container:
docker stop {container_name}
Removing a Stopped Container:
If you need to remove a stopped container, use:
docker rm {container_name}
Forcefully Stopping and Removing a Container:
For situations requiring a forceful stop and removal of a container, you can achieve it with:
docker kill {container_name}
Listing Images:
To display a list of all Docker images, execute:
docker images
Removing an Image:
If you need to remove an image, replace {image_id}
with the actual ID, tag, or digest of the image:
docker rmi {image_id}
Running an Interactive Container:
Run a container and attach your terminal’s standard input, output, and error streams to it:
docker run -it - name interactive-nginx nginx /bin/bash
Take note as the console environment changes from our EC2 user environment to the docker interaction bash shell with a root user. To exit from the docker console just type in the exit
and press enter from your keyboard.
Running a Detached Container:
Execute a container in detached mode using the command below. This allows the docker container to run in the background while freeing the console so that you can perform another task. Normally containers will be launched to run in the background we will always launch container instances in a detached mode.
docker run -d - name detached-nginx nginx
Executing a Command in a Running Container:
To run a command within a running container, opening a bash shell inside it, you can use the following:
docker exec -it {container_name} bash
Remember to replace {container_name}
and {image_id}
with your specific container names and image IDs. Keep in mind that running a container in detached mode means it operates in the background, while running it in attached or interactive mode places it in the foreground, allowing direct interaction from your terminal.