Docker’s Reference: The Ultimate Guide
This article is intended to be a brief description of the most used commands in Docker and understand the different concepts.
What is Docker?
Docker is a tool that enables you to quickly create, test, and deploy containerized applications.
What is a container?
Containers are running instances of an image. They are the ones who run things, the ones who will run our application. The container concept is like restoring a virtual machine from a snapshot.
From a single image, we can run multiple containers.
What is an image?
We can say that an image is a package that contains everything necessary for an application/service to run.
Images are used to create containers, and they never change.
There are many public images with basic elements like Java, Ubuntu, Apache, Mongo … etc, that can be downloaded and used. Normally when you create images, we start from a parent image to which we add things.
What is a volume?
Volumes in Docker is the simple and predefined way to store all files in a container.
It is not a good practice to save persistent data inside a Docker container. That’s what volumes are for, outside of containers. So we can create and delete containers without worrying about the data being deleted.
What is a network?
Networks in Docker will allow you to relate different containers to each other or to isolate them completely.
The most used commands are:
Know all the commands to use about an image:
docker image --help
Download an image. If you want to download a specific version, you need to add the image version if not, you just need to indicate which image you want to download:
docker pull mongo -> latest mongo version
docker pull mongo:3.6 -> 3.6 mongo version
Build your own image from a previously created Dockerfile (Let’s assume that our Dockerfile will display a mongo image):
docker build --tag mongo .
Find all images that start with a word or character. For example “mongo”.
docker images | grep mongo
Delete the image, here we have 2 options:
docker rmi mongo -> Delete the image with "mongo" name.
docker rmi [IMAGE_ID] --> Delete the image with "image_id" image.
To know all the commands to use about a container:
docker container --help
Create a simple container (where -d → Run the container in daemon mode):
docker run -d mongo
Create a simple container where it is indicated container name:
docker run -d --name containerName mongo
Create a simple container where it is indicated the port:
docker run -d -p internPort:externalPort --name containerName mongo
Add environment variables by command line to create a simple container (where -e → Run the container with environment variables):
docker run -d -e "test=123" --name containerName mongo
Get all the containers with the status “run”, type the command:
docker ps
Get all containers (actives and inactive):
docker ps -a
Remove the containers, there are different options:
1. Remove a container: docker rm -f containerName
2. Remove some containers: docker rm -f containerName containerName1
3. Remove all containers: docker rm -fv $(docker ps -aq)
Rename the container:
docker rename containerName containerNewName
Stop the container:
docker stop containerName
Stop all containers (active and inactive):
docker stop $(docker ps -aq)
Start a container:
docker start containerName
Restart a container:
docker restart containerName
Know the resources used by a container:
docker stats containerName
Perform actions to limit and increase the resources of a container, we can use these commands:
- Limit or increase the memory that a container can use (Where -m → It means memory limit):
docker run -d -m "limitMemory" --name containerName mongo
- How many CPUs does it have in our machine?:
grep "model name" /proc/cpuinfo | wc -l
- Limit or increase the number of CPUs to use:
docker run -d --cpuset-cpus 0-1 --name containerName mongo
Inspect a container, it means to get all information about the container:
docker inspect containerName
Copy files to a container:
- From our local to the container. Copy indext.html file from our local to /tmp/ folder in Apache container:
docker cp index.html apache:/tmp/
- From the container to our local. Copy the file test.log from the container to the current directory of our machine:
docker cp apache:/tmp/test.log .
Know all the commands to use about volumes:
docker volumes --help
Get all volumes created:
docker volume ls
Create a simple volume:
docker volume create volumeName
Delete created volumes:
docker volume rm volumeName
Display detailed information on one or more volumes:
docker volume inspect volumeName
Remove dangling volumes. It means volumes that don’t reference any container:
docker volume ls -f dangling = true | xargs docker volume rm
Know all the commands to use about the network:
docker network --help
Get all network created:
docker network ls
Get specific information from a network:
docker inspect networkName
Filter by a specific network:
docker network ls | grep [NETWORK_TYPE]
Create a simple network:
docker network create networkName
Create a specific network (where “bridge” is the network):
docker network create -d bridge --subnet 172.124.10.0/24 --gateway 172.124.10.1 networkName2
Create a container connected to a specific network:
docker run -d --network networkName2 mongo
How to connect two containers on the same network?
1. docker run -d --network networkName2 --name containerName mongo
2. docker run -d --network networkName2 --name containerName2 mongo
Delete network:
- Before deleting a network, make sure that no container is using it. This command can help you:
docker rm -fv $(docker ps -aq)
2. Delete the network:
docker network rm networkName
And that’s it!!, I hope it was helpful to clarify concepts -:-)