Intorduction to Docker
PS: Cheatsheet included
No more "it works on my machine"
Docker is a software that provides lightweight operating system virtualization also known as containerization. Docker Containers require minimum resources to run a code and also these resources can be increased or decreased according to your needs.
Docker Engine shares resources with the host but it isolates the environment. Unlike VMs, containers are lightweight and do not require a full operating system to run an app or a code piece.
Docker solves the most common problem which is “it works on my machine”.
Creating the same environment to work or to deploy an app is hard work to do especially in a large team.
Docker allows you to run any code or tools in any environment or OS because the code will run on an isolated lightweight kernel.
You can,
- tailor the configuration and make unique containers,
- use pre made official or unofficial docker containers,
- reduce the spent time of the creation of an environment for every developer and every deployment.
How to install Docker ?
Install Docker & Docker Desktop
Docker Container Lifecycle
Let’s see how a container lives,
- Pull an image from Docker Hub
- Run the image with Docker Engine
- Image becomes a container
- Containers can be deleted, stopped, run
- Commit changes & push to Docker Hub
- Repeat
Docker Basic Commands
- How to pull an image from Docker Hub ?
$ docker pull <image_name>
- How to run an image ?
$ docker run -it -d <image_name>
- How to stop the container?
$ docker stop <container_id>
- How to shut down the container?
$ docker kill <container_id>
- How to list running containers?
$ docker ps
- How to list all the containers ?
$ docker ps -a
- How to run interact with the terminal in a container ?
$ docker exec -it <container_id> bash
- How to remove a container?
$ docker rm <container_id>
- How to remove an image?
$ docker rmi <image_id>
How to save changes?
$ docker commit <image_id / container_id> <name>
more examples about commit
command
- How to delete all the containers?
$ docker rm -f$(docker ps -a -q)
- How to push a container to Docker Registry?
$ docker push <container_name>
- How to build a Dockerfile?
$ docker build . -t <name> -p <local_port_number>:<app_port_in_container>
What is Dockerfile?
Dockerfile is txt document that lets you customize your image. Dockerfile helps you to define commands and automates the building proccess.
Dockerfile Basics & Important Commands
- How to pull an image?
FROM: <image_name>
- How to add a file from local machine into the container ?
ADD: <local_source> <destination_in
- How to copy a file from a local machine into the container?
COPY: <local_source> <destionation_in_container>
- How to run a command inside a container ?
RUN: <commands>
- How to run a command after the container is started?
CMD: <commands>
- How to run a command when container initializes ?
ENTRYPOINT: <commands>
- How to define environment variables ?
ENV: <env_var_name> <value>
- Difference between
ADD
andCOPY
COPY
should be used when copying a file from local machine into the containerADD
also supports file download from an URL and extract a zipped file
2. Difference between RUN
ENTRYPOINT
and CMD
RUN
executes commands in the build phase of the containerENTRYPOINT
executes commands every time when container initializesCMD
executes commands after container is started
Dockerfile example
Docker Volumes
Containers are runnig snapshots of an Image due to that, they are not saving their states when they stopped or re-deployed. If you want to store any data you need to point out a location on the host machine for persistency.
In short, Docker Volumes are used to store data permanently on the host. There are two ways to do it
- bind mount is one of them, just pass
-v <local_destination> <destination_inside_container>
This can cause a problem because it uses local path, also can not work in different environment
2. second way is to create Volumes through Docker.
- How to create a docker volume?
$ docker volume create <volume_name>
- How to list volumes?
$ docker volume ls
- How to use volumes? Just pass this when running a container,
$ -mount source=<name_of_volume>, target=<directory_inside_container>
The End
I hope you find this article useful. As you can see, Docker is a simple and well-designed tool. You can now get things done in an easy way.