Docker is a popular platform for containerization, a lightweight and portable way of packaging and running applications. It allows developers to create self-contained environments that can be deployed and managed consistently across different platforms.
1. Understanding Docker Concepts
Docker
is a platform for packaging, deploying, and managing applications that run in containers. Containers are a way of isolating resources (e.g., memory, CPU, storage) from the underlying operating system. Each container has its own isolated environment where it can execute without interfering with other processes on the host machine.
- Docker
images
are like blueprints for creating containers. They contain all the necessary instructions and files needed to run an application or service in a containerized environment.
Dockerfiles
are text files that define how to create a Docker image. They use commands such as FROM
, RUN
, COPY
, etc., to build the image step by step.
- A
Docker registry
is a repository for storing and distributing Docker images.
- A
Docker container
is an instance of a Docker image that is running on a host machine. It executes the application defined by the image, providing isolation from the rest of the system.
Docker Compose
is a tool for defining and running multi-container applications. It uses a YAML file to configure services (containers) and their dependencies.
- A
Docker volume
is a storage mechanism for sharing data between containers or with the host machine. Volumes allow you to persist data even after containers are destroyed, making it easy to share data across multiple containers.
Docker swarm mode
allows you to manage multiple Docker hosts as a single cluster. You can deploy services across the nodes in the swarm and let Docker manage resource allocation and container placement.
- A
Docker network
is an isolated network for connecting containers together or to the host machine. It provides secure communication between containers without exposing ports on the host machine.
Docker Machine
allows you to provision and manage Docker hosts, whether they are physical machines, virtual machines (e.g., in a cloud), or containers running within other Docker containers.
Docker Hub
is an online repository for sharing Docker images with others. It’s one of the largest public registries, but you can also use private registries for storing and distributing your own images.
- A
Dockerfile
is a text file that contains all the commands needed to build a Docker image. Dockerfiles are used as input when building an image with the docker build
command.
Containers
are lightweight, ephemeral, and portable runtime environments for applications. They provide isolation from the host machine and each other, allowing you to run multiple containers on a single host without interference.
Docker Desktop
is an application that allows you to run Docker on Windows or macOS, providing access to the Docker CLI (command-line interface) and GUI tools for managing images and containers.
- A
Docker Compose file
is a YAML file used to define and configure multi-container applications with Docker Compose. It defines services, volumes, networks, and
other resources needed for your application to run.
2. Installing Docker
3. Running Your First Container
-
Launch a terminal/command prompt.
-
Pull a pre-built image from Docker Hub, e.g., docker pull nginx
.
-
Run a container using the pulled image: docker run -it --rm nginx
.
-
Open a web browser and navigate to localhost:80
to see the default Nginx welcome page.
4. Building Your Own Images
- Create an empty folder and navigate to it in your terminal.
- Create a
Dockerfile
. This is the file that contains the instructions on how to build our image. In this case, we will use a simple Dockerfile that starts with a
basic Go base image and copies our source code into it:
1
2
3
4
5
6
7
8
9
10
11
|
# syntax=docker/dockerfile:1
FROM golang:alpine AS builder
WORKDIR /go/src/github.com/user/my-app
COPY . ./
RUN go mod init my-app
RUN go build -o /bin/my-app
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /bin/my-app /root/
CMD ["./my-app"]
|
- Create a
main.go
file with the following content (a simple “Hello world” program):
1
2
3
4
5
|
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
|
- Build the Docker image using:
1
|
docker build -t my-golang-app .
|
- Run a container from this image using:
1
|
docker run my-golang-app
|
You should see “Hello, World!” printed in your terminal as output. If everything works correctly, you now have a working Docker image that contains your Go app. You can push it to a registry or use it locally for development and testing purposes.
5. Managing Containers
-
docker start/stop/restart: Start, stop, or restart a container.
-
docker exec: Execute a command inside a running container.
-
docker attach: Attach to a running container’s console.
-
docker logs: View a container’s logs.
-
docker rm: Remove a stopped container.
6. Using Docker Compose
- Create a
docker-compose.yml
file in your project directory. This is where you will define the services that make up your application, as well as any volumes or
networks needed to support them.
- Example
docker-compose.yml
file:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
version: "3"
services:
gateway:
image: "my_registry/gateway:latest"
ports:
- "80:8080"
depends_on:
- auth
- post
auth:
image: "my_registry/auth:latest"
post:
image: "my_registry/post:latest"
|
This configuration defines three services: gateway
, auth
, and post
. Each service uses a different image. The gateway
service also exposes port 80 on the host machine, while the other two services don’t expose any ports to the outside world. Note that the gateway
service depends on the auth
and post
services, ensuring they are started before it.
- Create a Docker image for each service using the appropriate
Dockerfile
. You can use multi-stage builds or separate images for each service.
- Run your application using
docker compose up -d
, which will start all services defined in the docker-compose.yml
file.
- Test your application by sending requests to the gateway service on port 80. Docker Compose will manage the networking between the containers, allowing you to communicate with each service as if they were running locally.
7. Common Commands
- Build an image from a Dockerfile (assuming you are in the directory that contains the Dockerfile):
1
|
$ docker build -t my-image .
|
- Run a container based on an existing image (e.g., nginx):
1
|
$ docker run -d --name webserver -p 80:80 nginx
|
- List all running containers:
- Stop one or more running containers:
1
|
$ docker stop <container_id>
|
- Start a stopped container:
1
|
$ docker start <container_id>
|
- Remove a container (the container must be stopped first):
1
|
$ docker rm <container_id>
|
- Pull an image from Docker Hub:
1
|
$ docker pull <image_name>:<tag>
|
- List all images on your local machine:
- Remove an image from your local machine (note that this will not remove the underlying layers if other images are using them):
1
|
$ docker rmi <image_id>
|
- Tag an existing image with a new name or tag:
1
|
$ docker tag <original_image>:<tag> <new_name>:<tag>
|
- Push an image to Docker Hub (make sure you are logged in first):
1
|
$ docker push <image_name>:<tag>
|
- Log in to Docker Hub:
1
2
3
4
|
$ docker login
Username: <your_username>
Password: ***********
Login Succeeded
|
- Log out of Docker Hub:
These are some common commands that you may find useful when working with Docker images and containers. If you need to learn more about specific Docker concepts or commands, you can check out the official Docker documentation .