Basic - Part 1
Notes from Docker & Kubernetes: The Paractice Guide [2022] By Maximilian Schwarzmüller
Images vs containers
An image can be described as a template/model, the BLUEPRINT, that contain the codes, tools and dependencies to be able run an application. Containers correspond to the running instances of images. Multiple containers can be run based on one single image.
Existing and pre-built Images
We can find pre-built images in the dockerhub. We can find official images with environment tied to multiple technologies and software.
Containers are isolated from one and each others. They are also isolated from our local machine.
Multiple containers instances can be build upon the same image.
Dockerizing a web app - Create a container for a NodeJS web app
In a development environment, we can create a Dockerfile to create an image which could be run as a container. This Dockerfile contains the configuration of our image.
The image below shows the folder containing our source code and Dockerfile. Using a container, take away the workload of installing NodeJS
and npm
on our system.
Running a Docker container
Then, in the terminal, we can run the following command to run the container based on the image created. The -p
flags is used to create a tunnel between the container port 3000 and the port 3000 of our localhost.
Listing Docker container
We can list all Docker containers running with the following command:
As seen below, Docker assigned automatically a name to the container we have started serene_leavitt
.
Stoping a Docker container
We can use the name attributed to the docker to stop the container.
Layer based architecture
Whenever we build an image, Docker cached every instructions. This cached results will be used by Docker when rebuilding an image if no change has been made. The image below show the cache instruction being used when rebuilding an image for the second time.
If changes are being made in an instruction, Docker will not used the cache and rebuild completely the layer of the instruction and the following.
Every instructions in a Dockerfile represent a layer.
Example of optimization
Copying the package.json
file and instruct RUN npm install
before copying the source code COPY . ./app
reduced the time need to build the image because npm
does not need to be reinstalled each time we make change in the source code.
Name and tags for images and containers
Images can be assigned a names and tags
name : tags
Names are used to specified a group of images while tags are used to specified an image within a group. The combination of a name and a tag create an unique identifier for the image.
We can assign a name and a tag to an image when building that image. For example:
Sharing images and containers
Sharing the Dockerfile and associated files of the project. The recipient need to build the image from the Dockerfile.
Sharing a built image. No build step required (recommended).
Share built-in image in DockerHub
Docker Hub or Private Registry.
The free tier of Docker Hub can be used to meet most basic needs. Registration is needed.
A public repository means that everyone can pull your image. The free tiers offers only 1 private repository.
Step 1: Create a repository
The image we want to push need to share the same name as the repository!
Step 2: Rename the image (if needed)
We can rename our image with the following command:
We need to rename our image amandinegh/nodejs-hello-world
. Note that renaming an image create a clone of that image.
Step 3: Login to DockerHub
To be able to push an image on DockerHub, we need to login
Step 4: Push the image on DockerHub
Download an image
To download an image from DockerHub, we can pull an image using this command:
Note that without any tag, the latest version will be pulled.
Last updated