Installing Docker on Ubuntu
To install Docker on Ubuntu, follow these steps:
- Remove existing Docker installations:
$ sudo apt-get remove docker docker-engine docker.io
- Install Docker:
$ sudo apt install docker.io
- Set up dependencies:
$ sudo snap install docker
- Test the installation:
$ sudo docker run hello-world
- View downloaded images:
$ sudo docker images
- Check running containers:
$ sudo docker ps
- To use Docker without 'sudo', add user to Docker group:
$ sudo usermod -aG docker $USER
Note: Reboot your system to complete the setup.
Pulling an Ubuntu Image
To download an Ubuntu image, you have several options:
- Latest version:
$ docker pull ubuntu
- Specific version (e.g., 20.04):
$ docker pull ubuntu:20.04
- Pull by digest for unchanging image:
$ docker pull ubuntu@sha256:<digest>
- Pull from different registries:
$ docker pull myregistry.local:5000/ubuntu
After pulling, view downloaded images with:
$ docker images
Running and Interacting with Containers
To run and interact with an Ubuntu container:
- Launch interactive terminal session:
$ docker run -ti --rm ubuntu /bin/bash
- Inside the container, check OS information:
root@container_id:/# cat /etc/os-release
- Create persistent storage link:
$ docker run -ti --rm -v ~/Docker_Share:/data ubuntu /bin/bash
This mounts a local directory (~/Docker_Share
) to /data
in the container.
Managing Docker Images and Containers
Efficient management of Docker images and containers involves several key commands:
Action | Command |
---|---|
List images | $ docker images |
View all containers | $ docker ps -a |
Label images | $ docker tag original-image-name:tag new-image-name:new-tag |
Create new image from running container | $ docker commit container_id new-image-name |
Remove unused images | $ docker rmi image_id |
Delete unnecessary containers | $ docker rm container_id |
For bulk removal, use these commands:
$ docker system prune
$ docker image prune
$ docker volume prune
By following these steps, you can effectively use Docker's capabilities to enhance your development environment. Remember that Docker's flexibility allows for efficient management of both images and containers, enabling a streamlined workflow for developers and system administrators alike.
- Docker Documentation. Docker Engine installation overview. Docker Inc.
- Ubuntu Documentation. Docker. Canonical Ltd.
- Mouat N. Using Docker: Developing and Deploying Software with Containers. O'Reilly Media; 2015.