Docker Series (Chapter 2/4): Creating and Using containers
For information on how to install Docker, please refer to: Docker Series (Chapter 0): Installing docker-engine in Ubuntu.
For guidance on how to create and use Docker images, please refer to: Docker Series (Chapter 1): Building and using images.
Alright, let’s continue learning the following Chapter.
Best Practices for Building Dockerfiles
Containers should be ephemeral/temporary.
Avoid installing unnecessary packages.
Each container should have only one purpose.
Avoid having too many layers in a container.
Multi-line sorting.
Leveraging cache.
Let me list an example here.
1 | # Base image |
Let’s use the Dockerfile to create a new image here.
1 | shy@flash-shy:/HDD/learn/docker/apache2$ docker build -t web:v1 . |
After building the iamge called: web:v1, let’s check the list of images.
1 | shy@flash-shy:/HDD/learn/docker/apache2$ docker images |
Best Practices for naming the image
Obviously, We successfully creating a new images about 7 seconds ago. The image’s name is web and its tag is v1. Its size is about 148MB.
So, what is the reason we could quickly find the image we want?
Image naming format
REPOSITORY+TAG
. It is recommended to use version number
as the naming convention.
A simple and clear image naming format allows users to quickly identify the image they need without the need for testing.
Explanation of the ‘latest’ tag and its usage
If no tag
is specified when building the image, the default latest
tag will be used.
Therefore, when you see ‘latest’ as the tag of an image, it does not necessarily mean that this is the latest version. It simply means that no tag was specified when the image was created, and nothing more.
1 | shy@flash-shy:/HDD/learn/docker/apache2$ docker build -t web . |
It is visibly that each period of creating the same image is zero
.
Here is the function of cache
. Because I have already creating the same image before, it may reuse the cache this time.
Therefore, it will dramatically shorten the creating time.
1 | shy@flash-shy:/HDD/learn/docker/apache2$ docker images |
We could find there are two images, both of them are called web and the image id are the same. The difference is tag
, this time I did not add the tag after the image name.
Therefore, the default tag is latest
.
Basic commands about images
docker build -t image_name:tag_name .
1 | docker build -t web:v1 . |
docker commit container_name image_name:tag_name
1 | docker commit web web:v2 |
docker save image_name:tag_name -o compression_name.tar
1 | docker save web:v2 -o web_v2.tar |
docker load -i compression_name
1 | scp web_v2.tar user@ip:/path/to/images |
2 Methods of creating containers with image
As we discuss in Docker Series (Chapter 1): Building and using images, there are two methods to create a container by using an image.
docker run
docker run is a basic Docker command used to directly start a new container. It is ideal for running a container individually to perform simple tasks or experiments.
1
docker run -d -p 8080:80 --name my-container nginx
docker compose
docker-compose is a tool used to define and manage
multi-container
Docker applications.It configures services, networks, volumes, and more through a
docker-compose.yaml
file. docker-compose is ideal for managing applications made up of multiple containers, simplifying the configuration and startup of containers.It is used for one-click start, stop, and management of multiple containers.
So,
docker-compose
builds on top ofdocker run
, providing a more efficient and manageable way to handle multiple containers. It is especially useful for managing microservices architectures in development, testing, and production environments.
Feature | docker run | docker-compose |
---|---|---|
Use Case | Single container, simple tasks or testing | Multiple containers, complex applications, service dependencies management |
Configuration Method | Configured through command-line arguments | Defined through the docker-compose.yml file with multiple containers, networks, volumes, etc. |
Start Multiple Containers | Need to start each container individually | Use docker-compose up to start all containers at once |
Service Dependency Management | Manually manage dependencies between containers | Define dependencies between containers in the docker-compose.yml file |
Port Mapping | Manually map ports, set individually for each container | Unified port mapping management for multiple containers in the docker-compose.yml file |
File Management | No file configuration, relies on command-line arguments | Uses docker-compose.yml file for centralized configuration and service management |