Getting Started with Docker: Exploring Container and Image Operations


In this blog post, we will dive into the basics of Docker container and image operations. We will explore essential commands, provide examples, and explain the concepts behind these operations. Whether you’re a beginner or looking to expand your Docker knowledge, this guide will equip you with the fundamental skills to work with Docker containers and images effectively. Docker containers are lightweight and isolated runtime environments that encapsulate applications and their dependencies. Let’s explore some basic container operations:

Container management operations

1- Create and Start a container from an image:

docker run –-name <container name> <image_name>:<version tag>
docker run --name container hello-world:latest

2- Stop a running container:

To stop a running container, you can use the docker stop command followed by the container ID or name. For example:

docker stop <container_id>

docker stop container

3- Restart a stopped container:
If a container has been stopped, you can restart it using the docker start command. Specify the container ID or name. For example:

docker start <container_id>
docker start container

4- View running containers:

docker ps

5- View all containers list
The docker ps command lists all the running containers on your system along with relevant information such as container ID, image used, and status.

docker ps –a

6- Inspect container details:
To get detailed information about a specific container, you can use the docker inspect command followed by the container ID or name. It provides details like network configuration, environment variables, and more.

docker logs <container_id>
docker logs container

7- Monitor container logs:

Use the docker logs command followed by the container ID or name to view the logs generated by a container. This is useful for troubleshooting or monitoring purposes.

docker logs <container_id>
docker logs container

Exploring Docker Images:

Docker images serve as the blueprints for creating containers. Let’s explore image operations:

1- Build an image from a Dockerfile:

Dockerfile is a text file that contains instructions to build a Docker image. The docker build command, followed by the -t flag and the desired image name, is used to build an image from a Dockerfile. For example:

docker build -t <my_image> .

mkdir mynewimage
cd mynewimage
vi dockerfile 
docker build -t my_custom_image .

This command builds an image with the tag my_custom_image using the Dockerfile located in the current directory (.).


The docker file created is from the base Ubuntu image and then over the base image the updates are installed along with the curl package. The resulting image has two more layers added from the base Ubuntu image.
Images and images files are a vast topic and require a separate post for explanation and exploration.

2- Pull an image from a registry:

If you want to use an existing image from a Docker registry, you can use the docker pull command followed by the image name. For example:

docker pull nginx

In the above example no tag is mentioned therefore the latest image by default is pulled from the docker registry.

3- Push an image to a registry:

Once you have built your own custom image, you can push it to a Docker registry for others to use. Use the docker push command followed by the image name. For example:

docker push <my_username>/<my_image>

This command pushes the image with the tag my_image to a registry associated with the username my_username.

4- View local images:

The docker images command lists all the images available locally on your system. It provides information such as image ID, repository, tag, and size.

docker images

5- Remove an unwanted image:

To remove an image from your local system, use the docker rmi command followed by the image ID or name. For example:

docker rmi <my_image>

docker rmi b539af69bc01

This command removes the image b539af69bc01 with short name (12 characters) given from the long sha256 name.

Working with Containers

Let’s explore additional operations to work with containers:

Execute commands inside a container

The docker exec command allows you to run a command inside a running container. Specify the container ID or name followed by the desired command. For example:

docker exec <my_container> ls

docker exec nginx_container ls

This command executes the ls command inside the nginx_container container.

Enter a shell session in a running container:

To access the shell prompt inside a running container, use the docker exec command with the -it flags and the container ID or name. For example:

docker exec -it <my_container> /bin/ash
docker run -it busybox /bin/ash

This command opens an interactive shell session inside the busybox container.

Allocate CPU and memory resources to a container:

Use the docker run command with the –cpu-shares and –memory flags to allocate CPU and memory resources to a container. For example:

docker run --cpu-shares=512 --memory=1g <my_image>

docker run --cpu-shares=512 --memory=1g busybox

This command limits the CPU shares to 512 and memory to 1GB for the container created from busybox image.

Update container environment variables:

To update environment variables of a running container, use the docker update command followed by the –env flag, the variable name, and its new value. For example:

docker update --env MY_VAR=new_value my_container

docker container run --name environment-image-example  -e ENV_EXAMPLE1=Environment -e ENV_EXAMPLE2=Example busybox:latest

This command updates the environment variable ENV_EXAMPLE1 and ENV_EXAMPLE2 to new values.

Map ports between the container and host:

Use the -p flag with the docker run command to map ports between the container and host. Specify the <host_port>:<container_port> format. For example:

docker run -p 8080:80 my_image
docker run -d --name nginx_portmapping -p 8080:80 nginx:latest

This command maps port 80 of the container to port 8080 on the host.

Image and Container Cleanup:

Let’s explore some commands to clean up unused images and containers:

Remove stopped containers in bulk:

The docker container prune command removes all stopped containers from your system, freeing up resources.

Clean up unused images:

Use the docker image prune command to remove unused images from your system. This helps reclaim disk space.

Purge all unused resources:

The docker system prune command cleans up all unused containers, networks, and images from your system.

Conclusion:

Docker containers and images form the backbone of modern application deployment and management. By mastering the basic operations covered in this blog post, you now have a solid foundation for working with Docker containers and images. As you explore further, you’ll discover additional commands and features that can enhance your containerization workflow. Remember, Docker provides a powerful and flexible environment for developing, testing, and deploying applications, making it an invaluable tool in today’s software development landscape.

Leave a comment