Working with Docker on Ubuntu 20.04
Working with Docker on Ubuntu 20.04

Working with Docker Images

Docker image is a read-only template file with instructions for creating a Docker container. You can either create your custom images or you can only use those created by others and published in the Docker Hub, the world’s largest library and community for container images.

You can search for an ubuntu image in the Docker Hub with the following command:

docker search ubuntu

To download an image locally, use the pull command. This example shows how to download the official ubuntu image.

docker pull ubuntu

Once the download is complete, you can list the available images on your local system.

docker images

If you no longer need an image, you can remove it from your system.

docker rmi ubuntu

Running and Managing Docker Containers

To start a container based on your new ubuntu image, run the following command where “ubuntu” is the local image name 

docker run ubuntu

To list Docker containers, use the docker ps command as follows. Use the -l flag to show the latest created container in all states:

docker ps
or
docker ps -l

To show all containers including those that have exited, use the -a flag.

docker ps -a

Docker also allows you to assign a name to a container using the --name option when running it.

docker run --name my_ubuntu ubuntu

Now you can use the container name to manage (start, stop, stats, remove, etc.) the container:

docker stop my_ubuntu
docker start my_ubuntu
docker stats my_ubuntu
docker rm my_ubuntu

you can stop a running container from the host session by running the following command:

docker kill my_ubuntu

That’s all!