Docker is an open platform for developing, shipping, and running applications. It is the standard de facto for packaging and sharing apps – from desktop to the cloud.
To achieve its goal, Docker is using containers.
The container has the following characteristics:
- A container is a package for the software and all required libraries.
- As soon as is launched, a container determines a runtime environment for your application.
- A container is lighter than a virtual machine, so that it can be launched way faster.
- A container is distributed in the form of an image, which consists of multiple layers. Multiple containers can reuse these layers.
The features above allow you to:
- Build isolated environments for deploying.
- Launch your applications in minutes.
Docker helps you to simplify and automate every step of the software development process.
Here are the essential Docker features:
- Docker has a giant community.
- Docker is a simple and lightweight way of distributing software.
- Docker has a giant public repository of containers for all possible purposes.
- Docker lowers dev and ops costs.
- Docker configuration is straightforward and quick.
- Docker provides application isolation.
- Docker image has layers, which has version control.
- Docker automates every step of container management.
You can install Docker on Linux, macOS, and Windows operating systems.
docker container run [your-image-name]
You can use the option --rm
with Docker run command to removes a container after it exits.
You can use the option -it
with Docker run
command to create and start a container and attach to the interactive shell.
docker container run -it [your-image-here] [shell-path-here (ie: /bin/bash)]
You can use the option -td
with the Docker run
command to create, start the container and keep it running.
docker container run -td [your-image-here]
You can use the option --name
with Docker run
command to assign a container name using the following syntax:
docker container run --name [container-name] -td [image-name]
If you want to access the external machine’s container process, you can expose a container port to the external network.
In this case, you can use the option -p
with the Docker command to expose the specific port as shown below:
docker container run -p [host-port]:[container-port] --name [container-name] -dt [image-name]
To list all running and stopped container in your system, run the following command:
docker ps -a
To list only running container in your system, run the following command:
docker ps
or
docker container ls
To display the live statistics of the running container (CPU and memory utilization, network and disk I/O) named apacheweb
, run the following command:
docker stats apacheweb
To list all running processes inside the running container named apacheweb
, run the following command:
docker top apacheweb
To display the logs of the running container named apacheweb
, run the following command:
docker logs apacheweb
To stop the running container named apacheweb
, run the following command:
docker container stop apacheweb
To start the container named apacheweb
, run the following command:
docker container start apacheweb
To pause the container named apacheweb
, run the following command:
docker container pause apacheweb
To restart the running container named apacheweb
, run the following command:
docker container restart apacheweb
To kill the running container named apacheweb
, run the following command:
docker container kill apacheweb
To connect to the running apacheweb
container, run the following command:
docker container exec -it apacheweb /bin/bash
To run any command inside the running container, run the following command:
docker container exec -it apacheweb ls
In this section, we will show you some most commonly used commands to manage the Docker image.
To download or pull the image from the Docker registry, use the following syntax:
docker pull [image-name]
If you want to upload an existing image to the Docker registry, use the following syntax:
docker push [image-name]
Login to Docker The Registry First, you will need to login into the Docker registry with the following command:
docker login
Once login, run the following command to get the id of the image and tag the image with the following command:
docker images
Add Tag To Docker Image Next, tag the image with the following command:
docker tag f35646e83998 amaksimov/nginx
Where:
f35646e83998
is the image id,amaksimov
is the Docker registry name andnginx
is the name of the image.
Next, push the Nginx
image to the Docker registry with the following command:
docker push amaksimov/nginx
You can also create an image from an existing container using the following syntax:
docker commit [container-name] [new-image-name:tag]
For example, create an image from the Apache container, run the following command:
docker commit apacheweb apache-image
You can also add additional information and tag the image
docker commit --author="fredjoseph" --message='Fix some configuration files' 46fbc5390f39 my-custom-image:1.0.0
Save an existing image to a tar
archive by running the following command:
docker save apache-image > apache-image.tar
docker save my-custom-image:1.0.0 | gzip > my-custom-image.tar.gz
You can print the history of any Docker image with the following command:
docker history apache-image
You can remove any Docker image using the following syntax:
docker rmi [image-name]
For example, if you want to remove the Apache
image, you will need to stop the container that is using the httpd
image:
docker container stop apacheweb
docker rm apacheweb
First, we need to delete apache-image
image:
docker rmi apache-image
Only afterwards, we remove the httpd
image with the following command:
docker rmi httpd
When you create a new container, store some data and delete the container then data will be lost. In this case, you can create a volume on the host system and start a container using this volume. After deleting the container, you can retrieve the data from the volume. You can also use Docker volume to share the data between multiple containers.
To create a new volume named datavolume
with the following command:
docker volume create datavolume
To list your created volume, run the following command:
docker volume ls
To print more information about volume, run the following command:
docker inspect datavolume
To create a new container named apacheweb
and mount the datavolume
on the host system to the /mnt
directory to the container, run the following command:
docker run -it --name apacheweb1 --mount source=datavolume,destination=/mnt -td httpd
To remove the volume, run the following command:
docker volume rm datavolume
To delete all unused docker images, you need to run the following command:
docker rmi $(docker images -a -q)
All exited Docker containers could be removed by running the following command:
docker rm $(docker ps -a -f status=exited -q)
To stop all Docker containers, you need to run:
docker stop $(docker ps -a -q)
To delete all stopped Docker containers, you need to run:
docker rm $(docker ps -a -q)
To delete all unused Docker resources, you may use the following command:
docker system prune
To delete all Docker resources completely (used and unused), run the following command:
docker system prune -a
On Windows, Docker runs in a VM called MobyLinuxVM
, but you cannot login to that VM via Hyper-V Manager.
We aren’t technically going to SSH into the VM, we’ll create a container that has full root access and then access the file system from there.
Just run this from your CLI and it'll drop you in a container (https://github.com/justincormack/nsenter1) with full permissions on the Moby VM. Only works for Moby Linux VM (doesn't work for Windows Containers).
docker run -it --rm --privileged --pid=host justincormack/nsenter1
For clearing all logs for all containers
truncate -s 0 /var/lib/docker/containers/*/*-json.log
For clearing logs for a specific container
truncate -s 0 /var/lib/docker/containers/<containerId>/<containerId>-json.log