How to use docker
In my last article, I wrote about how Docker Hello world works. In that, I have explained about container and docker ecosystem
Docker packs, ships, and runs the application as a lightweight containerization tool and builds an agile software delivery pipeline to ship new features faster. so let’s start using docker.
I recommend you start with the docker command line (CLI) tool first, then go for the docker desktop. Now let's say you want your Dockerfile image first, take a simple example
- Create a Dockerfile with no extension
FROM alpine
CMD ["echo", "Love from India"]
In the first line, we are using a base image (A base image is an image that is used to create all of your container images ) You can use multiple flavors of images, such as Ubuntu, Debian, etc., but alpine images are small in size.
Dockerfile has FROM, COPY, RUN, CMD, etc instructions which tell docker demon how to build an image. Each instruction is a layer in the container. CMD is a special instruction that tells specifies what command to run within the container. Best practices for writing Dockerfile
- Let’s build the image
docker build -t love:latest .
Docker build is a command that builds Docker images from a Dockerfile and a “context”. In the above command, we used the -t
option as a tag in the 'name: tag' format, and .
is a reference to context (The transfer of context from the local machine to the Docker daemon) and we have given the name love:latest
to our image.
As you see in step 1/2
the image alpine:latest
image got pulled from the docker hub and in the second step 2/2
we have given instructions to echo
a string which is love from India.
- Running docker container
docker container run love
when you run the above command you will see the output to the terminal
If you want just practice these commands without installing docker on your machine, head to https://labs.play-with-docker.com/ on this platform, you need a docker id and play with docker.
Why is Docker useful?
Docker allows you to exactly replicate your development environment which gives you leverage to distribute it QA team or in a production environment. It alleviates the problem of “it’s broken on my machine!”.
If you want to add another server (Ex: Database) to your existing infrastructure you don't have to worry about configuring that server and reinstalling the dependency, just pull that particular image from the Docker hub.
Docker is an easy tool to use when you get started using its daily development and OPS practices. with docker, you can run multiple separate containers with, a separate version of Python or any other language, app versions
Docker makes running multiple servers very easy, especially with orchestration engines like Kubernetes and Docker Swarm
When not to use Docker?
By default docker container run as root
so every service in the container runs as root, it’s not the best implementation for the production environment.
The size of your docker container application grows exponentially when new updates come in.
Building, testing, managing, and distributing Large and complicated applications require a dedicated sysadmin or DevOps engineer team.