Docker with basic examples
Posted on March 8, 2023 • 5 minutes • 893 words
What is Docker ?
Docker :
- It is a open platform to deliver your application faster to production.
- It helps in developing, running, testing, shipping the same environment without worrying about the host infrastructure.
- Advantages:
- Suitable for progressive deployment
- Dynamic workload management and scaling
- Cost efficient as compared to VMs(Virtual Machines)
- Open platform to use and to contribute
- Environment isolation
- Easy to share
Components of Docker
Docker Daemon:
- Listens to all the Docker API requests coming in the form of commands.
- Manages Docker objects like images, containers, networks and volumnes.
- It also communicate with other docker-daemons to manage the docker-service.
Docker Client:
- It is the primary way with which user can interact with docker-daemon.
- Listens to the commands like
docker run
,docker build
(via Docker APIs) and sends it to docker-daemon. - This client can communicate to multiple docker-daemons at the same time.
Docker Registry:
- The place to store the docker images is called docker registry.
- Docker Hub is the public registry available for everyone to access.
- You can
docker pull
the docker-image anddocker push
your docker-image to the registry.
Docker Objects:
Images:
- It is a read-only template which defines the environment you want to set for your application.
- Created with the file called
Dockerfile
(name should be the same). - Each instruction in Dockerfile is a layer. Other images can also be used as layers.
- When you update and rebuild an image, only the layer which changed gets rebuilt.
Containers:
- These are the runnable instance of docker-images.
- Controlled via commands like
start
,stop
,create
etc using docker-APIs or CLI. - Containers can connect to one or more networks and can attach storage in the form of volumnes.
- All the containers are isolated from each other and its host machine too.
Two ways to create a container:
Note: Install the docker : https://docs.docker.com/get-docker/ before you go further.
Let’s say we want to create a container with python installed.
1. With pre-configured image from Docker-Registry:
- Get the docker-image which contains python pre-installed and pull it from Registry( Docker-Hub : https://hub.docker.com/_/python
).
Use the command
docker pull
:
☺ docker pull python
Using default tag: latest
latest: Pulling from library/python
Digest: sha256:d3c16df33787f3d03b2e096037f6deb3c1c5fc92c57994a7d6f2de018de01a6b
Status: Image is up to date for python:latest
docker.io/library/python:latestdocker run -dt python
- Verify if the image is present locally by running
docker images
:
☺ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest 8cad8eec9ec5 4 days ago 875MB
- Create a container for this image using
docker run [Options] Image
:
Options:
-d, --detach=false
Run container in background & print ID
-t, --tty=false
Allocate a pseudo-TTY
☺ docker run -dt python
0311adddf2e1b83d2a1e8f4b1bf7d1e189b61f22fab62ff74f32ab61ef8c6752
- Verify if the container is created with
docker ps
:
☺ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0311adddf2e1 python "python3" 6 seconds ago Up 5 seconds
-
Finally, you have a container running locally on your machine with the image pulled from registry.
-
Let’s verify if it is actually having python inside. For that, let’s print
hello world
inside the container. -
Add the following in the file
main.py
:
print("hello world")
-
Run the following commands to copy this file to the container and then run the python file from the container:
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
docker cp main.py c9e35fd2e994:main.py
docker exec -it [CONTAINER] command python_file
-i
,--interactive
Keep STDIN open even if not attached
-t
,--tty
Allocate a pseudo-TTYdocker exec -it c9e35fd2e994 python main.py
-
Great ! That worked!! Hope it worked for you too.
- Lets stop and delete the container using
docker stop [Container_ID]
(may take a few sec) anddocker rm [Container_ID]
:
☺ docker stop 9cbff115d0d9
9cbff115d0d9
☺ docker rm 9cbff115d0d9
9cbff115d0d9
☺ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- Remove the image using
docker rmi [Image_Name]/[Image_Id]
:
☺ docker rmi python
Untagged: python:latest
Untagged: python@sha256:d3c16df33787f3d03b2e096037f6deb3c1c5fc92c57994a7d6f2de018de01a6b
Deleted: sha256:8cad8eec9ec53ea519f50fce862660e731b8a3b4ff5bda2af059d12495f5df08
☺ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2. With Dockerfile
:
- Create this docker file with the name
Dockerfile
and add the following inside this:
# Pull the latest python docker image layer in as a base.
FROM python:latest
#Copy this file to the container
COPY ./main.py .
#Run the python file
CMD ["python", "main.py"]
- Build the Dockerfile to create an image and a container :
-t: tag
-> Name to the image created
☺ docker build -t docker-first .
[+] Building 0.1s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 129B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:latest 0.0s
=> CACHED [1/1] FROM docker.io/library/python:latest 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:f6690f886d3e9a06263105e9d638936ba1ecef702eefba0522ed3fc3a0ad383e 0.0s
=> => naming to docker.io/library/docker-first
- Verify the image:
☺ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-first latest f6690f886d3e 4 days ago 875MB
- Run the python file which we copied inside by running:
☺ docker run docker-first
hello world
- You will see there is no container running, as it started, ran the python file and stopped:
☺ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- But when you run this to see all the containers, you will see :
docker ps [OPTIONS]
-a
,--all
Show all containers
☺ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1820c86eecd docker-first "python main.py" 5 minutes ago Exited (0) 5 minutes ago ecstatic_engelbart
- Great! You can take a rest for now… We will get into more details soon. 😁