How To Manage Docker Containers & Images in Linux

Containerization is a technology that enables you to package your application and all its dependencies in a “container image”. The container image will often have base operating system, application files and folders, required libraries, volume mount points, environment variables and all application binaries.

You can have multiple containers running from the same image since container image is a template for execution. All containers will share same behavior, unlocking the powers of application scaling and distribution without worries of the underlying infrastructure. Container images can be stored in a local or remote registry for easy distribution.

A running instance of container is managed by a container runtime. There are multiple container runtimes, and the most popular is Docker Engine. Interaction with Docker container runtime is possible through the “docker” command.

Below are the three primary components of a container architecture; client, runtime and registry.

Docker Containers

Working With Docker Containers & Images in Linux

Now that we have all the basics required to work with Docker Containers let’s see commands to manage images and containers.

You’ll need installed and running Docker Engine for all these exercises.

Install Docker on Ubuntu:

sudo apt-get update
sudo apt-get install software-properties-common ca-certificates curl apt-transport-https gnupg-agent -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo usermod -aG docker $USER
newgrp docker

Install Docker on Debian:

sudo apt-get update
sudo apt-get install software-properties-common ca-certificates curl apt-transport-https gnupg-agent -y
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo usermod -aG docker $USER
newgrp docker

Install Docker on Fedora:

sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io -y
sudo systemctl start docker && sudo systemctl enable docker
sudo usermod -aG docker $USER
newgrp docker

Install Docker on CentOS:

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io -y
sudo systemctl start docker && sudo systemctl enable docker
sudo usermod -aG docker $USER
newgrp docker

Show the docker CLI version:

docker version

Working With Docker Images

Our first section will be on working with Docker Containers.

1. Build an image from Dockerfile

Command execution syntax is:

docker build -t [username/]<image-name>[:tag] <dockerfile-path>

This command will build an image called myimage from a Dockerfile located in current working directory:

docker build -t myimage:latest .

2. Push an image to registry

Log in to a Docker registry:

docker login  [registry]

The commands for pushing an image to registry are:

docker push [registry/][username/]<image-name>[:tag]

3. List the images

To list container images use the commands:

docker images

4. Tag and image

This is a command I’ll run to create an image called “myimage” with the tag “v1” for the image alpine:latest

docker tag alpine:latest myimage:v1

Then you can create a new image specifying the “new tag” from an existing image and tag:

docker tag <image-name>[:tag][username/] <new-image-name>.[:new-tag]

To create a new image with the latest tag:

docker tag <image-name> <new-image-name>

5. Remove an image from the local registry

The command to use is:

docker rmi [username/]<image-name>[:tag]

6. Exporting and importing an image to an external file

Export the image to an external file:

docker save -o <filename>.tar

Import an image from an external file:

docker load -i <filename>.tar

7. Check the history of an image

Check the history of myimage image

docker history myimage

Working With Docker Containers

Below are some helpful commands when working with Containers in Docker.

1. Run a container in interactive mode

Below command will run a bash shell inside an image:

docker run -it ubuntu bash 

2. Run a container in detached mode

Using -d runs the container is detached mode.

docker run --name mynginx -d -p 80:80 nginx:latest

3. Running detached container with mounted local folders inside the container

Running detached container with mounted folders:

docker run --name mynginx-volume -d \
  -p 80:80 \
  -v myfolder/:/usr/share/nginx/html/ \
  nginx:latest

4. Listing Containers

List all containers

docker ps -a

List only active containers:

docker ps

5. Stopping a container

Stop a container

docker stop [container-name|container-id]

Stop a container (timeout = 2 second):

docker stop -t1

6. Follow the logs of a specific container

Command usage:

$ docker logs -f [container-name|container-id]

# Example
$ docker logs -f mynginx

7. Run a detached container in a previously created container network

Commands:

docker network create mynetwork
docker run --name mynginx-net -d --net mynetwork -p 80:80 nginx:latest

8. Execute a new process in an existing container

Execute and access bash inside mynginx container:

docker exec -it mynginx bash

9. Removing Containers

Removing a stopped container:

docker rm [container-name|container-id]

Force stop and remove a container:

docker rm -f [container-name|container-id]

Remove all stopped containers:

docker rm $(docker ps -q -f "status=exited")

Remove all containers:

docker rm -f $(docker ps -aq)

10. Start an existing container

Start an existing container that was stopped called mynginx:

docker container start mynginx

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Related Post

Welcome to our today’s article, the intention of this article is to help you understand the installation and configuration of […]

Welcome to our tutorial on installing and using Metasploit Framework on Arch | Manjaro | Garuda Linux systems. The MSF […]

Arduino boards are making immense waves in the digital production world as they are used heavily in creation of digital […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.