In this post we will be listing and exploring the commands and basic approach to get started build container images with Docker.
I am using Google Cloud Shell for this blog but you just installed Docker on your local machine.
Lets go through each keyword in brief:
Container Images: Container image is the package of the actual code and all its dependencies. This makes it easy to be executed on any environments/OS etc.
Example, if you have a nodeJs file say app.js. In order to run the app.js, you will need node version to be installed on the machine. Similarly, in case of a container Image, it will first have a node installation -> your code i.e. app.js in a single package.
Container: Container Images become container when they are running on the platform example, Docker/Kubernetes etc.
Registry: Once you build the image on the local machine, you can check in the image in to Container Registry. This allow it to be pulled from multiple platforms and make it sharable.
Ref in case you need more info: https://www.docker.com/resources/what-container/
Below are some frequently used docker commands:
docker run hello-world | To test if the docker installation is proper. |
docker images | List all the images on the local machine |
docker ps | List all the container processes running |
docker ps -a | List all the past executed container processes |
docker build | Build the container Image based on the filename "Dockerfile"
Example: docker build -t node-app:0.1
-t indicate tag name [TagName]:[Version] |
docker run | Run the container image. Example:
If you have NodeJS listener you can run it using below command
docker run -p 4000:80 --name my-app -d node-app:0.1
4000:80 -> Indicate the mapping of the port between the local machine and the docker container. In this case the Node will run on default port 80. In order to call the listener from the local machine, we are mapping 4000 -> 80
Thus our URL will be http://localhost:4000 which will call Container listener on http://localhost:80
--name is to name the container
-d is to run the container in background |
docker ps | You can use it to also get the container ID |
docker logs [containerID] | To get the container logs docker logs [] |
docker logs -f [container_id] | You can use -f in case you want to tail on the container logs |
docker exec -it [container_id] bash | exec command can be used if you want to SSH in the docker container.
This will allow you to actually explore the files with the container instances |
docker inspect [container_id] | Container's metadata in Docker by using Docker inspect
This will return a JSON formated output |
docker tag | You can use this to tag existing Image which another Tag. This is needed is you planning to publish images on container registry |
Steps to publish images on GCP registry
1. You will need to tag an image with below format [hostname]/[project-id]/[image]:[tag] For gcr:
[hostname]= gcr.io [project-id]= your project's ID [image]= your image name [tag]= any string tag of your choice. If unspecified, it defaults to "latest".
Example:
docker tag node-app:0.2 gcr.io/[project-id]/node-app:0.2
docker push gcr.io/[project-id]/node-app:0.2
|
docker stop [Container ID] | To stop the running container |
docker rm [container ID] | To remove the container instance |
docker rmi [image id] | To remove the images. Image ID can be fetched using docker images |
docker pull | To pull the image from the remote directory
example to pull and run:
docker pull gcr.io/[project-id]/node-app:0.2 docker run -p 4000:80 -d gcr.io/[project-id]/node-app:0.2 curl http://localhost:4000 |
If you want to perform an hands on steps, you can refer below document.
https://www.cloudskillsboost.google/focuses/1029?catalog_rank=%7B%22rank%22%3A1%2C%22num_filters%22%3A0%2C%22has_search%22%3Atrue%7D&parent=catalog&search_id=17839779