8.1 Tagging in Docker
Tagging in Docker is the process of assigning a label to an image, which makes version management easier. This makes deploying and updating applications more convenient and manageable. In this lecture, we’ll dive into what tags are, how to use them, and some recommendations to help you work with them more effectively.
Tag in Docker is a label that allows you to identify a specific version of an image. Tags make it easier to track changes and help you choose the right versions for different environments like development, testing, and production.
Main concepts of tags:
- Uniqueness: A tag uniquely identifies a specific version of an image.
- Versioning: Tags are used to indicate image versions, for example, v1.0, v2.0, latest.
- Flexibility: Tags are used to create images corresponding to specific development stages, such as
beta,stable,prod.
8.2 How to Use Tags
1. Assigning Tags When Building an Image
You can assign a tag to an image when building it using the -t parameter of the docker build command.
Example:
In this example, the image is assigned the name myapp and the tag 1.0.
docker build -t myapp:1.0 .
2. Assigning Additional Tags to an Existing Image
You can add tags to an already existing image using the docker tag command.
Example:
This example assigns an additional latest tag to the myapp:1.0 image.
docker tag myapp:1.0 myapp:latest
3. Using Tags When Running Containers
When you run a container, you can specify the specific tag of the image you want to use.
Example:
This example runs a container based on the myapp image with the 1.0 tag.
docker run -d myapp:1.0
8.3 Tagging Practice
Using Semantic Versioning
Semantic Versioning is a standard versioning practice that helps you understand the level of changes in an image.
Semantic Versioning Format:
<major>.<minor>.<patch>
Where:
-
major: Major changes, incompatible with previous versions. -
minor: New features, compatible with previous versions. -
patch: Bug fixes and other minor changes.
Example:
docker build -t myapp:2.1.3 .
Using Additional Labels
Additional labels help indicate the state of an image, like beta, alpha, stable, or prod.
Example:
docker build -t myapp:1.0-beta .
docker build -t myapp:1.0-stable .
Updating Tags
When updating an image, it is recommended to change tags for easier tracking of changes. The latest tag is often used to indicate the latest version of an image.
Example:
docker build -t myapp:2.0 .
docker tag myapp:2.0 myapp:latest
8.4 Examples of Using Tags
Examples of using tags in different environments:
Example 1: Development
For development, images with tags indicating the current version or development state are often used, for instance, dev.
docker build -t myapp:dev .
docker run -d myapp:dev
Example 2: Testing
In test environments, images with tags that indicate specific versions or states, such as beta, are used.
docker build -t myapp:1.1-beta .
docker run -d myapp:1.1-beta
Example 3: Production
In production environments, it's important to use stable and verified image versions tagged as stable, prod, or semantic versions like 1.1.0-stable.
docker build -t myapp:1.1.0-stable .
docker run -d myapp:1.1.0-stable
8.5 Best Practices
1. Always use Semantic Versioning
Stick to semantic versioning for all images. This helps to clearly distinguish levels of changes and makes version management easier.
2. Use clear and meaningful tags
Use tags that clearly reflect the state or purpose of the image (for example, beta, stable, prod).
3. Avoid using latest in production
The latest tag can be useful for development and testing, but in production, it's better to use specific versions to avoid unexpected changes.
4. Document tag usage
Document how and for what tags are used in your project. This will help the team better understand the versioning process and reduce the likelihood of errors.
Examples of Dockerfile with tags
Example 1: Creating and assigning tags
# Dockerfile for version 1.0.0
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
Commands for building and tagging
docker build -t myapp:1.0.0 .
docker tag myapp:1.0.0 myapp:stable
Example 2: Updating the image and tagging
# Dockerfile for version 1.1.0 with a new feature
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN rm -rf /app/tests /app/docs
ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "app.js"]
Commands for building and tagging
docker build -t myapp:1.1.0 .
docker tag myapp:1.1.0 myapp:latest
GO TO FULL VERSION