💡 Introduction
Docker is a powerful tool that simplifies application deployment by using containerization. This guide aims to help you understand and efficiently use Docker in your daily development workflow. By the end of this article, you will have a basic understanding about Docker commands and best practices.
⚙️ Prerequisites
Before diving into the commands:
- Ensure you have Docker installed on your machine. You can download it from the official Docker website.
- Basic knowledge of the command line will also be helpful.
✍️ Getting Started with Docker
👉 Basic Docker Commands
To run your first container, use the docker run
command. It will check if the image is available locally; if not, it will download it and then execute the container.
docker run hello-world
To view active containers:
docker ps
To view all containers, including stopped ones:
docker ps -a
To start and stop containers:
docker start container-name
docker stop container-name
To run a container and delete it after execution:
docker run -it --rm ubuntu bash
To access a running container:
docker exec -it container-name bash
To remove a container:
docker rm container-name -f
To remove all containers at once:
docker rm $(docker ps -a -q) -f
👉 Bind Mounts
Bind mounts allow you to map a local directory to a container directory, ensuring persistent storage.
docker run -d --name nginx -p 80:80 -v /c/users/danilo/www/html:/usr/share/nginx/html nginx
Using the mount
flag:
docker run -d --name nginx -p 80:80 --mount type=bind,source=/c/users/danilo/www/html,target=/usr/share/nginx/html nginx
👉 Docker Volumes
Docker volumes are the preferred way to handle persistent data.
To create a volume:
docker volume create volume-name
To inspect a volume:
docker volume inspect volume-name
To attach a volume to a container:
docker run --name nginx -d --mount type=volume,source=volume-name,target=/app nginx
To clean up unused volumes:
docker volume prune
👉 Docker Compose
Docker Compose allows you to define multi-container applications using a YAML file.
Below has an example of a docker-compose.yml
file, for laravel and nginx container application services, where the images is coming from my Docker Hub account.
version: '3'
services:
laravel:
build:
context: ./laravel
dockerfile: Dockerfile.prod
image: daniilomello/laravel:prod
container_name: laravel
networks:
- laranet
nginx:
build:
context: ./nginx
dockerfile: Dockerfile.prod
image: daniilomello/nginx:prod
container_name: nginx
networks:
- laranet
ports:
- "8080:80"
networks:
laranet:
driver: bridge
To start the containers:
docker-compose up -d --build
To stop the containers:
docker-compose down
👉 Working with Docker Images
To download an image:
docker pull image-name
To list available images:
docker images
To remove an image:
docker rmi image-name
Example Dockerfile
for creating a custom image:
FROM nginx:latest
WORKDIR /app
RUN apt-get update && \
apt-get install vim -y
COPY html/ /usr/share/nginx/html
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
To build and run your custom image:
docker build -t daniilomello/image-name .
docker run --rm -d -p 8080:80 daniilomello/image-name
To push your image to Docker Hub:
docker push daniilomello/image-name
👉 Docker Networking
Docker provides different network types for container communication.
To clean up unused networks:
docker network prune
To inspect an existing network:
docker network inspect bridge
To create a new network:
docker network create --driver bridge network-name
To run a container within a network:
docker run -dit --name ubuntu1 --network network-name bash
To connect an existing container to a network:
docker network connect network-name ubuntu3
🚀 Creating a NodeJS Express API
Create a container with a volume for your Node.js project:
docker run --rm -it -v //c/Users/danilo/www/docker/node/:/usr/src/app -p 3001:3001 node:15 bash
Initialize a package.json
file:
npm init -y
Install Express:
npm install express
Create an index.js
file:
const express = require('express');
const app = express();
const port = 3001;
app.get('/', (req,res) => {
res.send('<h1>Hello World</h1>');
});
app.listen(port, () => {
console.log(`🚀 Running on http://localhost:${port}/`);
});
Create a Dockerfile
for your Node.js API:
FROM node:15
WORKDIR /usr/src/app
COPY . .
EXPOSE 3001
CMD ["node", "index.js"]
Build and run the image:
docker build -t daniilomello/api-node .
docker run --name api-node daniilomello/api-node
Push it to Docker Hub:
docker push daniilomello/api-node
🤝 Conclusion
With this guide, you now have a basic foundation about Docker. This knowledge will help you containerize your applications efficiently and collaborate on projects that use Docker.
If you found this guide helpful, feel free to share it with a friend or post it on your social media to help others learn Docker. Thank you for reading! 🚀