Docker's default registry is called dockerhub and it contains all container images similar to github. We can pull existing public images from docker-hub and can push our customized images to docker registry (it will be available for everyone).
Docker image:
Docker image is a blueprint of an application. Docker images are basis of containers. We can search docker image in registry using docker search command.
docker search <image-name>
docker images can be classified 4 types.
- Base images
- It has no parent images. Usually it is like OS images Ex: ubuntu, python
- Child Images
- It is build based on parent images with additional functionality
- Official images
- It is created and maintained by docker folks
- User images
- It is custom images created by users. Normally it will be formatted like
- <username>/<imagename>,
- ex: manojnuk50/flask-img
docker pull <image-name>
docker pull ubuntu:12.04
It will pull the ubuntu images from docker registry and store it your local system. 12.04 specifies the tag information. if not specifies the tag name it will download the latest images from registry.
What is unbuild images?
Onbuild images are having multiple triggers. When we use unbuild image it will do some automate stuff for us. For example python inbuild image will do following steps while we creating containers
Image name: python:3-onbuild
- It will automatically copy requirements.txt file from current directory to container
- Run requirements.txt using pip install cmd
- Copy current directory files to /usr/src/app in container
Dockerfile:
A Dockerfile is a simple text-file that contains a list of commands that the Docker client calls while creating an image. It's a simple way to automate the image creation process.
Sample Docker file:
# base image
FROM python:3-onbuild
# specify the port that container should expos
EXPOSE 5000
# run the application
CMD ["python", "./app.py"]
FROM command specify the base image name. And expose 5000 specifies the port which need to expose
to outside the world.
CMD command specifies the command which needs to be execute when running the image in the container.
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect
Docker hello world:
Execute following hello-world docker command to check docker is installed properly in your machine.
docker container run hello-world
It will download hello-world image from docker hub and pack it into container and then run the image. So run command create new container for every time.
Creating sample flask app using docker
Now we are going to create sample python flask hello world app using docker. First create a new folder and place following files into that folder. Refer this github repo
mkdir flask-helloworld
cd flask-helloworld
requirements.txt
app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
Dockerfile
# base image
FROM python:3-onbuild
# specify the port that container should expos
EXPOSE 5000
# run the application
CMD ["python", "./app.py"]
Build docker image:
Following command read your Dockerfile and build your custom images. Go to the path where your Docker file locates and execute following command.
docker build -t <imagename>:<tag-name> <path of your dockerfile>
docker build -t sample-app:v1 .
final dot(.) specifies the current directory. -t specifies the tag name of the image
Listing docker images:
docker images
docker images -a
Running Docker container:
Docker run command is used to run a image inside the container. If the specified image available in local machine docker will take it from local or it will download from dockerhub and then store it to local machine.
docker container run <image-name>
docker container run sample-app:v1
It will create a new container and run the sample-app image inside the container.
If you want to execute the container in background use --detach (or) -d flag. It will detach the process from foreground and allow us to execute it into background. It will return the unique container id.
docker container run -p 5000:5000 -d sample-app:v1
Executing commands inside the container:
Following command allow us to login inside the container. It is very helpful to debug our application if something went wrong. we can execute linux commands inside the container
docker run -it <image-name> sh
docker run -it sample-app:v1 sh
List out available containers:
you can see the list of containers in your local machine using following command
docker container ls
Flags:
docker container run -p 5000:5000 -d --name flask-helloworld sample-app:v1
docker flags are used to specify the specific action or behavior of the container while running
- --name
- it specifies the name of the container
- if we are not specifying the container name it will assign random name
- --detach (or) -d
- it will detach the process from foreground and execute it into background process
- docker container run -p 80:80 -d hello-world
- --publish (or) -p
- it specifies the port number need to be exposed from container to host machine
- docker container run -p 80:80 hello-world
- if you are not specifying the port while running command it will automatically assign random ports
docker port <container-name/id>
it will show all the ports used by the container
Docker logs:
docker log command used see the logs generated by containers while executing application images. basically it will be stdout results. following commands are used to play with container logs.
docker logs 9a425901d134
It will show the entire logs results generated by sample-host container.
docker logs --tail 20 9a425901d134
It will show the entire logs results generated by sample-host container.
docker container top 9a425901d134
It will show the top processes running inside the container. (similar to linux top command)
Stop the container:
Following command used to stop the container.
docker container stop 9a425901d134
Deleting containers:
Every run docker creating new containers so it will eat disk space, so best practice is cleanup the containers once done with that.
docker container rm <container id>
docker container rm 9a4
docker rm $(docker ps -a -q -f status=exited)
- Above command will delete all the containers created in local machine
- -q only returns numeric Id’s
- -f filters output based on status
- first 3 character of container id is enough to mention while deleting. it will be unique always
Deleting Images:
docker rmi <image-id/name>
docker rmi $(docker images -a -q)
Conclusion:
In this part, I have convert some basic commands from docker. We will see more advanced concepts in next part.