Docker In Nutshell + Docker Cheat Sheet
Mohamed Kramti
Posted on September 29, 2024
Note from the author
Hello 👋
This is my first post on dev.to
If you like my post, please like it and let’s connect on LinkedIn
Check out my blog at https://kramti.com
Scroll to the end of the post for an amazing Docker Cheat Sheet Docker In Nutshell + Docker Cheat Sheet
Introduction
This article covers everything you need to know to start using Docker.
I will begin by presenting Docker and the issues it solves, then I’ll clarify the most used terms related to Docker, and finally, we will dockerize an application and run our first container.
What Problem Does Docker Solve?
1) Settings up an environment can be complex for some people, especially for a less technical person.
Docker will install all the dependencies your application need; You don’t have to worry about setting up your MySQL, installing Java, node or any other tool.
2) If you don’t want to share the filesystem, database, network interface, port or OS … between applications docker got you covered thanks to its isolation technique.
3) Security - Isolation concerns security too, deploying a vulnerable application or a malicious code can damage the other applications, and that's why it’s better to keep apps isolated.
4) Portability - As long as the Docker versions are compatible your shipped software will work anywhere
Key terms
Docker
Docker is an open-source project that:
- Will help you deploy your applications.
- Packages your applications into an image that can be used anywhere.
- Uses containers to run applications in isolated environments
Images
- Images contain:
- Application code
- Dependencies
- Configuration
- Runtime environment
Important: Images are read-only you can’t change the content of an image; if you need to change the content of an image you have to create a new image with the changes.
Containers
- A container:
- Is a runnable instance of an image.
- Completely Isolated process (runs undependably from any process on your computer).
- Help to improve the deployment of an application.
Note: We can create multiple containers from the same image.
Install Docker
Install Docker on Windows
Install WSL
WSL stands for Windows Subsystem for Linux.
It’s a feature in Windows 10 version 2004+ and windows 11; it allows Linux programs to run on Windows
To install WSL simply run this command on PowerShell
wsl --install
Alternatively, if you want to install it manually follow this guide
Manual installation steps for older versions of WSL | Microsoft Learn
Install Docker Desktop
The next step is to install the Docker Desktop
You can follow this link to download the Docker Desktop
Install Docker Desktop on Windows | Docker Documentation
The installation is really simple we won’t go into in this tutorial
Install Docker on Ubuntu
Before installing docker on your Linux system you must install some dependencies that Docker needs
sudo apt-get install ca-certificates curl gnupg lsb-release
Add the Docker GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Set up the repository
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
And lastly, install Docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Install Docker on other platforms
Docker supports a variety of platforms (Linux platforms, macOS and Windows). For more details check the official documentation Install Docker Engine | Docker Documentation
Check the installation
After installing docker you can run your first container using this command
docker run hello-world
Output
Hello from Docker!
This message shows that your installation appears to be working correctly.
…
If you see this message, then congratulation you have successfully installed Docker.
DockerHub
What is DockerHub?
Docker Hub is a service provided by Docker to download and share docker images.
You can access DockerHub from this link Docker Hub Container Image Library | App Containerization
Download an image from DockerHub
Let’s say you need a Linux OS to deploy your application
1- Go to hub.docker.com and search for the OS you need to install I’ll take Ubuntu as an example
2- You will get a variety of images, select the image you need to install in my case the official Ubuntu image
3- Run the docker pull ubuntu command on your terminal, CMD or PowerShell.
Output
…
Status: Downloaded newer image for ubuntu: latest
docker.io/library/ubuntu:latest
Congratulation now you have downloaded your first image, you may have noticed that the image size is way smaller than the official ubuntu Iso image and that’s another advantage of docker.
Note: By default, docker pull ubuntu will pull the latest version, but you can specify the docker version following the image name example docker pull ubuntu:20.04
Note: Alternatively, if you don’t want to go to hub.docker.com and search for the image using your browser you can search for images using your command line simply type
docker search ubuntu
This command will list all the available images, their descriptions and more …
DockerFile
What is a DockerFile?
A DockerFile is a file that contains a set of instructions for docker to create your own image, for example, it may contain the OS your application will run on, the source code of your application etc…
A DockerFile does not have an extension and it contains a set of instructions
How to create a DockerFile?
We learned previously that a DockerFile is a set of instructions below are the most common instructions used on DockerFiles
FROM is used to define the parent image that the new image will be built.
COPY & ADD is used to copy files to the filesystem of the image.
WORKDIR is used to specify the working directory.
RUN is used to run commands to run inside the container.
CMD is used to specify a command that executes by default when running a Docker container.
For more DockerFile instructions visit Dockerfile reference | Docker Documentation
Example: Dockerizing a Node.js web app
Note: Optional you can install a Docker extension in your editor.
Step 1 - Define the parent container
FROM node:16
Step 2 - Define the working directory which will contain our node application
WORKDIR /usr/src/app
Note: The following instructions will be executed inside /usr/src/app
Step 3 - Copy the node application to our image
COPY . .
Note: The first dot represents the current file on our host machine, and the second dot is the current working directory.
Step 4 - Informing Docker of the port our application is listening to
EXPOSE 4444
Step 5 - Installing the application dependencies
RUN npm install
Step 6 - Lastly define the command to run the application
CMD [ "node", "server.js" ]
That’s all! The DockerFile should look like that
FROM node:16
WORKDIR /usr/src/app
COPY . .
EXPOSE 4444
RUN npm install
CMD [ "node", "server.js" ]
DockerIgnore file
The .dockerIgnore file is similar to the .gitignore you simply include the path to the files or folders you want Docker to ignore while dockerizing your application.
In the previous example, we can create a .dockerignore file to ignore the node_modules folder
Simply create a .dockerignore file and it should contain one line
node_modules
Build the Image
Now that we had set up the DockerFile and the .dockerIgnore file we can build our image
to do so simply run the following command:
docker build -t [ Type your image tag here ]
Example
docker build -t myFirstImage
Start & Stop the Container
To start the image created you can use the Docker Desktop application just go to images ⇒ select the image and click run
or we can use the command line
This command displays all the available images
docker images
and to start the container
docker run -p 8080:4444 --name [Give the container a name] [Image name]
Example:
docker run -p 8080:4444 --name myContainer myFirstImage
Note: -p 8080:4444
is used to map the 8080 port to the exposed port 4444.
We will be able to access the exposed port using the 8080 port on the local host.
To see the running container run
docker ps
This command displays the running containers along with their ID, created date, and Ports
To stop the container run
docker stop [Container id or Container name]
Example:
docker stop myContainer
Conclusion
In this article you learned pretty much everything you need to get started with Docker from basic concepts, downloading and installing Docker to creating and managing your Docker images and container.
Posted on September 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.